PHP VAØ DATABASE
9-4
COMPUTER LEARNING CENTER
WWW.HUUKHANG.COM
function optionselected($stSQL,$item,$links)
{
$results
=
mysql_query($stSQL,
$links);
$totalRows=mysql_num_rows($results);
$strOption="<option value=\"\" selected>";
$strOption
.="--Select--</option>";
if($totalRows>0)
{
while ($row = mysql_fetch_array ($results))
{
$strOption .="<option value=\"" ;
$strOption .=$row["ID"]."\"";
if($row["ID"]==$item)
$strOption .=" selected ";
$strOption .= ">".$row["Name"];
$strOption .="</option>";
}
}
return
$strOption;
}
Sau ñoù, goïi phöông thöùc naøy trong trang lietketheonhom.php nhö ví duï 10-3.
<?php
require("dbcon.php");
require("database.php");
$id="";
if
(isset($cateid))
$id=$cateid;
$stSQL ="select CateID As ID, CateName as Name from tblCategories ";
$result = mysql_query($stSQL, $link);
$totalRows=mysql_num_rows($result);
$strOption=optionselected($stSQL,$id,$link);
?>
<form name=frmMain method=post>
<tr>
<td align=left colspan=4>
Category:
<select
name=cateid
onchange="document.frmMain.submit();">
<?=$strOption?>
</select></td>
<td
align=right> </td>
</tr>
</form>
Laàn ñaàu tieân baïn coù theå choïn maëc ñònh moät nhoùm hoaëc lieät keâ taát caû, khi ngöôøi söû duïng choïn nhoùm
saûn phaåm naøo ñoù thì trang lietketheonhom.php seõ lieät keâ danh saùch loaïi saùch cuûa nhoùm saùch ñoù.
Ñeå laøm ñieàu naøy, baïn khai baùo theû form vôùi theû select nhö ví duï 10-4.
<form name=frmMain method=post>
<tr>
<td align=left colspan=4>
Category:
<select
name=cateid
onchange="document.frmMain.submit();">
<?=$strOption?>
</select></td>
<td
align=right> </td>
</tr>
</form>
Khi ngöôøi söû duïng choïn caùc maåu tin nhö hình 10-2 vaø nhaán nuùt Delete, döïa vaøo giaù trò cuûa nuùt coù
teân action (trong tröôøng hôïp naøy laø Delete), baïn coù theå khai baùo bieán ñeå laáy giaù trò choïn baèng caùch
khai baùo nhö ví duï 10-5.
$strid=$chon;