Codeigniter - Retrieve bank categories

Asked

Viewed 56 times

0

I have a table ''category'' in the database and it has the column 'id' and 'title', I am recovering the same in several menu’s dropdown, but loads all categories of the database but I would like to load each category in its respective menu, someone could help me?

From now on, thank you very much!

inserir a descrição da imagem aqui

<ul>
  <li class="drop-menu">
    <span class="menu-item">Teste 1 <i class="fas fa-caret-down"></i></span>
    <ul class="submenu">
      <?php
          foreach($categorias as $categoria){
      ?>
        <li><a href="<?php echo base_url
        ('categoria/'.$categoria->id.'/'.limpar($categoria->titulo)) ?>"><?php echo $categoria->titulo ?></a></li>
      <?php
          }
      ?>
    </ul>
  </li>
  <li class="drop-menu">
    <span class="menu-item">Teste 2 <i class="fas fa-caret-down"></i></span>
    <ul class="submenu">
      <?php
          foreach($categorias as $categoria){
      ?>
        <li><a href="<?php echo base_url
        ('categoria/'.$categoria->id.'/'.limpar($categoria->titulo)) ?>"><?php echo $categoria->titulo ?></a></li>
      <?php
          }
      ?>
    </ul>
  </li>
</ul>

  • You could inform the queries you make to generate this $categories array ?

1 answer

1

From what I understand you would actually need to generate every 'li' of the table by php itself using the while loop:

    $query = "select * from sua_tabela where seus_parametros";
    try {
        $result = $connect->prepare($query);
        $result->execute();
        $count = $result->rowCount();
        if ($count > 0) {
            while ($categoria= $result->FETCH(PDO::FETCH_OBJ)) {
                ?>
                <li class="drop-menu">
                    <span class="menu-item"><?php echo $categoria->titulo; ?><i class="fas fa-caret-down"></i></span>
                    <ul class="submenu">
                        <li>
                            <a href="<?php echo base_url('categoria/'.$categoria->id.'/'.limpar($categoria->titulo)) ?>"><?php echo $categoria->titulo ?></a>
                        </li>
                    </ul>
                </li>    
            <?php
            }
        }
    } catch (PDOException $error) {
        echo $error;
    }

Note that I used PDO for the connection and for the query, if using mysqli make the necessary changes.

  • I really use mysql, but I do not know how to make these changes, I am beginner with database and php, I am doing a course of codeigniter by udemy and in the course is not explained this doubt that arose me during the same, where should I make these changes? Thanks in advance!

  • 1

    I don’t use mysqli, take a look at this other post: link, just adapt what is in the code I gave you using the mysql query, but I strongly suggest that you use PDO instead of mysqli, because it uses (prepare) that optimizes the processes between served-client and brings even more security than mysqli, when you have a time search more about it.

Browser other questions tagged

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