2014-02-19 108 views
0

我有一个包含选择类别下拉列表的表单。当用户添加一个新类别时,他应该从所有主类别中选择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>'; 
     } 
    } 
    } 

} 

}

+0

你的PHP代码是错误的。仔细检查你的大括号 –

+0

是否正确!现在是好的,jst复制粘贴错误,对不起 – thecore7

+0

另外,'$ c'未定义 –

回答

0

您可以添加其他enrty添加父类别到其子类别。

id || bg_category || parent ==> and for example I have:  
1 || Jewellery || 0  
2 || Rings  || 1  
3 || Ear rings || 2  
4 || Bracelets || 0 
5 || Jewellery || 2 

在上述情况下,我试图添加下Rings类别Jewellery类别。然而,新的Jewellery类别有不同的ID

如果你提供你的INSERT语句,我就可以在查询帮助过

+0

感谢您的帮助,但我解决了管理问题,这里是解决方案更新,谢谢 – thecore7

0

工作职能:

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

} 

}