Showing products according to multi-level category selected in PHP,Mysql

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.
show_products_according_to_multi_level_category_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 .

<?php
													
	 function show_cat($p_cid=0,$space=''){
	 	$r2=$mysqli->query=("SELECT * FROM category WHERE p_cid='$p_cid'");
 		
			if($p_cid==0){
			$space='';
		}else{
			 $space .="-----";
		}
		
		
		while($row2=$r2->fetch_assoc())
		{
		
		
		 
		 
		 ?>
		 <li><a href="products.php?action=cat&cid=<?php echo $row2['cid']; ?>" title="Desktops"><span><?php echo $space.$row2['name'] ?></span></a></li>
 		 <?php
		 
		
		 
		 	show_cat($row2['cid'],$space);
		 
		}

	 
	 }
	 
	 
	 
	 show_cat();
	  
	
	 
	 ?>

Above we created a function show_cat having 2 parameter p_cid and space . p_cid to handle parent cide and space to maintain spaces of category depth level as shown above in image .We listed all main category with there sub level category .

Looking to sharp your PHP knowledge ,Visit us www.unitedwebsoft.in for complete advance PHP course .We cover a complete e-commerce project based training that build your confidence to do any web project .We also started Laravel PHP MVC framework training

Step 2

On our products.php page to show list of products according to category click .We have to code below .

 $cid=$_GET['cid'];
								 
								
								//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[]=$row['cid'];
											
											sub_cids($row['cid'],$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()){
							 ?>
							 
							 <div style="width:160px; height:180px; float:left; margin:4px; padding:4px; border:1px solid #CCCCCC;">
			
				 <a href="product_details.php?pid=<?php echo $row['pid']; ?>"><?php	echo $row['title'];	?><br/><br/></a>
							 <a class="example-image-link" href="uploads/<?php echo $row['image']; ?>" data-lightbox="example-1">
								<img width="110" src="uploads/<?php echo $row['image']; ?>" />
								</a>
								<br/>
 								<?php
								if($product->check_in_cart($row['pid'],$mid,$cart_table)==false){
									$link="
								}else{
									$link="#";
								}
								?>
								
								<a href="<?php echo "products.php?action=add_to_cart&pid=".$row['pid']; ?>">
								
								<img width="120"   src="css/images/addtocart-green-42.png" />
								
								</a>
								
								</div>
								
								
								<?php
								
							 }
							} //else 
							 ?>
							 

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

  
<ul>
    <li>Mobile</li>
    <li>-----Android</li>
    <li>----------Samsung</li>
    <li>---------------Galaxy Tab</li>
    <li>-----Windows</li>
    <li>Computer</li>
    <li>-----Desktop</li>
    <li>-----Laptop</li>
  </ul>
  

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 . For advance level PHP training visit www.unitedwebsoft.in

Online Web Development Training,Video Tutorials