PHP中在數(shù)據(jù)庫中保存Checkbox數(shù)據(jù)(2)
更新時(shí)間:2006年10月09日 00:00:00 作者:
這代碼是非常簡(jiǎn)單的,你很快地就看完了吧。主要的工作有兩個(gè)函數(shù)完成:"get_checkbox_labels" 和 "make_checkbox_html"。其中 "get_checkbox_labels" 查詢表const_skills 并且返回一個(gè)對(duì)象數(shù)組,每一個(gè)對(duì)象有一個(gè)id值和相應(yīng)的技能名稱。我們傳送這個(gè)數(shù)組和其它一些參數(shù)給"make_checkbox_html" ,這個(gè)函數(shù)將返回一個(gè)字串,用來生成checkbox的html代碼?,F(xiàn)在我們把這個(gè)字串插入html文件來生成我們需要的包含有各種技能選擇的表單。注意我并沒有傳送變量 $checked 給"make_checkbox_html",這個(gè)參數(shù)是一個(gè)我們要顯示的checked的對(duì)象數(shù)組。如果一個(gè)用戶學(xué)會(huì)了一項(xiàng)新的技能,我們可以提供一個(gè)“編輯技能“頁,顯示的checkbox框中保存的用戶的技能項(xiàng)應(yīng)是被預(yù)先 checked。
用這種方法來動(dòng)態(tài)創(chuàng)建一個(gè)表單相對(duì)于用一個(gè)固定的html代碼來生成技能checkbox的好處在哪?嗯,或許我們?cè)试S求職者選擇一個(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)中,用戶可以注冊(cè),然后能創(chuàng)建/編輯他們的簡(jiǎn)介。你可能要用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);
}
?>
很簡(jiǎn)單吧。現(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ù)摰?。我希望這有助于你用它們來工作,創(chuàng)建一個(gè)數(shù)據(jù)驅(qū)動(dòng)的網(wǎng)站。
您可能感興趣的文章:
- PHP簡(jiǎn)單獲取多個(gè)checkbox值的方法
- php一次性刪除前臺(tái)checkbox多選內(nèi)容的方法
- js限制checkbox勾選的個(gè)數(shù)以及php獲取多個(gè)checkbbox的方法深入解析
- php checkbox 取值詳細(xì)說明
- php select,radio和checkbox默認(rèn)選擇的實(shí)現(xiàn)方法
- php checkbox復(fù)選框值的獲取與checkbox默認(rèn)值輸出方法
- php 用checkbox一次性刪除多條記錄的方法
- PHP中在數(shù)據(jù)庫中保存Checkbox數(shù)據(jù)(1)
- PHP中CheckBox多選框上傳失敗的代碼寫法
相關(guān)文章
如何在PHP中使用Oracle數(shù)據(jù)庫(6)
如何在PHP中使用Oracle數(shù)據(jù)庫(6)...2006-10-10基于數(shù)據(jù)庫的在線人數(shù),日訪問量等統(tǒng)計(jì)
基于數(shù)據(jù)庫的在線人數(shù),日訪問量等統(tǒng)計(jì)...2006-10-10php面向?qū)ο笕ヂ?(十) final static const關(guān)鍵字的使用
這個(gè)關(guān)鍵字只能用來定義類和定義方法,不能使用final 這個(gè)關(guān)鍵字來定義成員屬性,因?yàn)閒inal 是常量的意思,我們?cè)赑HP 里定義常量使用的是define()函數(shù),所以不能使用final 來定義成員屬性。2009-09-09