国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP中在數(shù)據(jù)庫中保存Checkbox數(shù)據(jù)

2024-05-04 23:05:04
字體:
供稿:網(wǎng)友
介紹

  checkbox是一個(gè)非常有用的頁面表單項(xiàng),在讓用戶進(jìn)行多重選擇的情況下,它甚至可以允許用戶選擇全部項(xiàng)目或是一個(gè)都不選。但是,盡管這是一個(gè)非常優(yōu)秀的表單元素,但在我們的工作中,在如何正確地保存選擇項(xiàng)這方面總存在一些易混淆的情況發(fā)生。本文將描述在遵循好的數(shù)據(jù)庫設(shè)計(jì)原則的方法下,如何把checkbox選擇項(xiàng)正確地保存在數(shù)據(jù)庫中。

要求

  本文將闡述如何把選擇項(xiàng)正確地保存在用戶數(shù)據(jù)庫中的方法。盡管這里包括了有用的php代碼,但我將從數(shù)據(jù)庫設(shè)計(jì)的觀點(diǎn)來表達(dá)它們,所以,你可以很方便地使用任何一個(gè)數(shù)據(jù)庫和服務(wù)器端腳本語言來實(shí)現(xiàn)。我只是想提供一個(gè)如何做的方法,讓你能應(yīng)用于你自己的站點(diǎn)中。如果你想運(yùn)行這里的源碼,你需要安裝php、mysql和網(wǎng)絡(luò)服務(wù)器。

例1:招聘站點(diǎn)

  假如你被要求做一個(gè)招聘類的網(wǎng)站,允許求職的軟件開發(fā)人員填寫他們的技能,讓雇主能訪問這個(gè)站點(diǎn)并根據(jù)求職者的技能找到合適的員工。你也知道,一個(gè)開發(fā)人員擁有的技能會(huì)多于一個(gè),因此你決定這樣設(shè)計(jì)你的站點(diǎn)。

  每一個(gè)求職者將允許訪問本站,注冊一個(gè)用戶,并且輸入他的技能,checkbox就派上用場了,你可能想作這樣的一頁:

__ php __ mysql __ zope
__ perl __ javascript __ jsp

[提交]

  每一個(gè)求職都可以選擇他所擁有的技能。顯然對于不同人來說這選擇項(xiàng)是不同的。一個(gè)人可能會(huì)是php和mysql,其它人可能只是jsp。你將如何保存這些選擇呢?一個(gè)很自然的想法是針對每個(gè)選項(xiàng)建一個(gè)字段,這樣開始可以正常工作。但是隨后你可能會(huì)發(fā)現(xiàn),當(dāng)你想擴(kuò)展或調(diào)整時(shí),麻煩就來了,你可能不得不修改你的表結(jié)構(gòu)。
好的方法應(yīng)是這樣的:

  你應(yīng)有一個(gè)用戶表包含用戶的注冊信息,如用戶名、密碼和其它一些你需要的什么內(nèi)容。假如你直接使用本文后面給出的源碼,你要建一個(gè)簡單的表如下:

id username
1 user1
2 user2
3 user3

我們先建一個(gè)表 "const_skills" 用如下的 sql 語句:

sql> create table const_skills (
id int not null primary key,
value varchar(20) );

現(xiàn)在我們加入技能:

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 現(xiàn)在應(yīng)是這樣的:

id value
1 php
2 mysql
3 zope
4 perl
5 javascript
6 jsp

這個(gè)表只是讓用戶可以選擇相應(yīng)的技能,現(xiàn)在,再建一個(gè)表 lookup_skills 用如下的sql:

sql> create table lookup_skills (
id int not null auto_increment primary key,
uid int,
skill_id int );

  這個(gè)表lookup_skills的目的是提供從用戶表到開發(fā)技能表之間的一個(gè)映射關(guān)系。換句話說,它讓我們保存開發(fā)者和他們有的技能,如,當(dāng)求職者完成選擇點(diǎn)擊提交時(shí),我們將填寫這個(gè)表用checkbox中被選定的那些值。對于每一個(gè)選上的技能,我們在這個(gè)表中加一條記錄,記下用戶id及所選項(xiàng)的id。(想必大家都清楚了吧。我譯到這,嘿嘿…)

在我們看這個(gè)插入記錄的代碼之前,我們先設(shè)計(jì)一下這個(gè)頁面,應(yīng)有的內(nèi)容有一個(gè)表單,我們可以查詢的數(shù)據(jù)庫并且取checkbox標(biāo)簽從const_skills表中,建這個(gè)checkbox表單項(xiàng)。

代碼如下:

< ?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;
}

