如何解决 codeigniter 中的动态多个下拉菜单问题?

发布于 2024-10-10 18:45:36 字数 2126 浏览 1 评论 0原文

我有一个通过从数据库中获取标题动态生成的菜单。我通过添加列 p_id 为子页面设置了相同的表。

我的代码看起来像这样。

private $Section = array();
function _menu($root){

    $this->db->select('id,title,p_id');

    $this->db->Where('p_id',$root);

    $this->db->Where('status','active');

    $this->db->Where('type','page');

    $this->db->order_by('sort');

    $query = $this->db->get('pages');

    foreach ($query->result() as $row)

    {

        $this->Section[] = '<ul>';

            $this->Section[] = '<li>';

            $this->Section[] = anchor('site/page/'.$row->id.'#cont_main',$row->title);

            $this->_menu($row->id);

            $this->Section[] = '</li>';

        $this->Section[] = '</ul>';

    }
    

    $query->free_result();

    return $this->Section;

}

//get menu tree    

function MenuTree(){

    return $this->_menu(0);

}

在上面的 _menu() 中,如果导航有两个或更多子项,那么它不会显示,因为它填充的源提供了额外的

    。来源看起来像这样

<ul>
<li><a href="#">menu 1</a>
    <ul>
        <li><a>menu 1 sub1</a></li>
    </ul>
    <ul>
        <li><a>menu 1 sub2</a></li>
    </ul>
</li>
While I am assuming to get:
<ul>
<li><a href="#">menu 1</a>
    <ul>
        <li><a>menu 1 sub1</a></li>
        <li><a>menu 1 sub2</a></li>
    </ul>
</li>
**edited** I have to add a menu according to the child element. Image comes in all menus which don't have child too due to extra ```
    ``` if I use ul before the loop. The source looks like this.
    <ul>
    <li><a href="#">menu 1</a>
        <ul>
            <li><a>menu 1 sub1</a>
                <ul></ul>
            </li>
            <li><a>menu 1 sub2</a></li>
        </ul>
    </li>
    

    任何想法将不胜感激。

    I have a menu generated dynamically by taking title from the database. I have set the same table for sub pages by adding a column p_id.

    And my code looks like this.

    private $Section = array();
    function _menu($root){
    
        $this->db->select('id,title,p_id');
    
        $this->db->Where('p_id',$root);
    
        $this->db->Where('status','active');
    
        $this->db->Where('type','page');
    
        $this->db->order_by('sort');
    
        $query = $this->db->get('pages');
    
        foreach ($query->result() as $row)
    
        {
    
            $this->Section[] = '<ul>';
    
                $this->Section[] = '<li>';
    
                $this->Section[] = anchor('site/page/'.$row->id.'#cont_main',$row->title);
    
                $this->_menu($row->id);
    
                $this->Section[] = '</li>';
    
            $this->Section[] = '</ul>';
    
        }
        
    
        $query->free_result();
    
        return $this->Section;
    
    }
    
    //get menu tree    
    
    function MenuTree(){
    
        return $this->_menu(0);
    
    }
    

    In above _menu() if a nav has two or more children then it doesn't display because the source populated by it gives extra
    </ul> and <ul>. The source looks like this

    <ul>
    <li><a href="#">menu 1</a>
        <ul>
            <li><a>menu 1 sub1</a></li>
        </ul>
        <ul>
            <li><a>menu 1 sub2</a></li>
        </ul>
    </li>
    

    While I am assuming to get:

    <ul>
    <li><a href="#">menu 1</a>
        <ul>
            <li><a>menu 1 sub1</a></li>
            <li><a>menu 1 sub2</a></li>
        </ul>
    </li>
    

    **edited**
    I have to add a menu according to the child element. Image comes in all menus which don't have child too due to extra ```

      ``` if I use ul before the loop. The source looks like this.

      <ul>
      <li><a href="#">menu 1</a>
          <ul>
              <li><a>menu 1 sub1</a>
                  <ul></ul>
              </li>
              <li><a>menu 1 sub2</a></li>
          </ul>
      </li>
      

      Any idea would be appreciated.

      如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

      扫码二维码加入Web技术交流群

      发布评论

      需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

      评论(2

      梦过后 2024-10-17 18:45:36

      您需要在 foreach 循环之外添加 ul 并确保您已经有了结果!

      if ($query->num_rows() > 0) { // we actually have results, open a UL and loop to add the LIs
          $this->Section[] = '<ul>';
          foreach ($query->result() as $row) {
      

      You need to add your ul outside the foreach loop AND make sure that you already have results!

      if ($query->num_rows() > 0) { // we actually have results, open a UL and loop to add the LIs
          $this->Section[] = '<ul>';
          foreach ($query->result() as $row) {
      
      泪痕残 2024-10-17 18:45:36

      您正在使用的

      foreach ($query->result() as $row) {
         $this->Section[] = '<ul>';
      

      当然总是显示 ul 因为它为每个元素执行此操作。您需要将

      $this->Section[] = '<ul>';
      

      其关闭块放置在循环之外。如果您希望仅在有多个父级时触发它,则应根据您需要的一切来检查此条件并做出相应反应的 IF 语句。


      剩下的问题似乎是 ul 的放置。您可以

      function MenuTree() {
          return "<ul>" . $this->_menu(0) . "</ul>";
      }
      

      完全删除上面提到的 ul 以获得内部循环。

      You are using

      foreach ($query->result() as $row) {
         $this->Section[] = '<ul>';
      

      which of course always displays an ul since it does it FOR EACH element. You need to place

      $this->Section[] = '<ul>';
      

      and it's closing block outside the loop. If you want it only triggered if there are more than one parent, an IF statement that check's this and reacts accordingly should by everything you need.


      The problem that remains seems to be the placing of the ul. You could do

      function MenuTree() {
          return "<ul>" . $this->_menu(0) . "</ul>";
      }
      

      and remove the mentioned ul from above completely to just get the inner loops.

      ~没有更多了~
      我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
      原文