How to display menu and submenus?

Hi all.
Task: display a menu with submenu that corresponds to it(menu).
Table menu
1ac5b8578f6247bd966bb85017ffe06a.png
Table submenu
c26e919ee486486aa3c1dca7d10d609c.png
Part of the Code with the queries!!
function get_menu() {
 global $pdo;
 $stmt = $pdo->query('SELECT * FROM menu ');
 $menu = $stmt->fetchAll();
 return $menu;
}
get_submenu function() {
 global $pdo;
 $stmt = $pdo->query('SELECT FROM sub_menu sub_menu ');
 $submenu = $stmt->fetchAll();
 return $submenu;
}

HTML
<?php foreach ($menu as $menu) : ?>
 the <li><span><?php echo $menu['name']; ?></span>
 <ul class="sub-menu">
 <?php foreach ($submenu as $submenus) : ?>
 the <li><a href><?php echo $submenus['name']; ?></a></li>
 <?endforeach; ?>
</ul>
</li>
 <?endforeach; ?>

Question! How do I output the data to a "Main menu" in the submenu was "Cold, Hot, Side dishes", and "Children's menu" - "ice cream, Desserts?
August 19th 19 at 22:55
2 answers
August 19th 19 at 22:57
Can just add a condition in the investment cycle:
<?php foreach ($submenu as $submenus) : ?>
 <?php if($submenus['id_menu'] != $menu['id']) continue; ?>
 the <li><a href>
 <?php echo $submenus['name']; ?>
</a></li>
<?php endforeach; ?>


In General, it is better in one table to store. Just add a parent_id field. You're not going to create a third table if you have added the nesting level..
And in which table to add the parent_id field ? In the submenu ? Then as I name the menu out?(Main, Child, etc."?
2nd nesting most likely will not. - Sigrid.Mosciski commented on August 19th 19 at 23:00
August 19th 19 at 22:59
Your code:
<?php
 $menu = get_menu();
 $submenu = get_submenu();
?>
<?php foreach ($menu as $item) : ?>
 the <li><span><?php echo $item['name']; ?></span>
 <ul class="sub-menu">
 <?php foreach ($submenu as $submenu_item) : ?>
 <?php if($submenu_item['id_menu'] == $item['id']): ?>
 the <li><a href><?php echo $submenu_item['name']; ?></a></li>
 <?php endif; ?>
 <?endforeach; ?>
</ul>
</li>
<?endforeach; ?>


In the submenu table you have a column id_menu, this number corresponds to the id from the table menu. See if they match, the system displays only the desired items submenu.
What would you say tables? Whether they are designed for such a task? And how to make it so that if there is nesting, it will be if no nesting, then instead of span will be - Sigrid.Mosciski commented on August 19th 19 at 23:02
instead of the span tag will be a - Sigrid.Mosciski commented on August 19th 19 at 23:05

Find more questions by tags HTMLPHP