0

我尝试在这个问题上实现@Tomas 答案 > PHP foreach create menu from array

我有下面的模型:

<?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;
    }

}
?>

然后我使用这个控制器来抛出数据:

<?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
    }
}

这是我显示菜单的视图:

<?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>";
                        }
                    }
                }
                        ?>

单个菜单在循环上效果很好,但是当菜单有子菜单时,循环在结构上变得错误。

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

菜单结构应该是这样的:

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
4

4 回答 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>";
}

尝试这个。也许为你工作..

于 2013-09-18T03:03:42.550 回答
0

您在子菜单中打印 ul li 的结构似乎是错误的

尝试这个

   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>";
    }
于 2013-09-17T05:59:01.247 回答
0

@平原简。我试过你的循环,但他们打印:

<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

在您的循环中,所有子菜单都成为第一个菜单。他们应该变成这样:

<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>
于 2013-09-17T07:41:00.073 回答
0

在此处输入图像描述

你可以这样做:这里是创建下拉菜单的功能它与创建动态菜单的方法相同 请检查这里

我这个它可以帮助你。

于 2014-04-30T11:47:08.113 回答