2foreach循环与mysql查询

gpnt7bae  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(308)

我有两个mysql表工具栏和subtoolbar
工具栏

| id | toolbar  | status |

|  1 | Usuarios | ACTIVE |

|  2 | Bancas   | ACTIVE |

|  3 | Balance  | ACTIVE |

舌下腺

| id | subtoolbar | toolbarid | status |

|  1 | Editar     | 1         | ACTIVE |

|  2 | Agregar    | 1         | ACTIVE |

|  3 | Bancas     | 2         | ACTIVE |

|  4 | Listeros   | 2         | ACTIVE |

|  5 | Agencias   | 2         | ACTIVE |

|  6 | Taquillas  | 2         | ACTIVE |

|  7 | Layout     | 3         | ACTIVE |

我想列出每个报表及其子报表,因此我尝试了以下方法

$res=mysql_query("select * FROM toolbar");

$num_rows = (mysql_num_rows($res) -1);

while ($x = mysql_fetch_assoc($res)){

  $toolid[] = $x['id'];

  $toolbar[] = $x['toolbar'];

  $toolstatus[] = $x['status'];

}

在php中

foreach (range(0, $num_rows) as $number) {

$res1=mysql_query("select * FROM subtoolbar where

 toolbarid='$toolid[$number]'");

$num_rows2 = (mysql_num_rows($res1) -1);

while ($y = mysql_fetch_assoc($res1)){

  $subtoolid[] = $y['id'];

  $subtoolbar[] = $y['subtoolbar'];

  $toolbarid[] = $y['toolbarid'];

  $subtoolstatus[] = $y['status'];

}

 echo $toolbar[$number]; 

foreach (range(0, $num_rows2) as $number2) {

echo $subtoolbar[$number2];

}}

结果是这样的
高利贷
编辑
阿格雷加
班卡斯
编辑
阿格雷加
班卡斯
李斯特罗斯
平衡
编辑

ozxc1zmp

ozxc1zmp1#

我正在分享我自己的代码,这是在我的网站正常工作-

<div id="navbar" class="navbar-collapse collapse">

                            <ul class="nav navbar-nav">
                            <?php
                            $Clsmenu = new Menus;
                            $menu_res = mysql_query("SELECT * FROM `nav_menus`");();
                            while($menu_details=mysql_fetch_array($menu_res))
                            {                               
                                $sub_menu_query=mysql_query("SELECT * FROM `category` WHERE `Menu_id`='$menu_details['id']'");
                                if(mysql_num_rows($sub_menu_query))
                                {?>
                                <li class="dropdown">
                                    <a href="category.html" class="dropdown-toggle" data-toggle="dropdown"><?php echo $menu_details['name']; ?></a>
                                     <ul class="mega_dropdown dropdown-menu" style="width: 830px;">
                                      <?php
                                        while($submenu_relation_details=mysql_fetch_array($sub_menu_query))
                                        {
                                     ?>
                                        <li class="block-container col-sm-2">
                                            <ul class="block">
                                                <li class="link_container group_header">
                                                    <a href="#"><?php echo $submenu_relation_details['name']; ?></a>
                                                </li>
                                                <?php
                                                  $sub_menu=mysql_query("SELECT * FROM `sub_category` WHERE `cat_id`='$submenu_relation_details['id']'");
                                                  $i=1;
                                                  while($data=mysql_fetch_array($sub_menu))
                                                  { 
                                                   ?>
                                                    <li class="link_container"><a href="Selectedproduct.php?id=<?php echo base64_encode($data['id']); ?>"><?php echo $data['name']; ?></a></li>
                                                   <?php
                                                   $i++;
                                                  }
                                                ?>
                                             </ul>
                                        </li>
                                        <?php
                                        }
                                        ?>
                                        </ul>

                                </li>

                                <?php 
                                }
                                else
                                {
                                ?>
                                <li>
                                <a href="category.html" ><?php echo $menu_details['name']; ?></a></li>
                                <?php
                                }

                            }
                            ?>
                            </ul>

                        </div>

我也在分享你的图片,请点击我。

cyej8jka

cyej8jka2#

请尝试这种方法:但必须记住:
必须使用mysqli()而不是mysql()
检查数组是否为空,值是否已设置。

$res=mysql_query("select t.id,t.toolbar,st.* FROM toolbar t left join 
  subtoolbar st on t.id=toolbarid where t.status='ACTIVE' and 
  st.status='ACTIVE'");

 if(mysql_num_rows($res)>0)
 {
   while ($rs = mysql_fetch_assoc($res)){
      $retArr[$rs['toolbarid']][] = array("toolbar"=>$rs["subtoolbar"], "subtoolbar"=>$rs["subtoolbar"]);
   }
}

foreach($retArr as $rs )
{
 $cnt=0;

   foreach($rs as $toolbarName => $data)
   {   
     if( $cnt==0)
     {
      echo $data["toolbar"]."<br>"; 
     }
     $cnt++;
     echo $data["subtoolbar"]."<br>";

   }

}

相关问题