2 foreach循环与mysql查询

时间:2016-10-06 03:10:32

标签: php mysql

我有2个mysql表工具栏和子工具栏

工具栏

| id | toolbar  | status |

|  1 | Usuarios | ACTIVE |

|  2 | Bancas   | ACTIVE |

|  3 | Balance  | ACTIVE |

subtoolbar

| 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];

}}

结果以这种方式出现

USUARIOS

  • Editar

  • Agregar

Bancas

  • Editar

  • Agregar

  • Bancas

  • Listeros

余额

  • Editar

2 个答案:

答案 0 :(得分:0)

请尝试这种方法: 但必须记住:

  1. 必须使用mysqli_ ()而不是mysql _ ()
  2. 检查数组是否为空&是否设置了值。

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

答案 1 :(得分:0)

我正在分享我自己的代码,该代码在我的网站中正常运行 -

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

I am sharing you Image also, Please click on me.