PHP中在数据库中保存Checkbox数据(1)
介绍
checkbox是一个非常有用de页面表单项,在让用户进行多重选择de情况下,它甚至可以允许用户选择全部项目或是一个都不选.但是,尽管这是一个非常优秀de表单元素,但在我de工作中,在如何正确地保存选择项这方面总存在一些易混淆de情况发生.本文将描述在遵循好de数据库设计原则de方法下,如何把checkbox选择项正确地保存在数据库中.
要求
本文将阐述如何把选择项正确地保存在用户数据库中de方法.尽管这里包括了有用dePHP相关代码,但我将从数据库设计de观点来表达它们,所以,您可以很方便地使用任何一个数据库和服务器端脚本语言来实现.我只是想提供一个如何做de方法,让您能应用于您自己de站点中.如果您想运行这里de源码,您需要安装php、mysql和网络服务器.
例1:招聘站点
假如您被要求做一个招聘类de网站,允许求职de软件开发人员填写他们de技能,让雇主能访问这个站点并根据求职者de技能找到合适de员工.您也知道,一个开发人员拥有de技能会多于一个,因此您决定这样设计您de站点.
每一个求职者将允许访问本站,注册一个用户,并且输入他de技能,Checkbox就派上用场了,您可能想作这样de一页:
__ PHP __ MySQL __ Zope
__ Perl __ Javascript __ JSP
[提交]
每一个求职都可以选择他所拥有de技能.显然对于不同人来说这选择项是不同de.一个人可能会是PHP和Mysql,其它人可能只是JSP.您将如何保存这些选择呢?一个很自然de想法是针对每个选项建一个字段,这样开始可以正常工作.但是随后您可能会发现,当您想扩展或调整时,麻烦就来了,您可能不得不修改您de表结构.
好de方法应是这样de:
您应有一个用户表包含用户de注册信息,如用户名、密码和其它一些您需要de什么内容.假如您直接使用本文后面给出de源码,您要建一个简单de表如下:
id username
1 User1
2 User2
3 User3
我先建一个表 “const_skills” 用如下de 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”);
您de const_skills 现在应是这样de:
id value
1 PHP
2 MySQL
3 Zope
4 Perl
5 Javascript
6 JSP
这个表只是让用户可以选择相应de技能,现在,再建一个表 lookup_skills 用如下deSQL:
SQL> CREATE TABLE lookup_skills (
id int not null auto_increment primary key,
uid int,
skill_id int );
这个表lookup_skillsde目de是提供从用户表到开发技能表之间de一个映射关系.换句话说,它让我保存开发者和他们有de技能,如,当求职者完成选择点击提交时,我将填写这个表用checkbox中被选定de那些值.对于每一个选上de技能,我在这个表中加一条记录,记下用户id及所选项deid.(想必大家都清楚了吧.我译到这,嘿嘿…)
在我看这个插入记录de相关代码之前,我先设计一下这个页面,应有de内容有一个表单,我可以查询de数据库并且取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;
}
? >
Tags: (1), bo, ch, CK, ec, he, HP, kb, ox, PH, P中, x数, 中保, 中在, 保存, 在数, 存C, 库中, 据1, 据库, 数据