专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »PHP教程 » 保存图片数据库:PHP中在数据库中保存Checkbox数据(2) »正文

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

来源: 发布时间:星期四, 2009年2月12日 浏览:148次 评论:0


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

  这代码是非常简单你很快地就看完了吧主要工作有两个完成:\"get_checkbox_labels\" 和 \"make_checkbox_html\"其中 \"get_checkbox_labels\" 查询表const_skills 并且返回个对象个对象有个id值和相应技能名称我们传送这个和其它些参数给\"make_checkbox_html\" 这个将返回个字串用来生成checkboxhtml代码现在我们把这个字串插入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 .gif' /> 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 */
substr($q, 0, -1);
}

?>

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


搜索

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


/* builds a query to search for the skills
checked off in the $skills .gif' /> */

function skill_search($skills) {
(!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 .= \";\";
$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是个优秀表单元素正如本文所谈论我希望这有助于你用它们来工作创建个数据驱动网站WebSite

[第 2节完]
----------------------------

作者:Dan LaFlamme
译者:sharetop([email protected])


0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: