Codeigniter - Retrieve categories from the bank

0

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

Thanks in advance!

<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>
    
asked by anonymous 01.05.2018 / 20:37

1 answer

1

From what I understand, you would actually need to generate each '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.

    
01.05.2018 / 21:20