How to create dynamic navigation menu select from database using Codeigniter?

true man

I've been trying to find the solution for creating a dynamic navigation menu store in database using Codeigniter. But till now I can't solve my problem and I really don't understand how to create this feature I try to create below code but I can show only menu without sub menu. As in the view I can't only select menu but can't with sub menus. I'm really don't understand how to make the conditional for create sub menus Because I have to compared parent with menus ID if I found the parent equal to menu ID I will show the drop down for current that menu But it is fail for me

Here is my View

<div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
       <li><a>some menu retrieve from DB </a></li>
           <li class="dropdown megamenu-fullwidth"> 
              <a data-toggle="dropdown" class="dropdown-toggle">title</a>
              <ul class="dropdown-menu"> 
                <li class="megamenu-content">
                 <ul class="col-lg-2 col-sm-2 col-md-2">
                   <li class="no-border"><p><strong>Head</strong></p></li>
                   <li><a href="#">Descript<br></a></li>
                 </ul> 
               </li> 
            </ul>
        </li>
       <?PHP endif; ?>
      <?PHP endforeach; ?>
 </ul>    
</div>

Here is Controller

$this->data['menus'] =  $this->nav_m->menus(); 

Here is model

 public function menus() {
        $this->db->select("*");
        $this->db->from("nav"); 
        $this->q = $this->db->get();
        if ($this->q->num_rows() > 0) {
           return $this->q->result_array();
        }
    }

Here is my navigation which I used Var_dump() from DB

array (size=7)
  0 => 
    array (size=9)
      'id' => string '1' (length=1)
      'uid' => string '0' (length=1)
      'text' => string 'title1' (length=6)
      'link' => string 'menus1' (length=6)
      'show_condition' => string '1' (length=1)
      'parent' => string '0' (length=1)
      'class' => string '' (length=0)
      'data' => string '' (length=0)
      'des' => string '' (length=0)
  1 => 
    array (size=9)
      'id' => string '2' (length=1)
      'uid' => string '0' (length=1)
      'text' => string 'title2' (length=6)
      'link' => string 'menus2' (length=6)
      'show_condition' => string '1' (length=1)
      'parent' => string '2' (length=1)
      'class' => string '1' (length=1)
      'data' => string '' (length=0)
      'des' => string '' (length=0)
  2 => 
    array (size=9)
      'id' => string '3' (length=1)
      'uid' => string '0' (length=1)
      'text' => string 'title3' (length=6)
      'link' => string 'menu3' (length=5)
      'show_condition' => string '1' (length=1)
      'parent' => string '2' (length=1)
      'class' => string '' (length=0)
      'data' => string '' (length=0)
      'des' => string '' (length=0)
  3 => 
    array (size=9)
      'id' => string '4' (length=1)
      'uid' => string '0' (length=1)
      'text' => string 'Sub1' (length=4)
      'link' => string 'menu4' (length=5)
      'show_condition' => string '1' (length=1)
      'parent' => string '2' (length=1)
      'class' => string '' (length=0)
      'data' => string '' (length=0)
      'des' => string '' (length=0)
  4 => 
    array (size=9)
      'id' => string '5' (length=1)
      'uid' => string '0' (length=1)
      'text' => string 'sub2' (length=4)
      'link' => string 'hhhhhhhhhhhh' (length=12)
      'show_condition' => string '1' (length=1)
      'parent' => string '2' (length=1)
      'class' => string '' (length=0)
      'data' => string '' (length=0)
      'des' => string '' (length=0)
  5 => 

Please help

CodeGodie

the way you have your result is too complicated to deal with. I would separate children and parent menu items in your database, then build an array so that the view can then parse it easier. Try this:

DB:

enter image description here

Then your model:

function menus() {
    $this->db->select("*");
    $this->db->from("menu_parents");
    $q = $this->db->get();

    $final = array();
    if ($q->num_rows() > 0) {
        foreach ($q->result() as $row) {

            $this->db->select("*");
            $this->db->from("menu_children");
            $this->db->where("fk_parent_id", $row->parent_id);
            $q = $this->db->get();
            if ($q->num_rows() > 0) {
                $row->children = $q->result();
            }
            array_push($final, $row);
        }
    }
    return $final;
}

Controller:

public function get_menus() {
    $this->load->model('your_model');
    $menus = $this->your_model->menus();
    $data = array('menus' => $menus);
    $this->load->view('page1', $data);
}

View:

<div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <?php foreach ($menus as $menu) { ?>
            <li><a><?= $menu->text ?></a></li>
            <?php
            if (isset($menu->children)) {
                foreach ($menu->children as $child) {
                    ?>
                    <li class="dropdown megamenu-fullwidth"> 
                        <a data-toggle="dropdown" class="dropdown-toggle" href="#"><</a>
                        <ul class="dropdown-menu"> 
                            <li class="megamenu-content">
                                <ul class="col-lg-2 col-sm-2 col-md-2">
                                    <li class="no-border"><p><strong>Head</strong></p></li>
                                    <li><a href="#">Descript<br></a></li>
                                </ul> 
                            </li> 
                        </ul>
                    </li>
                    <?php
                }
            }
            ?>
        <?php } ?>
    </ul>    
</div>

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

Can we get the slected options Text of select menu using jquery "Dynamic option values"

How to select an element from a ComboBox menu using Vaadin Testbench?

How can create a dynamic menu in angular 2?

Parsing JSON (from select database) using Volley

How to make dynamic chart js using JSON in Codeigniter?

How to create a dynamic dropown using angular 2?

How to create a web installer using codeigniter Php

How to select users with most votes, from a database?

How to select from database according to current time

How to navigate from bottomsheetdialog to fragment using navigation?

How can I create dynamic/conditional navigation with Jetpack Navigation?

How to create a collapsable menu using bootstrap

How to subtract two dates (from 2 columns in database) in Codeigniter

How to create a dynamic menu in Polymer 1.0 using dom-repeat

How to create an id for a vertical navigation menu?

How can i store multiple select box value in database in codeigniter?

How to hide navigation menu in pages that are hidden in Navigation Menu in Liferay?

How to create a CNN Menu Navigation using HTML, CSS and JQuery

How select data from select statement from another database?

How to Select an item from Dropdown Menu using while Loop in Php Mysql

How to generate dynamic menu tree using recursion in ASP.Net?

While posting data from tinymce its not storing database using Codeigniter

How to deduct value from database codeigniter

How to store the data from a dynamic table to database

How to create an overlay Navigation menu on mobile screens, while having a normal navigation bar on normal screens

How to Explode data from database in controller of CodeIgniter?

Create a menu from database (mixed sub menus)

How to create a model class for dynamic keys and values in firebase database in android

how to select data from a database using SQL PDO

TOP lista

quentelabel

Arquivo