I have a form that contains select dropdown list of categories. When user adds a new category he is supposed to choose from all main categories with parent="0" or from their child categories. My database structure is:
id || bg_category || parent ==> and for example I have:
1 || Jewellery || 0
2 || Rings || 1
3 || Ear rings || 2
4 || Bracelets || 0
The condition is that a parent category can not be added as child category to its child category or to itself. So far I have this function:
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>';
}
}
Now this function returns all categories and show availability to make a child category to be parent to its mother category.
WORKING FUNCTION:
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>';
}
}
}
}
}
You can add another enrty for adding parent category to its child category.
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
In above case, I tried to add Jewellery
category under Rings
category. However, new Jewellery
category has different id
If you provide your insert statement, I can help on that query too
Working function :
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>';
}
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.