| 介紹 checkbox是一個(gè)非常有用的頁面表單項(xiàng),在讓用戶進(jìn)行多重選擇的情況下,它甚至可以允許用戶選擇全部項(xiàng)目或是一個(gè)都不選。但是,盡管這是一個(gè)非常優(yōu)秀的表單元素,但在我們的工作中,在如何正確地保存選擇項(xiàng)這方面總存在一些易混淆的情況發(fā)生。本文將描述在遵循好的數(shù)據(jù)庫設(shè)計(jì)原則的方法下,如何把checkbox選擇項(xiàng)正確地保存在數(shù)據(jù)庫中。 要求 本文將闡述如何把選擇項(xiàng)正確地保存在用戶數(shù)據(jù)庫中的方法。盡管這里包括了有用的php代碼,但我將從數(shù)據(jù)庫設(shè)計(jì)的觀點(diǎn)來表達(dá)它們,所以,你可以很方便地使用任何一個(gè)數(shù)據(jù)庫和服務(wù)器端腳本語言來實(shí)現(xiàn)。我只是想提供一個(gè)如何做的方法,讓你能應(yīng)用于你自己的站點(diǎn)中。如果你想運(yùn)行這里的源碼,你需要安裝php、mysql和網(wǎng)絡(luò)服務(wù)器。 例1:招聘站點(diǎn) 假如你被要求做一個(gè)招聘類的網(wǎng)站,允許求職的軟件開發(fā)人員填寫他們的技能,讓雇主能訪問這個(gè)站點(diǎn)并根據(jù)求職者的技能找到合適的員工。你也知道,一個(gè)開發(fā)人員擁有的技能會(huì)多于一個(gè),因此你決定這樣設(shè)計(jì)你的站點(diǎn)。 每一個(gè)求職者將允許訪問本站,注冊一個(gè)用戶,并且輸入他的技能,checkbox就派上用場了,你可能想作這樣的一頁: __ php __ mysql __ zope __ perl __ javascript __ jsp [提交] 每一個(gè)求職都可以選擇他所擁有的技能。顯然對于不同人來說這選擇項(xiàng)是不同的。一個(gè)人可能會(huì)是php和mysql,其它人可能只是jsp。你將如何保存這些選擇呢?一個(gè)很自然的想法是針對每個(gè)選項(xiàng)建一個(gè)字段,這樣開始可以正常工作。但是隨后你可能會(huì)發(fā)現(xiàn),當(dāng)你想擴(kuò)展或調(diào)整時(shí),麻煩就來了,你可能不得不修改你的表結(jié)構(gòu)。 好的方法應(yīng)是這樣的: 你應(yīng)有一個(gè)用戶表包含用戶的注冊信息,如用戶名、密碼和其它一些你需要的什么內(nèi)容。假如你直接使用本文后面給出的源碼,你要建一個(gè)簡單的表如下: id username 1 user1 2 user2 3 user3 我們先建一個(gè)表 "const_skills" 用如下的 sql 語句: sql> create table const_skills ( id int not null primary key, value varchar(20) ); 現(xiàn)在我們加入技能: sql> insert into const_skills(id, value) values (1, "php"); sql> insert into const_skills(id, value) values (2, "mysql"); sql> insert into const_skills(id, value) values (3, "zope"); sql> insert into const_skills(id, value) values (4, "perl"); sql> insert into const_skills(id, value) values (5, "javascript"); sql> insert into const_skills(id, value) values (6, "jsp"); 你的 const_skills 現(xiàn)在應(yīng)是這樣的: id value 1 php 2 mysql 3 zope 4 perl 5 javascript 6 jsp 這個(gè)表只是讓用戶可以選擇相應(yīng)的技能,現(xiàn)在,再建一個(gè)表 lookup_skills 用如下的sql: sql> create table lookup_skills ( id int not null auto_increment primary key, uid int, skill_id int ); 這個(gè)表lookup_skills的目的是提供從用戶表到開發(fā)技能表之間的一個(gè)映射關(guān)系。換句話說,它讓我們保存開發(fā)者和他們有的技能,如,當(dāng)求職者完成選擇點(diǎn)擊提交時(shí),我們將填寫這個(gè)表用checkbox中被選定的那些值。對于每一個(gè)選上的技能,我們在這個(gè)表中加一條記錄,記下用戶id及所選項(xiàng)的id。(想必大家都清楚了吧。我譯到這,嘿嘿…) 在我們看這個(gè)插入記錄的代碼之前,我們先設(shè)計(jì)一下這個(gè)頁面,應(yīng)有的內(nèi)容有一個(gè)表單,我們可以查詢的數(shù)據(jù)庫并且取checkbox標(biāo)簽從const_skills表中,建這個(gè)checkbox表單項(xiàng)。 代碼如下: < ?php /* insert code to connect to your database here */ /* get the checkbox labels */ $skills = get_checkbox_labels("const_skills"); /* create the html code for a formatted set of checkboxes */ $html_skills = make_checkbox_html($skills, 3, 400, "skills[]"); ? > < html > < body > < br > < form name="skills" method="post" action="insertskills.php" > check off your web development skills: < ? echo "$html_skills"; ? > < br > < input type="submit" value="submit" > < /form > < /body > < /html > < ?php function get_checkbox_labels($table_name) { /* make an array */ $arr = array(); /* construct the query */ $query = "select * from $table_name"; /* execute the query */ $qid = mysql_query($query); /* each row in the result set will be packaged as an object and put in an array */ while($row= mysql_fetch_object($qid)) { array_push($arr, $row); } return $arr; } /* prints a nicely formatted table of checkbox choices. $arr is an array of objects that contain the choices $num is the number of elements wide we display in the table $width is the value of the width parameter to the table tag $name is the name of the checkbox array $checked is an array of element names that should be checked */ function make_checkbox_html($arr, $num, $width, $name, $checked) { /* create string to hold out html */ $str = ""; /* make it */ $str .= "< table width="$width" border="0" >n"; $str .= "< tr >n"; /* determine if we will have to close add a closing tr tag at the end of our table */ if (count($arr) % $num != 0) { $closingtr = true; } $i = 1; if (isset($checked)) { /* if we passed in an array of the checkboxes we want to be displayed as checked */ foreach ($arr as $ele) { $str .= "< td >< input type="checkbox" name="$name" value="$ele- >id""; foreach ($checked as $entry) { if ($entry == $ele- >value) { $str .= "checked"; continue; } } $str .= " >"; $str .= "$ele- >value"; if ($i % $num == 0) { $str .= "< /tr >n< tr >"; } else { $str .= "< /td >n"; } $i++; } } else { /* we just want to print the checkboxes. none will have checks */ foreach ($arr as $ele) { $str .= "< td >< input type="checkbox" name="$name" value="$ele- >id" >"; $str .= "$ele- >value"; if ($i % $num == 0) { $str .= "< /tr >n< tr >"; } else { $str .= "< /td >n"; } $i++; } } /* tack on a closing tr tag if necessary */ if ($closingtr == true) { $str .= "< /tr >< /table >n"; } else { $str .= "< /table >n"; } return $str; } ? > |
新聞熱點(diǎn)
疑難解答