0

I try to implements @Tomas answer on this question > PHP foreach create menu from array

I have Model below:

<?php
class Menu_model extends CI_Model{
    function __construct(){
        parent::__construct();
        $this->load->database();
    }

    function getCategory($lang){
        $sql = "SELECT * FROM category WHERE category.lang='".mysql_real_escape_string($lang)."'";
        $result = $this->db->query($sql);
        return $result;
    }

    function getSubCategory($lang){
        $sql = "SELECT * FROM subcategory WHERE subcategory.lang='".mysql_real_escape_string($lang)."'";
        $result = $this->db->query($sql);
        return $result;
    }

}
?>

Then I use this Controller to throw the data:

<?php
$_SESSION['lang'] = 'EN';

class Home extends CI_Controller {
    function __construct(){
        parent::__construct();
        $this->load->database();
        $this->load->model("menu_model");
    }

    public function index()
    {
        $data["category"] = $this->menu_model->getCategory($_SESSION['lang']);
        $data["subcategory"] = $this->menu_model->getSubCategory($_SESSION['lang']);

        $this->load->view('leftmenu_view',$data);//Left Menu
    }
}

This is my view to display the menu:

<?php
                foreach($category->result() as $menu){
                echo "<li><a class=\"sf-with-ul\" href=\"category/".$menu->urlcategory."\">".$menu->namecategory."</a></li>";
                    foreach($subcategory->result() as $key=>$submenu){
                        if ($menu->idcategory == $submenu->idcategory){
                            if ($key > 0){
                            echo "</li>";
                            }
                            echo "<ul class=\"sub-menu\"><a href=\"category/".$submenu->urlsubcategory."\">".$submenu->namesubcategory."</a><li>";
                        }
                    }
                }
                        ?>

The single menu works good on loops, but when the menu have submenu, the loops become false in structure.

ul
  li-a--Single Menu--/a-/li ->Have no SubMenu, Works Good
  li-a--Single Menu--/a-/li ->Have no SubMenu, Works Good
  li-a--Single Menu--/a-/li ->Have SubMenu, False Structure
    ul-class=submenu
       a-Sub Menu-/a
       li--/li
       ul-class=submenu
         a-Sub Menu-/a
         li--/li
    /ul
ul

The menu structure should become like this:

ul
  li-a--Single Menu--/a-/li
  li-a--Single Menu--/a-/li
  li-a-Single Menu With Sub Menu-/a-/li
    ul-class=submenu
      li-a-Sub Menu-/a-/li
      li-a-Sub Menu-/a-/li
      li-a-Sub Menu-/a-/li
    /ul
  li-a--Single Menu--/a-/li
  li-a--Single Menu--/a-/li
ul
3
  • Did you dump all your variables out to see what you have in your $data["category"]? Just make sure your $menu->namesubcategory may not be NULL Commented Sep 16, 2013 at 5:34
  • Hi @JofryHS, I've update and editing my script today. I use $data["category"] and $data["subcategory"] to throw the data. And Yes, $data["category"] is not NULL. Commented Sep 17, 2013 at 0:49
  • @Tomas's structure is different i think.... Commented Sep 17, 2013 at 6:08

4 Answers 4

2
foreach($category->result() as $menu){
echo "<li><a class=\"sf-with-ul\" href=\"category/".$menu->urlcategory."\">".$menu->namecategory."</a>";
$subcat = $subcategory->result();
if(count($subcat) > 0){

    echo "<ul class=\"sub-menu\">";
        foreach($subcat as $submenu){ 
        if ($menu->idcategory == $submenu->idcategory){
            echo "<li class=\"menu-item menu-item-type-taxonomy menu-item-object-category\" style=\"white-space: normal; float: none; width: auto;\"><a class=\"sf-with-ul\" href=\"http://travellingbali.com/category/".$menu->urlcategory."/".$submenu->urlsubcategory."/\" style=\"float: none; width: auto;\">".$submenu->namesubcategory."</a>";                                   

            echo "</li>";
        }
    }
    echo "</ul>";

}
echo "</li>";
}

try this. maybe work for you..

Sign up to request clarification or add additional context in comments.

Comments

0

@plain-jane. I tried your looping, but they print:

<li><a>--</a></li> ->True
<li><a>--</a></li> ->True
  <ul class="submenu">--</ul> ->False
<li>--</li> ->False
  <ul class="submenu">--</ul> ->False
<li>--</li> ->False

On your loops, all sub-menu became first menu. They should become like this:

<li><a>-Menu-</a></li>
<li><a>-Menu-</a></li>
  <ul class="submenu">
    <li><a>-SubMenu-</a></li>
    <li><a>-SubMenu-</a></li>
    <li><a>-SubMenu-</a></li>
  </ul>
<li><a>-Menu-</a></li>

Comments

0

your structure of printing ul li in sub menu is wrong it seems

Try this

   foreach($category->result() as $menu)
    {
       echo "<li><a class=\"sf-with-ul\" href=\"category/".$menu->urlcategory."\">".$menu->namecategory."</a>";
       /*check whether sub menu is there if so first print <ul> then <li><a>*/
       if(count($subcategory->result()) > 0 && is_array($subcategory->result()))
       {
             echo "<ul class=\"sub-menu\">";
             foreach($subcategory->result() as $key=>$submenu)
             {
               if ($menu->idcategory == $submenu->idcategory){
                echo "<li><a href=\"category/".$submenu->urlsubcategory."\">".$submenu->namesubcategory."</a></li>";
                }
             }
            echo "</ul>";
        }
        echo "</li>";
    }

4 Comments

Thanks @plain-jane. I tried your looping, but they still print false.
I've tried your edited code, but they still print false structure. All sub-menu now print at the top of menu, then the main-menu print at the bottom of menu.So the sub-menu and main-menu have the same style, all became main-menu.
what is this true and false in the structure below?
Are you sure there are 3 sub menus in your second menu item?
0

enter image description here

You can do by this way: here this the function to create dropdown menu it's the same ways to create dynamic menu please check here

I this it can help you.

Comments

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.