我有一个包含选择类别下拉列表的表单。当用户添加一个新类别时,他应该从所有主类别中选择parent =“0”或从他们的子类别中选择。 我的数据库结构为:PHP产品类别选择下拉菜单,允许用户将类别定义为主类别或子类别
id || bg_category || parent ==> and for example I have:
1 || Jewellery || 0
2 || Rings || 1
3 || Ear rings || 2
4 || Bracelets || 0
的条件是一个父类不能被添加为子类别到其子类别或本身。 到目前为止,我有这个功能:
public function getMasterCategories($cid = 0) {
echo '<option value="0" selected="true">No category</option>';
if (empty($cc))
$cc = 9999999999;
$que = 'select id, bg_category,parent from products_categories group by id order by bg_category';
$res = mysql_query($que) or die('Mysql Error:' . mysql_error() . '<br /> Query:' . $que);
$num_rows = mysql_num_rows($res);
for ($i = 0; $i < $num_rows; $i++) {
$rw = mysql_fetch_row($res);
if ($c == $rw[0])
echo '<option value="', $rw[0], '" selected="true">', $rw[1], '</option>';
else
echo '<option value="', $rw[0], '">', $rw[1], '</option>';
}
}
现在这个函数返回所有类别和节目可用性,使孩子类别是父母亲类别。
工作职能:
function getMasterCategories($c=0,$cc=0){
echo '<option value="0" selected="true">Няма</option>';
if(empty($cc)) $cc=9999999999;
$que='select id, bg_category,parent from products_categories where id="'.$cc.'"';
$res=mysql_query($que) or die('Mysql Error:'.mysql_error().'<br /> Query:'.$que);
$num_rows=mysql_num_rows($res);
$rw=mysql_fetch_row($res);
if($rw[0]>0){
$querys='select id,bg_category,parent from products_categories where parent="'.$cc.'" order by bg_category';
$results=mysql_query($querys) or die('Mysql Error:'.mysql_error().'<br /> Query:'.$querys);
$num_rs=mysql_num_rows($results);
if($num_rs=="0"){
$query='select id,bg_category,parent from products_categories where id!="'.$cc.'" order by bg_category';
$result=mysql_query($query) or die('Mysql Error:'.mysql_error().'<br /> Query:'.$query);
$num_r=mysql_num_rows($result);
for($i=0;$i<$num_r;$i++){
$row=mysql_fetch_row($result);
$q='select id,bg_category,parent from products_categories where parent="'.$cc.'" and id="'.$row[0].'" order by bg_category';
$r=mysql_query($q) or die('Mysql Error:'.mysql_error().'<br /> Query:'.$q);
$ro=mysql_fetch_row($r);
if(empty($ro[0])){
if($c==$row[0]) echo '<option value="',$row[0],'" selected="true">',$row[1],'</option>';
else echo '<option value="',$row[0],'">',$row[1],'</option>';
}
}
}
}
}
你的PHP代码是错误的。仔细检查你的大括号 –
是否正确!现在是好的,jst复制粘贴错误,对不起 – thecore7
另外,'$ c'未定义 –