I have a mysql table for categories and subcategories to order in a dropdown menu list. I thought to use a while statement into while, but i get only the first main category with relative subcategories and then the loop stop after the first record.
How i want to display it:
-1 main cat
-1.1 sub-cat
-1.2 sub-cat ...and so on
-2 main cat
- 2.1 sub-cat
- 2.2 sub-cat...and so on
this is the simple test page:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<ul>
<?php
$main_cat = "SELECT *
FROM category
WHERE cat_parent_id = :value
AND cat_lang = 'gb'
ORDER BY cat_id ASC";
$stmt = $con->prepare($main_cat);
$stmt->bindValue(':value', 0, PDO::PARAM_STR);
$stmt->execute();
while ($row_main = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row_main);
?>
<li><?php echo $cat_name; ?></li>
<li>
<ul>
<?php
$sub_cat = "SELECT *
FROM category
WHERE cat_parent_id = ?
ORDER BY cat_id ASC";
$stmt = $con->prepare($sub_cat);
$stmt->bindParam(1, $cat_id);
$stmt->execute();
while ($row_sub = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row_sub);
?>
<li><?php echo $cat_name; ?></li>
<?php }
}
?>
</ul>
</li>
</ul>
</body>
</html>