douqiaotong8682 2016-12-15 11:49
浏览 64

格式化php生成的动态ul列表

I have a code that generates dynamic menu, I cannot seem to make the list be similar to the static one on html file, like for example the pathologist children is generated in two different ways instead of being generated in the same unordered list under same class

I have the following array from mysql table

   Array ( [0] => stdClass Object ( [id] => 1 [name] => Obituaries [parent] => 0 ) [1] => stdClass Object ( [id] => 2 [name] => Caskets [parent] => 0 ) [2] => stdClass Object ( [id] => 3 [name] => Pathologist [parent] => 0 ) [3] => stdClass Object ( [id] => 4 [name] => Hospital [parent] => 0 ) [4] => stdClass Object ( [id] => 5 [name] => Flowers [parent] => 0 ) [5] => stdClass Object ( [id] => 6 [name] => Hearse [parent] => 0 ) [6] => stdClass Object ( [id] => 7 [name] => Small ones [parent] => 2 ) [7] => stdClass Object ( [id] => 8 [name] => Big ones [parent] => 2 ) [8] => stdClass Object ( [id] => 9 [name] => Red Rose [parent] => 0 ) [9] => stdClass Object ( [id] => 12 [name] => Pathologist 1 [parent] => 3 ) [10] => stdClass Object ( [id] => 13 [name] => Pathologist 2 [parent] => 3 ) ) 

I want to generate a dynamic menu which looks exactly like this, see my html (static) example

<ul>
                <li><a href="#">Obituaries<i class="icons icon-right-dir"></i></a></li>
                <li><a href="#">Caskets <i class="icons icon-right-dir"></i></a>
                 <ul class="sidebar-dropdown">
                        <li>
                            <ul>
                                <li><a href="#">Smlal Ones</a></li>
                                <li><a href="#">Big Ones</a></li>

                            </ul>
                        </li>                        
                    </ul>
                </li>
                <li><a href="#">Pathologists <i class="icons icon-right-dir"></i></a>
                    <ul class="sidebar-dropdown">
                        <li>
                            <ul>
                                <li><a href="#">Pathologist 1</a></li>
                                <li><a href="#">Pathologist 2</a></li>

                            </ul>
                        </li>                        
                    </ul>
                </li>
                <li><a href="#">Hospitals<i class="icons icon-right-dir"></i></a></li>

            </ul>

My Code(PHP)

function menuBuilder() {
    $tree = $this->_API->getData('categories');
    $node_id = '';

    echo '<ul>';
    foreach ($tree as $node):
        if ($node->parent == 0):

            echo '<li><a href="#">'.
            $node->name .'<i class="icons icon-right-dir"></i></a> ';
            $node_id = $node->id;
            $this->subMenuBuilder($tree, $node_id);
            echo '</li>';
        endif;
    endforeach;
    echo '</ul>';
}

function subMenuBuilder($tree, $node_id) {

    foreach ($tree as $branch):

        if ($branch->parent == $node_id):
            echo '<ul class="sidebar-dropdown"><li><ul>';
           echo '<li><a href="#">'.
             $branch->name .'</a>';
            $this->subMenuBuilder($tree, $branch->id);
             echo '</ul><li></li>';
                echo '</ul>';
        endif;

    endforeach;

}

What php generates

<ul>
   <li><a href="#">Obituaries<i class="icons icon-right-dir"></i></a> </li>
   <li>
      <a href="#">Caskets<i class="icons icon-right-dir"></i></a> 
      <ul class="sidebar-dropdown">
         <li>
            <ul>
               <li><a href="#">Small ones</a>
            </ul>
         </li>
         </li>
      </ul>
      <ul class="sidebar-dropdown">
         <li>
            <ul>
               <li><a href="#">Big ones</a>
            </ul>
         </li>
         </li>
      </ul>
   </li>
   <li>
      <a href="#">Pathologist<i class="icons icon-right-dir"></i></a> 
      <ul class="sidebar-dropdown">
         <li>
            <ul>
               <li><a href="#">Pathologist 1</a>   //difference here
            </ul>
         </li>
         </li>
      </ul>
      <ul class="sidebar-dropdown">
         <li>
            <ul>
               <li><a href="#">Pathologist 2</a>      //difference here
            </ul>
         </li>
         </li>
      </ul>
   </li>
   <li><a href="#">Hospital<i class="icons icon-right-dir"></i></a> </li>
   <li><a href="#">Flowers<i class="icons icon-right-dir"></i></a> </li>
</ul>

How can I make the generated tags be exactly same as the static one above, especially i know the problem could lie in the subMenuBuilder function. Any Suggestions?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥120 计算机网络的新校区组网设计
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等