On this previous tutorial we have discussed regarding how to create multi-level category using recursive function in PHP,Mysql. Now have to show products according to multi-level category selected in PHP,Mysql. 
Step 1
First of all let's do the below code in left.php to acheive multi-level category on left side as shown on above image .
; //to get all sub cids function sub_cids($cid,$cids=0){ global $cids; $r=$mysqli->query("SELECT * FROM category WHERE p_cid='$cid' "); if( $r->num_rows >0){ while($row=$r->fetch_assoc()){ $cids; sub_cids($row,$cids); } } return $cids; } $cids=sub_cids($cid); $cids=$cid; $cids_str=implode(",",$cids); $q="SELECT * FROM products WHERE category IN ($cids_str) "; break; } $r=$mysqli->query($q); $count=$r->num_rows; if($count==0){ echo "Sorry ! No result under this category"; }else{ while($row=$r->fetch_assoc()){ ?>
Above we created a new function i.e sub_cids .Here we have $cid variable on this page in get mode means reading it from url that passed from 'left.php' after category click . So here on this function we finds all sub level category's id so that we can show product according it's level . For demonstration consider We have category like
- Mobile
- -----Android
- ----------Samsung
- ---------------Galaxy Tab
- -----Windows
- Computer
- -----Desktop
- -----Laptop
So, for this condition if we have 2 product one under 'Galaxy Tab' and other under 'Windows' category . Now if someone click on mobile, it will show 2 products . If someone clicks any among android ,samsung or galaxy tab ,it will show only 1 product. Similarly if someone click on 'windows' category ,it will show other 1 product . To use this tutorial in your project you should have at least 2 tables 'category' and 'products'. Download this database tables If you have any query ,you can ask me through below comment .



