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.

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 .

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


php mysql video tutorial in Hindi language by UnitedWebSoft

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

								//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){
									return $cids;
								   $q="SELECT * FROM products WHERE category IN ($cids_str)  ";
							 	echo "Sorry ! No result under this category";
							 <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 href="<?php echo "products.php?action=add_to_cart&pid=".$row['pid']; ?>">
								<img width="120"   src="css/images/addtocart-green-42.png" />
							} //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

    <li>---------------Galaxy Tab</li>

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