猿问

php 中的 foreach(菜单和子菜单)

我已经通过 oop php 创建了一个菜单和子菜单,但 foreach 循环有问题。我想要实现的目标是让子菜单数组仅在菜单数组的第三个元素中循环!我希望我描述得很好!


这是 foreach 代码:


<ul id="navigation">

    <?php

    $HeaderMenuOBJ = new Headermenu();

    $HeaderMenu = $HeaderMenuOBJ->getHeaderMenu();

    $SubHeaderMenu = $HeaderMenuOBJ->getHeaderSubMenu();

   //var_dump($SubHeaderMenu);

   //var_dump($HeaderMenu);

    foreach ($HeaderMenu as $row){

        ?>

        <li>

            <a class="active" 

                href="

                    <?php

                     echo $row['href'];

                    ?>

                ">

                <?php echo($row['content']);?>

                

            </a>

            <ul class="submenu">

                <?php 

                    foreach ($SubHeaderMenu as $row){

                ?>

                <li>

                    <a 

                        href="

                            <?php

                                echo $row['href'];

                            ?>

                            ">

                            <?php 

                                echo $row['content'];

                            ?>

                    </a>

                </li>

                <?php

                  }

                ?>

            </ul>


      </li>

        <?php

    }

 ?>

</ul>

这是我的 DOM(一种从数据库读取数据的控制器):


class Headermenu extends dbh {

    public $main_query = "SELECT * FROM `header_menu` WHERE parent is null";

    public $sub_query = "SELECT * FROM `header_menu` WHERE parent = 3";