? >
  這代碼是非常簡單的,你很快地就看完了吧。主要的工作有兩個(gè)函數(shù)完成:"get_checkbox_labels" 和 "make_checkbox_html"。其中 "get_checkbox_labels" 查詢表const_skills 并且返回一個(gè)對象數(shù)組,每一個(gè)對象有一個(gè)id值和相應(yīng)的技能名稱。我們傳送這個(gè)數(shù)組和其它一些參數(shù)給"make_checkbox_html" ,這個(gè)函數(shù)將返回一個(gè)字串,用來生成checkbox的html代碼。現(xiàn)在我們把這個(gè)字串插入html文件來生成我們需要的包含有各種技能選擇的表單。注意我并沒有傳送變量 $checked 給"make_checkbox_html",這個(gè)參數(shù)是一個(gè)我們要顯示的checked的對象數(shù)組。如果一個(gè)用戶學(xué)會(huì)了一項(xiàng)新的技能,我們可以提供一個(gè)“編輯技能“頁,顯示的checkbox框中保存的用戶的技能項(xiàng)應(yīng)是被預(yù)先 checked。

  用這種方法來動(dòng)態(tài)創(chuàng)建一個(gè)表單相對于用一個(gè)固定的html代碼來生成技能checkbox的好處在哪?嗯,或許我們允許求職者選擇一個(gè)在我們的表const_skills中原先沒有的項(xiàng)目,如dhtml,這樣,我們可以將它插入表const_skills中,然后,求職者來訪問我們的站點(diǎn),就會(huì)發(fā)現(xiàn)多了一個(gè)dhtml選項(xiàng)。這一切無需調(diào)整html文件。

  插入 lookup_skills

  現(xiàn)在我們已經(jīng)創(chuàng)建了這個(gè)表單,下面我們需要保存這個(gè)用戶所選的技能。在make_checkbox_html函數(shù)中,我們用skill[]調(diào)用每一個(gè)選擇項(xiàng)元素,意味著我們可以以數(shù)組元素的形式訪問每個(gè)選擇項(xiàng)。這樣我們可以插入把這個(gè)選擇插入表lookup_skill中。如果用戶選中5個(gè)選項(xiàng),我們就在lookup_skill中插入5條記錄。記住在表lookup_skills中每一條記錄只有兩個(gè)字段用戶id和技能id。在我的這個(gè)例子站點(diǎn)中,用戶可以注冊,然后能創(chuàng)建/編輯他們的簡介。你可能要用session來保存userid,當(dāng)他們登錄后。但如何管理userid超過了本文的范圍。

  下面的代碼,我們假定我們可能訪問這個(gè)userid用這個(gè)變量名$uid,下面就是插入記錄的函數(shù)代碼:


/* 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);
}

?>

  很簡單吧。現(xiàn)在你知道如何從表const_skill讀記錄來動(dòng)態(tài)創(chuàng)建一個(gè)表單,也知道如何保存用戶選擇的技能到表lookup_skills中。下面我們要做什么?讓我們看一下搜索吧
搜索

  當(dāng)一個(gè)雇主來找一個(gè)網(wǎng)絡(luò)開發(fā)人員時(shí),他來到你的搜索頁面,你可以顯示同樣的一個(gè)表單并且允許他選擇他想要雇員擁有的技能。你取到了他選中的技能的數(shù)組,然后你可以遍歷這個(gè)數(shù)組,用一個(gè)sql語句找出擁有此技能的求職者,你可以顯示這個(gè)列表或結(jié)果,并允許搜索者點(diǎn)一個(gè)項(xiàng)目顯示它的詳細(xì)信息。下面的這個(gè)函數(shù)描述了如何創(chuàng)建這個(gè)查詢語句:


/* 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;
}
}

?>

  如果執(zhí)行了搜索 php 和 javascript ,這個(gè)函數(shù)返回這個(gè)語句:

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;

  這個(gè)函數(shù)將返回你所選擇的項(xiàng)目的邏輯與,這就是說,如果我們選了php 和javascript 兩項(xiàng),只會(huì)返回*同時(shí)*擁有php 和 javascript兩種技能的求職者的username。如果你想要找擁有其中任一個(gè)技能的求職者,你可以用 php *or* javascript ,如果你想顯示相同的記錄,你可以去掉最后的"group by..." 子句。


  總結(jié)

  好了,就是這樣。checkboxes是一個(gè)優(yōu)秀的表單元素,正如本文所談?wù)摰摹N蚁M@有助于你用它們來工作,創(chuàng)建一個(gè)數(shù)據(jù)驅(qū)動(dòng)的網(wǎng)站。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 龙游县| 留坝县| 凉城县| 蓬莱市| 成武县| 盱眙县| 揭东县| 桐城市| 建瓯市| 永修县| 蒙城县| 阜南县| 察隅县| 朝阳区| 南部县| 萝北县| 九龙城区| 乌鲁木齐县| 澄迈县| 高邮市| 象山县| 平湖市| 长阳| 凌源市| 洪泽县| 天津市| 抚州市| 宜君县| 阜康市| 弥渡县| 石嘴山市| 宿松县| 格尔木市| 望谟县| 东平县| 贵南县| 陇西县| 乐亭县| 同江市| 皋兰县| 台东市|