【发布时间】:2014-07-02 10:45:00
【问题描述】:
我正在尝试使用数据库表中的类别和子类别动态填充下拉菜单。 当我测试下面的代码时,我只得到父亲类别:
数据库:
id_cat //category id
nom_cat //category name
pere // category father
HTML:
<div id='cssmenu'>
<ul>
<?php
$sql="select * from categories where (pere=0) ";
$result=mysql_query($sql,$conn);
while($rows = mysql_fetch_array($result))
{
$sql1="select * from categories where (pere=$rows[0]) ";
$result1=mysql_query($sql,$conn);
$nc =mysql_num_rows($result1);
if($nc>0){$ctype='has-sub';}else{$ctype='';}
echo"<li class='".$ctype."'><a href='#'><span>".$rows[1]."</span></a></li>";
if ($nc>0){
echo"<ul>";
while($rows1 = mysql_fetch_array($result1))
{
echo"<li><a href='categories.php?cat=$rows1[0]'><span>$rows1[1]</span></a></li>";
}
echo"</ul>";
}
}
echo"</ul>";
?>
</div>
【问题讨论】:
-
不要使用 mysql_* 函数。使用 mysqli_*。也用这个
$result1=mysql_query($sql1,$conn);替换这个$result1=mysql_query($sql,$conn);
标签: php database printing categories