    public function getHeaderMenu(){

       $menu = parent::connect()->query($this->main_query);

        while($row = $menu->fetch_array()){

            $rows[] = $row;

            /*$submenu = parent::connect()->query(str_replace("@param",$header->PK_ID,$this->sub_query));

           while($sub_header = $submenu-> fetch_array()){

               $header->subheaders[]=$sub_header;

           }*/

           //$headers[] = $header;

       }

       return $rows;

    } 



MMMHUHU
浏览 111回答 1
1回答

倚天杖

直接回答:首先,您需要在外部和内部 foreach 中使用 2 个不同的变量:foreach ($HeaderMenu as $menuRow){现在foreach ($SubHeaderMenu as $subMenuRow){两者都有$row. 你可以这样做:if ($menuRow['id'] == 3) { foreach ($SubHeaderMenu as $subMenuRow){- 所以将内部 foreach 包含在其中,if并且仅当父 id 为 3 时才执行此操作,但最好获取所有 header_menu 行,将该数据转换为嵌套数组并按动态结构显示。动态结构答案:获取所有菜单项,创建菜单数组,每个菜单都会有其子菜单项。然后显示这个就很容易了。我假设只有两个级别 - 菜单和子菜单。样本数据:+----+-------+---------------+-----------+| id | href&nbsp; | content&nbsp; &nbsp; &nbsp; &nbsp;| parent_id |+----+-------+---------------+-----------+|&nbsp; 1 | href1 | header1&nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; NULL ||&nbsp; 2 | href2 | header2&nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; NULL ||&nbsp; 3 | href3 | header3&nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; NULL ||&nbsp; 4 | href4 | subheader 1 1 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 ||&nbsp; 5 | href5 | subheader 1 2 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 ||&nbsp; 6 | href6 | subheader 2 1 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2 |+----+-------+---------------+-----------+代码:<?php$mysqli = new mysqli("localhost", "zz", "zz", "zz");// fetch all menu items$query = 'select * from header_menu order by parent_id';$result = $mysqli->query($query);$data = $result->fetch_all(MYSQLI_ASSOC);var_dump($data);// build menu with menus and their submenus$menu = [];foreach ($data as $row) {&nbsp; if ($row['parent_id'] === null) {&nbsp; &nbsp; $menu[$row['id']] = $row;&nbsp; &nbsp; $menu[$row['id']]['submenus'] = [];&nbsp; } else {&nbsp; &nbsp; $menu[$row['parent_id']]['submenus'][] = $row;&nbsp; }}var_dump($menu);// now display it in html or however you wantforeach ($menu as $item) {&nbsp; echo $item['content'].PHP_EOL;&nbsp; foreach ($item['submenus'] as $subitem) {&nbsp; &nbsp; echo '&nbsp; '.$subitem['content'].PHP_EOL;&nbsp; }}$数据:array(6) {&nbsp; [0]=>&nbsp; array(4) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; string(1) "1"&nbsp; &nbsp; ["href"]=>&nbsp; &nbsp; string(5) "href1"&nbsp; &nbsp; ["content"]=>&nbsp; &nbsp; string(7) "header1"&nbsp; &nbsp; ["parent_id"]=>&nbsp; &nbsp; NULL&nbsp; }&nbsp; [1]=>&nbsp; array(4) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; string(1) "2"&nbsp; &nbsp; ["href"]=>&nbsp; &nbsp; string(5) "href2"&nbsp; &nbsp; ["content"]=>&nbsp; &nbsp; string(7) "header2"&nbsp; &nbsp; ["parent_id"]=>&nbsp; &nbsp; NULL&nbsp; }&nbsp; [2]=>&nbsp; array(4) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; string(1) "3"&nbsp; &nbsp; ["href"]=>&nbsp; &nbsp; string(5) "href3"&nbsp; &nbsp; ["content"]=>&nbsp; &nbsp; string(7) "header3"&nbsp; &nbsp; ["parent_id"]=>&nbsp; &nbsp; NULL&nbsp; }&nbsp; [3]=>&nbsp; array(4) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; string(1) "4"&nbsp; &nbsp; ["href"]=>&nbsp; &nbsp; string(5) "href4"&nbsp; &nbsp; ["content"]=>&nbsp; &nbsp; string(13) "subheader 1 1"&nbsp; &nbsp; ["parent_id"]=>&nbsp; &nbsp; string(1) "1"&nbsp; }&nbsp; [4]=>&nbsp; array(4) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; string(1) "5"&nbsp; &nbsp; ["href"]=>&nbsp; &nbsp; string(5) "href5"&nbsp; &nbsp; ["content"]=>&nbsp; &nbsp; string(13) "subheader 1 2"&nbsp; &nbsp; ["parent_id"]=>&nbsp; &nbsp; string(1) "1"&nbsp; }&nbsp; [5]=>&nbsp; array(4) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; string(1) "6"&nbsp; &nbsp; ["href"]=>&nbsp; &nbsp; string(5) "href6"&nbsp; &nbsp; ["content"]=>&nbsp; &nbsp; string(13) "subheader 2 1"&nbsp; &nbsp; ["parent_id"]=>&nbsp; &nbsp; string(1) "2"&nbsp; }}$菜单:array(3) {&nbsp; [1]=>&nbsp; array(5) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; string(1) "1"&nbsp; &nbsp; ["href"]=>&nbsp; &nbsp; string(5) "href1"&nbsp; &nbsp; ["content"]=>&nbsp; &nbsp; string(7) "header1"&nbsp; &nbsp; ["parent_id"]=>&nbsp; &nbsp; NULL&nbsp; &nbsp; ["submenus"]=>&nbsp; &nbsp; array(2) {&nbsp; &nbsp; &nbsp; [0]=>&nbsp; &nbsp; &nbsp; array(4) {&nbsp; &nbsp; &nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(1) "4"&nbsp; &nbsp; &nbsp; &nbsp; ["href"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(5) "href4"&nbsp; &nbsp; &nbsp; &nbsp; ["content"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(13) "subheader 1 1"&nbsp; &nbsp; &nbsp; &nbsp; ["parent_id"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(1) "1"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; [1]=>&nbsp; &nbsp; &nbsp; array(4) {&nbsp; &nbsp; &nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(1) "5"&nbsp; &nbsp; &nbsp; &nbsp; ["href"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(5) "href5"&nbsp; &nbsp; &nbsp; &nbsp; ["content"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(13) "subheader 1 2"&nbsp; &nbsp; &nbsp; &nbsp; ["parent_id"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(1) "1"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp; [2]=>&nbsp; array(5) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; string(1) "2"&nbsp; &nbsp; ["href"]=>&nbsp; &nbsp; string(5) "href2"&nbsp; &nbsp; ["content"]=>&nbsp; &nbsp; string(7) "header2"&nbsp; &nbsp; ["parent_id"]=>&nbsp; &nbsp; NULL&nbsp; &nbsp; ["submenus"]=>&nbsp; &nbsp; array(1) {&nbsp; &nbsp; &nbsp; [0]=>&nbsp; &nbsp; &nbsp; array(4) {&nbsp; &nbsp; &nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(1) "6"&nbsp; &nbsp; &nbsp; &nbsp; ["href"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(5) "href6"&nbsp; &nbsp; &nbsp; &nbsp; ["content"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(13) "subheader 2 1"&nbsp; &nbsp; &nbsp; &nbsp; ["parent_id"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(1) "2"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp; [3]=>&nbsp; array(5) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; string(1) "3"&nbsp; &nbsp; ["href"]=>&nbsp; &nbsp; string(5) "href3"&nbsp; &nbsp; ["content"]=>&nbsp; &nbsp; string(7) "header3"&nbsp; &nbsp; ["parent_id"]=>&nbsp; &nbsp; NULL&nbsp; &nbsp; ["submenus"]=>&nbsp; &nbsp; array(0) {&nbsp; &nbsp; }&nbsp; }}菜单结构的输出:header1&nbsp; subheader 1 1&nbsp; subheader 1 2header2&nbsp; subheader 2 1header3现在您可以输出所有菜单项及其子菜单项,而不仅仅是菜单 ID 3。
随时随地看视频慕课网APP
我要回答