3

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

9
  • are you allowed to change the information in the database or does it have to stay like that? It would be so much easier if you had one table for your parent menus, and one menu for your children. Commented Mar 9, 2015 at 16:59
  • in the first value of your array, you have an array with parant = 0, is this suppose to be a parent menu? if so, how would you explain the others, they have parent = 2, but there are no parents for those submenus Commented Mar 9, 2015 at 17:03
  • should you show me some (sorry) if do like that you will select it and compare on menus ID right Commented Mar 9, 2015 at 17:04
  • yes you would just need to get that information from the database by ID Commented Mar 9, 2015 at 17:05
  • i'm really crazy right now I have not any idea about this Commented Mar 9, 2015 at 17:06

1 Answer 1

9

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>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks i have to do it now
I found one think on menu_childrent if I set value of fk_parent_id different (doesn't relate to Parent_id(1,2,3) and fk_parent_id(3,3,3)) I will can't retrieve menu which content parent_id 1 and 2. How could we selecte all parent_menu although it have not childrent
in the model, move array_push($final, $row); outside and under the if statement. I will update my answer.
my ID: 573 593 595 Password:126khd
I coun't vote up now sorry because I have not yet enough reputation sorry I will vote letter
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.