PHP中在数据库中保存Checkbox数据(2)



这代码是非常简单的,你很快地就看完了吧。主要的工作有两个函数完成:"get_checkbox_labels" 和 "make_checkbox_html"。其中 "get_checkbox_labels" 查询表const_skills 并且返回一个对象数组,每一个对象有一个id值和相应的技能名称。我们传送这个数组和其它一些参数给"make_checkbox_html" ,这个函数将返回一个字串,用来生成checkbox的html代码。现在我们把这个字串插入html文件来生成我们需要的包含有各种技能选择的表单。注意我并没有传送变量 $checked 给"make_checkbox_html",这个参数是一个我们要显示的checked的对象数组。如果一个用户学会了一项新的技能,我们可以提供一个“编辑技能“页,显示的checkbox框中保存的用户的技能项应是被预先 checked。

用这种方法来动态创建一个表单相对于用一个固定的html代码来生成技能checkbox的好处在哪?嗯,或许我们允许求职者选择一个在我们的表const_skills中原先没有的项目,如DHTML,这样,我们可以将它插入表const_skills中,然后,求职者来访问我们的站点,就会发现多了一个DHTML选项。这一切无需调整html文件。

插入 lookup_skills

现在我们已经创建了这个表单,下面我们需要保存这个用户所选的技能。在make_checkbox_html函数中,我们用skill[]调用每一个选择项元素,意味着我们可以以数组元素的形式访问每个选择项。这样我们可以插入把这个选择插入表lookup_skill中。如果用户选中5个选项,我们就在lookup_skill中插入5条记录。记住在表lookup_skills中每一条记录只有两个字段用户id和技能id。在我的这个例子站点中,用户可以注册,然后能创建/编辑他们的简介。你可能要用session来保存userid,当他们登录后。但如何管理userid超过了本文的范围。

下面的代码,我们假定我们可能访问这个userid用这个变量名$uid,下面就是插入记录的函数代码:


/* the function we call to insert.
the $skills argument is the skills array that
is sent to the script when the user hits the submit button
*/
function insert_skills($uid, $skills) {

/* first, we'll delete any entries this user already has
in the table */
purge_lookup("lookup_skills", $uid);

/* now create the sql insert query */
$query = create_checkbox_query($skills, "lookup_skills", $uid);

/* execute the query */
mysql_query($query);
}

/* helper function for insert_skills().
removes all rows in $table with $uid */
function purge_lookup($table, $uid) {
$q = "DELETE FROM $table, WHERE uid = '$uid'";
mysql_query($q);
}

/* helper function for insert_skills().
generates the sctual SQL query */
function create_checkbox_query($arr, $table, $uid) {
$q = "INSERT INTO $table (uid, skill_id) VALUES";

foreach ($arr as $check) {
$q .= " ( $uid , $check )" . ",";
}

/* remove the last comma and return */
return substr($q, 0, -1);
}

?>

很简单吧。现在你知道如何从表const_skill读记录来动态创建一个表单,也知道如何保存用户选择的技能到表lookup_skills中。下面我们要做什么?让我们看一下搜索吧
搜索

当一个雇主来找一个网络开发人员时,他来到你的搜索页面,你可以显示同样的一个表单并且允许他选择他想要雇员拥有的技能。你取到了他选中的技能的数组,然后你可以遍历这个数组,用一个SQL语句找出拥有此技能的求职者,你可以显示这个列表或结果,并允许搜索者点一个项目显示它的详细信息。下面的这个函数描述了如何创建这个查询语句:


/* builds a query to search for the skills
checked off in the $skills array */

function skill_search($skills) {
if (!empty($skills)) {
$query = "SELECT DISTINCT user.username
FROM user, const_skills, lookup_skills
WHERE lookup_skills.uid = user.id
AND lookup_skills.skill_id = const_skills.id ";

$query .= " AND (";
foreach ($skills as $check) {
$query .= " const_skills.id = $check OR";
}

/* remove the final OR */
$query = substr($query, 0, -2);
$query .= ")";

$count = count($skills);
$query .= " GROUP BY user.username HAVING count(user.username) >= $count";

$query .= ";";
return $query;
}
}

?>

如果执行了搜索 PHP 和 Javascript ,这个函数返回这个语句:

SELECT DISTINCT user.username FROM user, const_skills, lookup_skills WHERE lookup_skills.uid = user.id AND lookup_skills.skill_id = const_skills.id AND ( const_skills.id = 3 OR const_skills.id = 5 ) GROUP BY user.username HAVING count(user.username) >= 2;

这个函数将返回你所选择的项目的逻辑与,这就是说,如果我们选了PHP 和Javascript 两项,只会返回*同时*拥有PHP 和 Javascript两种技能的求职者的username。如果你想要找拥有其中任一个技能的求职者,你可以用 PHP *OR* Javascript ,如果你想显示相同的记录,你可以去掉最后的"GROUP BY..." 子句。


总结

好了,就是这样。checkboxes是一个优秀的表单元素,正如本文所谈论的。我希望这有助于你用它们来工作,创建一个数据驱动的网站。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3