在数据库中保存Checkbox数据(一)
时间:2007-02-17 来源:PHP爱好者
介绍
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
checkbox是一个非常有用的页面表单项,在让用户进行多重选择的情况下,它甚至可以允许用户选择全部项目或是一个都不选。但是,尽管这是一个非常优秀的表单元素,但在我们的工作中,在如何正确地保存选择项这方面总存在一些易混淆的情况发生。本文将描述如何把checkbox选择项正确地保存在数据库中,遵循好的数据库设计原则的方法下。
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
要求
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
本文将阐述如何把选择项正确地保存在用户数据库中的方法。尽管这里包括了有用的PHP代码,但我将从数据库设计的观点来表达它们,所以,你可以很方便地使用任何一个数据库和服务器端脚本语言来实现。我只是想提供一个如何做的方法,让你能应用于你自己的站点中。如果你想运行这里的源码,你需要安装php、mysql和网络服务器。
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
例1:招聘站点
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
假如你被要求做一个招聘类的网站,允许求职的软件开发人员填写他们的技能,让雇主能访问这个站点并根据求职者的技能找到合适的员工。你也知道,一个开发人员拥有的技能会多于一个,因此你决定这样设计你的站点。
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
每一个求职者将允许访问本站,注册一个用户,并且输入他的技能,Checkbox就派上用场了,你可能想作这样的一页:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
__ PHP __ MySQL __ Zope
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
__ Perl __ Javascript __ JSP
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
[提交]
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
每一个求职都可以选择他所拥有的技能。显然对于不同人来说这选择项是不同的。一个人可能会是PHP和Mysql,其它人可能只是JSP。你将如何保存这些选择呢?一个很自然的想法是针对每个选项建一个字段,这样开始可以正常工作。但是随后你可能会发现,当你想扩展或调整时,麻烦就来了,你可能不得不修改你的表结构。好的方法应是这样的:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
你应有一个用户表包含用户的注册信息,如用户名、密码和其它一些你需要的什么内容。假如你直接使用本文后面给出的源码,你要建一个简单的表如下:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
id username
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
1 User1
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
2 User2
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
3 User3
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
我们先建一个表 "const_skills" 用如下的 SQL 语句:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> CREATE TABLE const_skills (
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
id int not null primary key,
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
value varchar(20) );
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
现在我们加入技能:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> INSERT INTO const_skills(id, value) VALUES (1, "PHP");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> INSERT INTO const_skills(id, value) VALUES (2, "MySQL");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> INSERT INTO const_skills(id, value) VALUES (3, "Zope");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> INSERT INTO const_skills(id, value) VALUES (4, "Perl");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> INSERT INTO const_skills(id, value) VALUES (5, "Javascript");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> INSERT INTO const_skills(id, value) VALUES (6, "JSP");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
你的 const_skills 现在应是这样的:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
id value
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
1 PHP
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
2 MySQL
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
3 Zope
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
4 Perl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
5 Javascript
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
6 JSP
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
这个表只是让用户可以选择相应的技能,现在,再建一个表 lookup_skills 用如下的SQL:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> CREATE TABLE lookup_skills (
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
id int not null auto_increment primary key,
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
uid int,
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
skill_id int );
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
这个表lookup_skills的目的是提供从用户表到开发技能表之间的一个映射关系。换句话说,它让我们保存开发者和他们有的技能,如,当求职者完成选择点击提交时,我们将填写这个表用checkbox中被选定的那些值。对于每一个选上的技能,我们在这个表中加一条记录,记下用户id及所选项的id。(想必大家都清楚了吧。我译到这,嘿嘿…)在我们看这个插入记录的代码之前,我们先设计一下这个页面,应有的内容有一个表单,我们可以查询的数据库并且取checkbox标签从const_skills表中,建这个checkbox表单项。代码如下:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<?php
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* insert code to connect to your database here */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* get the checkbox labels */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$skills = get_checkbox_labels("const_skills");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* create the html code for a formatted set of
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
checkboxes */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$html_skills = make_checkbox_html($skills, 3, 400, "skills[]");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
?>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<html>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<body>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<br>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<form name="skills" method="POST" action="insertskills.php">
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
Check off your web development skills:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<? echo "$html_skills"; ?>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<br>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<input type="submit" value="Submit">
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
</form>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
</body>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
</html>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<?php
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
function get_checkbox_labels($table_name) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* make an array */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$arr = array();
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* construct the query */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$query = "SELECT * FROM $table_name";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* execute the query */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$qid = mysql_query($query);
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* each row in the result set will be packaged as
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
an object and put in an array */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
while($row= mysql_fetch_object($qid)) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
array_push($arr, $row);
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
return $arr;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* Prints a nicely formatted table of checkbox choices.
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$arr is an array of objects that contain the choices
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$num is the number of elements wide we display in the table
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$width is the value of the width parameter to the table tag
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$name is the name of the checkbox array
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$checked is an array of element names that should be checked
*/
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
function make_checkbox_html($arr, $num, $width, $name, $checked) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* create string to hold out html */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str = "";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* make it */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "<table width="$width" border="0">n";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "<tr>n";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* determine if we will have to close add
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
a closing tr tag at the end of our table */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
if (count($arr) % $num != 0) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$closingTR = true;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$i = 1;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
if (isset($checked)) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* if we passed in an array of the checkboxes we want
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
to be displayed as checked */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
foreach ($arr as $ele) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "<td><input type="checkbox" name="$name" value="$ele->id"";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
foreach ($checked as $entry) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
if ($entry == $ele->value) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "checked";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
continue;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= ">";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "$ele->value";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
if ($i % $num == 0) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "</tr>n<tr>";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
} else {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "</td>n";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$i++;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
} else {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* we just want to print the checkboxes. none will have checks */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
foreach ($arr as $ele) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "<td><input type="checkbox" name="$name" value="$ele->id">";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "$ele->value";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
if ($i % $num == 0) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "</tr>n<tr>";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
} else {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "</td>n";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$i++;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* tack on a closing tr tag if necessary */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
if ($closingTR == true) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "</tr></table>n";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
} else {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "</table>n";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
return $str;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
?>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
[第一节完]
作者:Dan LaFlamme
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
译者:sharetop([email protected])
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
来源: www.phpbuilder.net
php爱好者站 http://www.phpfans.net php动态|php基础|php进阶|php模板.
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
checkbox是一个非常有用的页面表单项,在让用户进行多重选择的情况下,它甚至可以允许用户选择全部项目或是一个都不选。但是,尽管这是一个非常优秀的表单元素,但在我们的工作中,在如何正确地保存选择项这方面总存在一些易混淆的情况发生。本文将描述如何把checkbox选择项正确地保存在数据库中,遵循好的数据库设计原则的方法下。
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
要求
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
本文将阐述如何把选择项正确地保存在用户数据库中的方法。尽管这里包括了有用的PHP代码,但我将从数据库设计的观点来表达它们,所以,你可以很方便地使用任何一个数据库和服务器端脚本语言来实现。我只是想提供一个如何做的方法,让你能应用于你自己的站点中。如果你想运行这里的源码,你需要安装php、mysql和网络服务器。
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
例1:招聘站点
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
假如你被要求做一个招聘类的网站,允许求职的软件开发人员填写他们的技能,让雇主能访问这个站点并根据求职者的技能找到合适的员工。你也知道,一个开发人员拥有的技能会多于一个,因此你决定这样设计你的站点。
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
每一个求职者将允许访问本站,注册一个用户,并且输入他的技能,Checkbox就派上用场了,你可能想作这样的一页:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
__ PHP __ MySQL __ Zope
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
__ Perl __ Javascript __ JSP
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
[提交]
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
每一个求职都可以选择他所拥有的技能。显然对于不同人来说这选择项是不同的。一个人可能会是PHP和Mysql,其它人可能只是JSP。你将如何保存这些选择呢?一个很自然的想法是针对每个选项建一个字段,这样开始可以正常工作。但是随后你可能会发现,当你想扩展或调整时,麻烦就来了,你可能不得不修改你的表结构。好的方法应是这样的:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
你应有一个用户表包含用户的注册信息,如用户名、密码和其它一些你需要的什么内容。假如你直接使用本文后面给出的源码,你要建一个简单的表如下:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
id username
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
1 User1
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
2 User2
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
3 User3
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
我们先建一个表 "const_skills" 用如下的 SQL 语句:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> CREATE TABLE const_skills (
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
id int not null primary key,
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
value varchar(20) );
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
现在我们加入技能:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> INSERT INTO const_skills(id, value) VALUES (1, "PHP");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> INSERT INTO const_skills(id, value) VALUES (2, "MySQL");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> INSERT INTO const_skills(id, value) VALUES (3, "Zope");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> INSERT INTO const_skills(id, value) VALUES (4, "Perl");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> INSERT INTO const_skills(id, value) VALUES (5, "Javascript");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> INSERT INTO const_skills(id, value) VALUES (6, "JSP");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
你的 const_skills 现在应是这样的:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
id value
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
1 PHP
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
2 MySQL
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
3 Zope
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
4 Perl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
5 Javascript
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
6 JSP
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
这个表只是让用户可以选择相应的技能,现在,再建一个表 lookup_skills 用如下的SQL:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
SQL> CREATE TABLE lookup_skills (
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
id int not null auto_increment primary key,
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
uid int,
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
skill_id int );
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
这个表lookup_skills的目的是提供从用户表到开发技能表之间的一个映射关系。换句话说,它让我们保存开发者和他们有的技能,如,当求职者完成选择点击提交时,我们将填写这个表用checkbox中被选定的那些值。对于每一个选上的技能,我们在这个表中加一条记录,记下用户id及所选项的id。(想必大家都清楚了吧。我译到这,嘿嘿…)在我们看这个插入记录的代码之前,我们先设计一下这个页面,应有的内容有一个表单,我们可以查询的数据库并且取checkbox标签从const_skills表中,建这个checkbox表单项。代码如下:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<?php
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* insert code to connect to your database here */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* get the checkbox labels */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$skills = get_checkbox_labels("const_skills");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* create the html code for a formatted set of
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
checkboxes */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$html_skills = make_checkbox_html($skills, 3, 400, "skills[]");
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
?>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<html>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<body>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<br>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<form name="skills" method="POST" action="insertskills.php">
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
Check off your web development skills:
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<? echo "$html_skills"; ?>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<br>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<input type="submit" value="Submit">
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
</form>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
</body>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
</html>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
<?php
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
function get_checkbox_labels($table_name) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* make an array */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$arr = array();
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* construct the query */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$query = "SELECT * FROM $table_name";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* execute the query */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$qid = mysql_query($query);
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* each row in the result set will be packaged as
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
an object and put in an array */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
while($row= mysql_fetch_object($qid)) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
array_push($arr, $row);
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
return $arr;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* Prints a nicely formatted table of checkbox choices.
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$arr is an array of objects that contain the choices
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$num is the number of elements wide we display in the table
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$width is the value of the width parameter to the table tag
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$name is the name of the checkbox array
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$checked is an array of element names that should be checked
*/
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
function make_checkbox_html($arr, $num, $width, $name, $checked) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* create string to hold out html */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str = "";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* make it */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "<table width="$width" border="0">n";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "<tr>n";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* determine if we will have to close add
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
a closing tr tag at the end of our table */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
if (count($arr) % $num != 0) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$closingTR = true;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$i = 1;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
if (isset($checked)) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* if we passed in an array of the checkboxes we want
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
to be displayed as checked */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
foreach ($arr as $ele) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "<td><input type="checkbox" name="$name" value="$ele->id"";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
foreach ($checked as $entry) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
if ($entry == $ele->value) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "checked";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
continue;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= ">";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "$ele->value";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
if ($i % $num == 0) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "</tr>n<tr>";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
} else {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "</td>n";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$i++;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
} else {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* we just want to print the checkboxes. none will have checks */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
foreach ($arr as $ele) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "<td><input type="checkbox" name="$name" value="$ele->id">";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "$ele->value";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
if ($i % $num == 0) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "</tr>n<tr>";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
} else {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "</td>n";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$i++;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
/* tack on a closing tr tag if necessary */
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
if ($closingTR == true) {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "</tr></table>n";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
} else {
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
$str .= "</table>n";
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
return $str;
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
}
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
?>
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
[第一节完]
作者:Dan LaFlamme
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
译者:sharetop([email protected])
ww w.china it power.co4BmzP7VSCJPU6vuQMIctOCDSl
来源: www.phpbuilder.net
php爱好者站 http://www.phpfans.net php动态|php基础|php进阶|php模板.
相关阅读 更多 +