介紹
checkbox是一個非常有用的頁面表單項,在讓用戶進行多重選擇的情況下,它甚至可以允許用戶選擇全部項目或是一個都不選。但是,盡管這是一個非常優秀的表單元素,但在我們的工作中,在如何正確地保存選擇項這方面總存在一些易混淆的情況發生。本文將描述在遵循好的數據庫設計原則的方法下,如何把checkbox選擇項正確地保存在數據庫中。
要求
本文將闡述如何把選擇項正確地保存在用戶數據庫中的方法。盡管這里包括了有用的PHP代碼,但我將從數據庫設計的觀點來表達它們,所以,你可以很方便地使用任何一個數據庫和服務器端腳本語言來實現。我只是想提供一個如何做的方法,讓你能應用于你自己的站點中。如果你想運行這里的源碼,你需要安裝php、mysql和網絡服務器。
例1:招聘站點
假如你被要求做一個招聘類的網站,允許求職的軟件開發人員填寫他們的技能,讓雇主能訪問這個站點并根據求職者的技能找到合適的員工。你也知道,一個開發人員擁有的技能會多于一個,因此你決定這樣設計你的站點。
每一個求職者將允許訪問本站,注冊一個用戶,并且輸入他的技能,Checkbox就派上用場了,你可能想作這樣的一頁:
__ PHP __ MySQL __ Zope __ Perl __ Javascript __ JSP
[提交]
每一個求職都可以選擇他所擁有的技能。顯然對于不同人來說這選擇項是不同的。一個人可能會是PHP和Mysql,其它人可能只是JSP。你將如何保存這些選擇呢?一個很自然的想法是針對每個選項建一個字段,這樣開始可以正常工作。但是隨后你可能會發現,當你想擴展或調整時,麻煩就來了,你可能不得不修改你的表結構。 好的方法應是這樣的:
你應有一個用戶表包含用戶的注冊信息,如用戶名、密碼和其它一些你需要的什么內容。假如你直接使用本文后面給出的源碼,你要建一個簡單的表如下:
id username 1 User1 2 User2 3 User3
我們先建一個表 "const_skills" 用如下的 SQL 語句:
SQL> CREATE TABLE const_skills ( id int not null primary key, value varchar(20) );
現在我們加入技能:
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 現在應是這樣的:
id value 1 PHP 2 MySQL 3 Zope 4 Perl 5 Javascript 6 JSP
這個表只是讓用戶可以選擇相應的技能,現在,再建一個表 lookup_skills 用如下的SQL:
SQL> CREATE TABLE lookup_skills ( id int not null auto_increment primary key, uid int, skill_id int );
這個表lookup_skills的目的是提供從用戶表到開發技能表之間的一個映射關系。換句話說,它讓我們保存開發者和他們有的技能,如,當求職者完成選擇點擊提交時,我們將填寫這個表用checkbox中被選定的那些值。對于每一個選上的技能,我們在這個表中加一條記錄,記下用戶id及所選項的id。(想必大家都清楚了吧。我譯到這,嘿嘿…)
在我們看這個插入記錄的代碼之前,我們先設計一下這個頁面,應有的內容有一個表單,我們可以查詢的數據庫并且取checkbox標簽從const_skills表中,建這個checkbox表單項。
代碼如下:
< ?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; }
? >
|