Menu, NAV Bootstrap dynamic submenus with PHP PDO Mysql

2

Good evening.

I need to put together a dynamic, three-table menu that needs to be interconnected, but I'm lost. Here's a part of the organization chart below:

Atthemoment,Ihavethefollowingcode:

<liclass="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Produtos<span class="caret"></span></a>
    <ul class="dropdown-menu">

    <li class="dropdown dropdown-submenu"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Audiologia</a>
    <ul class="dropdown-menu">

      <?php

      require 'conexao.php';

      $consulta = $PDO->query("SELECT * FROM subcategoria WHERE categoria_id = '1' ORDER BY ID;");

      while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) { ?>

      <li><a href="produto.php?id=<?php echo "$linha[id]"; ?>"><?php echo "$linha[titulo]"; ?></a></li>

      <?php } ?>

      <ul class="dropdown-menu">

        <?php

        require 'conexao.php';

        $consulta = $PDO->query("SELECT * FROM produto WHERE categoria_id = '1' AND subcategoria_id = '1' ORDER BY ID;");

        while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) { ?>

        <li><a href="produto.php?id=<?php echo "$linha[id]"; ?>"><?php echo "$linha[titulo]"; ?></a></li>

        <?php } ?>

      </ul>

    </ul>
</li>

In my database, I have the tables:

CATEGORIES = with the main categories, ID and CATEGORY.

SUBCATEGORIES = with the ID, CATEGORY_ID and SUBCATEGORY fields.

PRODUCT = with fields ID, CATEGORY_ID, SUBCATEGORIA_ID and TITLE.

I can, from this code, have the category and the sub category, but not the product. I do not think this is the right way to do it, I would like instructions.

    
asked by anonymous 26.09.2017 / 03:46

1 answer

0

So the code gets smaller and easier to see, even though my code is not 100% the way you need it, so adjusting it gets simpler this way.

In the second query I put it to get the id of the first query and in case you need to use another foreach for the categories I have separated the variable $ id_category to be simpler to replace.

<?php
require 'conexao.php';
$id_categoria = 1;
$consulta = $PDO->query("SELECT * FROM subcategoria WHERE categoria_id = '".$id_categoria."' ORDER BY ID;");
while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) {
    $categoria[$linha['id']]['titulo'] = $linha['titulo'];
    $subconsulta = $PDO->query("SELECT * FROM produto WHERE categoria_id = '".$id_categoria."' AND subcategoria_id = '" . $linha['id'] . "' ORDER BY ID;");
    while ($sublinha = $subconsulta->fetch(PDO::FETCH_ASSOC)) {
        $categoria[$linha['id']]['subitens'] = array($sublinha['id'], $sublinha['titulo']);
    }
}
?>

<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Produtos<span class="caret"></span></a>
    <ul class="dropdown-menu">
    <li class="dropdown dropdown-submenu"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Audiologia</a>
    <ul class="dropdown-menu">
        <?php foreach($categoria as $indice => $item): ?>
        <li><a href="produto.php?id=<?php echo $indice; ?>"><?php echo $item['titulo']; ?></a></li>
        <ul class="dropdown-menu">
            <?php foreach($item['subitens'] as $subindice => $subitem): ?>
            <li><a href="produto.php?id=<?php echo $subindice; ?>"><?php echo $subitem; ?></a></li>
            <?php endforeach; ?>
        </ul>
        <?php endforeach; ?>
    </ul>
</li>
    
26.09.2017 / 04:17