0
votes

I have a problem with categories and subcategories. I cant get subcategory data in same category_id as category. How I define subcategory category_id is same as category category_id and subcategory parent is 2.

Database is here

<?php 
//connect to Mysql database
include "storescripts/connect_to_mysql.php"; 
// This block grabs the whole list for viewing
$dynamic_menu = "";
$sql = mysql_query("SELECT * FROM `category` WHERE `parent` IS NULL ORDER BY `category`.`name` ASC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0)
	{
	while($row = mysql_fetch_array($sql)){
			 $id = $row["id"];
			 $name = $row["name"];
			 $parent = $row["parent"];
			 $category_id = $row["category_id"];
			 $dynamic_menu .= '<div class="menu-primary">
                <li><a>'.$name.'</a></li></div>
				<div class="menu-under">
            <li><a>'.$name.'</a></li>
        </div>';

}} else {
	$dynamic_menu = "You have no products listed in our store yet";
}
?>
<div class="menu">
    <?php echo $dynamic_menu; ?>
</div>


<!--

I'd like to html created like below 

<div class="menu-primary">
        	<li><a>Kaamerad</a></li>
        </div>
        <div class="menu-under">
        	<ul>
            <li><a>optikad</a></li>
            <li><a>mälukaardid</a></li>
            </ul>
        </div>
-->
2
Want to see your DB ?Naveed Ramzan

2 Answers

0
votes

Inside the While Loop, you must include another query that fetches subcategories for a particular category (after getting it from id field), that is where parent is a certain category Id.

0
votes

I think that firstly you should change db entries. For example Optical parent should be 5(pk of kaamerad entry) etc.

Then you should write a recursive function like PHP Recursive menu function.

I hope it would help!