Menu with PHP and Mysql

Asked

Viewed 78 times

-2

I created a navigation system for my site. See:

<nav class="navegacao" id="navegacao">
  <ul class="menus">
    <li><a href="#">Categoria 1</a></li>
    <li><a href="#">Categoria 2</a>
      <ul class="submenus">
        <li><a href="#">Sub-categoria 1</a></li>
        <li><a href="#">Sub-categoria 2</a></li>
        <li><a href="#">Sub-categoria 3</a>
          <ul class="submenus">
            <li><a href="#">Sub-categoria 1-1</a></li>
            <li><a href="#">Sub-categoria 1-2</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="#">Categoria 3</a></li>
  </ul>
</nav>

I left this Dynamic navigation system creating a PHP function called list_categories() that generates navigation using Mysql. See:

inserir a descrição da imagem aqui

Worked!!

There is only one problem that I am not able to solve, this being added the excerpt <ul class="submenu"></ul> even though there’s nothing to list inside it.

Can someone help me solve?

From now on, thank you!

1 answer

-1

On line 18 of your code, you are typing "ul" before you know if there are any sub-categories. This is a logic problem.

This is common because you are not separating business logic from visualization, ideally you assemble the entire category structure in memory (in a p. array e.g. array) and write on screen in another function.

Study about MVC and various other problems q vc can have with this mixture of skills tbm will be solved.

Note: In the next question do not post the print of your code, but your code directly using the site formatting functions.

  • Thanks!! I’m still starting to post things here, so I’m not used to it yet. But next time I’ll do it yes! Ahhh, and on the Array, I made a version, but I don’t know why it only generates 1 level. I’ll see if it puts here for you to see

Browser other questions tagged

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