douqun1977 2017-09-21 14:26
浏览 14

PHP:来自同一表和行的类别和子类别?

I have a table that looks like this:

id            di_make              di_model

1             Samsung               TV656  
2             Samsung               TV555
3              Sony                 LCD33  

I need to create a category and subcategories menu from this table...

LIKE THIS:

http://jsfiddle.net/7NYhe/500/

So i tried this:

$storecategories = "";
$storesubcategories = "";

$sql2="SELECT DISTINCT `di_make` FROM `products` ORDER BY id";
$query2 = mysqli_query($db_conx, $sql2);
$existCount2 = mysqli_num_rows($query2);
if ($existCount2!=0) {

while($row2 = mysqli_fetch_array($query2, MYSQLI_ASSOC)){

$di_makeC = $row2["di_make"];


/////Create sub categories/////////
$sql3="SELECT * FROM `products` WHERE di_make='$di_makeC'";
$query3 = mysqli_query($db_conx, $sql3);
$existCount3 = mysqli_num_rows($query3);
if ($existCount3!=0) {

while($row3 = mysqli_fetch_array($query3, MYSQLI_ASSOC)){

$di_modelC = $row3["di_model"];

$storesubcategories .='<li>'.$di_modelC.'</li>';



}
}
/////End of Create sub categories/////////


 $storecategories .= '<li class="dropdown">'.$di_makeC.'
      <ul>
           '.$storesubcategories.'
      </ul>
   </li>';

}
}

However, the code above acts weirdly!

By that I mean, it will create the Categories and then create the subcategories BUT it will repeat the subcategories from the second category.

Like this:

http://jsfiddle.net/7NYhe/501/

Could someone please advice on this issue?

Thanks in advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用