Function fill menu dynamically

Asked

Viewed 57 times

0

I have a result that comes after a query in the database. With this result I make the menu dynamically, when the user has permission to view the page. What I would like to know is if you have the option of improving my code, it is working, but I would like to improve it if possible and have less line to compile. This if is only the first, to complete the entire menu have to repeat it 50 times, which are the menu items. I tried to use the "switch", but also without success.

Here I receive the data from the database query

$paginas = $pagina->verificaPaginas();
$qtdRow = count($paginas);

Result of $pages

array(168) { [0]=> array(9) { ["idpagina"]=> string(1) "1" ["url"]=> string(34) "../Acervos/acervoViewCad.class.php" ["descricao"]=> string(9) "CADASTRAR" ["categoria"]=> string(1) "1" ["grupo"]=> string(1) "1" ["acessoid"]=> string(1) "1" ["idlogin"]=> string(1) "1" ["paginaid"]=> string(1) "1" ["visualizar"]=> string(1) "1" } [1]=> array(9) { ["idpagina"]=> string(1) "2" ["url"]=> string(37) "../Acervos/acervoViewEditar.class.php" ["descricao"]=> string(7) "ALTERAR" ["categoria"]=> string(1) "1" ["grupo"]=> string(1) "1" ["acessoid"]=> string(1) "2" ["idlogin"]=> string(1) "1" ["paginaid"]=> string(1) "2" ["visualizar"]=> string(1) "1" } [2]=> array(9) { ["idpagina"]=> string(1) "3" ["url"]=> string(40) "../Categorias/categoriaViewCad.class.php" ["descricao"]=> string(9) "CADASTRAR" ["categoria"]=> string(1) "2" ["grupo"]=> string(1) "1" ["acessoid"]=> string(1) "3" ["idlogin"]=> string(1) "1" ["paginaid"]=> string(1) "3" ["visualizar"]=> string(1) "1" } [3]=> array(9) { ["idpagina"]=> string(1) "4" ["url"]=> string(43) "../Categorias/categoriaViewEditar.class.php" ["descricao"]=> string(7) "ALTERAR" ["categoria"]=> string(1) "2" ["grupo"]=> string(1) "1" ["acessoid"]=> string(1) "4" ["idlogin"]=> string(1) "1" ["paginaid"]=> string(1) "4" ["visualizar"]=> string(1) "1" } [4]=> array(9) { ["idpagina"]=> string(1) "5" ["url"]=> string(43) "../Fornecedores/fornecedorViewCad.class.php" ["descricao"]=> string(9) "CADASTRAR" ["categoria"]=> string(1) "3" ["grupo"]=> string(1) "1" ["acessoid"]=> string(1) "5" ["idlogin"]=> string(1) "1" ["paginaid"]=> string(1) "5" ["visualizar"]=> string(1) "1" } [5]=> array(9) { ["idpagina"]=> string(1) "6" ["url"]=> string(64) "../Fornecedores/insereMatriculaEditaFornecedorViewGrid.class.php" ["descricao"]=> string(12) "ALTERAR POST" ["categoria"]=> string(1) "3" ["grupo"]=> string(1) "1" ["acessoid"]=> string(1) "6" ["idlogin"]=> string(1) "1" ["paginaid"]=> string(1) "6" ["visualizar"]=> string(1) "1" }

This is where I build the menu dynamically

 <nav id="menu">
    <ul class="menu"> <!-- Esse é o 1 nivel ou o nivel principal -->
        <?php for ($a = '0';
                   $a < $qtdRow;
                   $a++) { ?>
        <!-- Inicio menu cadastros -->          
    <?php if (($paginas[$a]['grupo'] == 1))  { ?>
        <li><a href="#">CADASTROS</a>
            <ul class="submenu-1"> <!-- Esse é o 1 nivel ou o primeiro Drop Down -->
                <?php for ($a = '0';
                           $a < $qtdRow;
                           $a++) { ?>
            <?php if (($paginas[$a]['categoria'] == 1)) { ?>
                <li><a href="#">ACERVO</a>
                    <ul class="submenu-2"> <!-- Esse é o 2 nivel ou o primeiro Drop Down -->
                        <?php for ($a = '0'; $a < $qtdRow; $a++) { ?>
                            <?php if (($paginas[$a]['categoria'] == 1) && ($paginas[$a]['grupo'] == 1)) { ?>
                                <li><a href="javascript:void(0)"
                                       onclick='window.open("<?= $paginas[$a]['url']; ?>", "iframe_a")'><?= $paginas[$a]['descricao']; ?></a>
                                </li>
                            <?php } ?>
                        <?php } ?>
                        </li>
                    </ul>
                    <?php } ?>
                    <?php } ?>

                </li>
                </li>
            </ul>
            <?php } ?>
            <!-- Fim menu cadastro-->
            <?php } ?><!-- Fecha contagem -->

The selection of the bank comes this: inserir a descrição da imagem aqui

  • Even with WHILE or SWITCH, the same thing happens.

  • These terms CADASRS and ACQUIS, for example, does not come from the bank? Post the code of $paginas = $pagina->verificaPaginas();, may help in formulating an idea. Perhaps follow the idea of setting up a function for each level and action.

  • Good morning @Gnomoescalate, not these terms do not come from the bank, which comes from the bank is the number that in this case represents the group and the category. You want the result of $pages = $page->checkWebsite();.

  • I added part of the result @Gnomoescalate.

  • I got it, I got it. What you take is that the terms REGISTRATION and COLLECTION, for example, are entered into your code manually, to facilitate the creation of a function they would need to be in a database, array associated with the group or category.

  • Only that not only I have the ACQUIS, I have several others that compose the menu.

Show 1 more comment

1 answer

0

Good night.

Well, for starters you can change the for for foreach and although it did not be a bad practice or worse your code I recommend that save open and close php tags (<?php ?>) to create html code, try using echo in some cases. To illustrate what I mean, I’ll take this piece of code:

<?php for ($a = '0'; $a < $qtdRow; $a++) { ?>
                            <?php if (($paginas[$a]['categoria'] == 1) && ($paginas[$a]['grupo'] == 1)) { ?>
                                <li><a href="javascript:void(0)"
                                       onclick='window.open("<?= $paginas[$a]['url']; ?>", "iframe_a")'><?= $paginas[$a]['descricao']; ?></a>
                                </li>
                            <?php } ?>
                        <?php } ?>

Thus:

<?php
   foreach($paginas as $dados)
   {
      if($dados['categoria'] == 1 && $dados['grupo'] == 1)
      {
         echo "<li><a href='javascript:void(0)' onclick=\"window.open('{$dados['url']}','iframe_a')\">{$dados['descricao']}</a></li>";
      }
   }
?>

Now for the menu question.

Normally the dynamic menus are two levels and for each level a table in the database is created:

Tabela categorias:
id_categoria|nome   |
     1      |Camisas|
     2      |Sapatos|
-----------------------------
Tabela sub_categorias:
id_sub|nome    |id_categoria|
   1  |Cadastro|      1     |
   2  |Alterar |      1     |
   3  |Cadastro|      2     |
   4  |Alterar |      2     |
   5  |Deletar |      2     |
-----------------------------

And to go through the tables creates two methods, one to list all the items in the category table listarCategorias() and another to list sub-categories listarSubCategorias($id_categoria) this would receive the id_category and only list the sub_category items that correspond to this id. The code would be more or less:

$categorias = $pagina->listarCategorias();
foreach($categorias as $dados)
{
   echo "<ul>";
      echo "<li><a href='#'>{$dados['nome']}</a>";
         echo "<ul>";
         $sub = $pagina->listarSubCategorias($dados['id_categoria']);
         foreach($sub as $dados2)
         {
            echo "<li><a>{$dados2['nome']}<a></li>";
         }
         echo "</ul>";
   echo "</ul>";
}

In your case the tables would have more fields, this is just an example, if you want a more specific answer I suggest you give more details about how this menu should work and what each variable means.

PS: Keep in mind that I can’t see the image (because of the computer proxy I’m using to make that response).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.