将数据显示为从数据库派生的下拉列表

时间:2018-03-18 11:10:14

标签: mysql

我有菜单栏。它有许多主要类别和子类别。我将这些主要和子类别存储在数据库中。  我使用下面的代码

导出了这些数据
<li class="dropdown">
    <?php while($rowmain = mysql_fetch_array($resultmain)):;?>
    <?php echo "<a href='#' class='dropdown-toggle' data-toggle='dropdown'>$rowmain[0]<b class='caret'></b></a>" ?>
    <ul class="dropdown-menu multi-column columns-3">
    <div class="row">
    <div class="multi-gd-img">
    <ul class="multi-column-dropdown">
    <h6>All Groceries</h6>
        <li><a href='groceries.php'><?php echo "$rowmain[1]"?></a></li>
    </ul>
    </div>  
    </div>
    </ul>
    <?php endwhile;?>
</li>

mysql代码

<?php
ob_start();
include("../database/connection.php");

$main="SELECT t1.name,x from maincatagory t1 LEFT JOIN(SELECT GROUP_CONCAT(DISTINCT name) as x,mainID from subcatagory GROUP by mainID) t2 on t1.mainID = t2.mainID";
$resultmain=mysql_query($main);
// Mysql_num_row is counting table row
$countmain=mysql_num_rows($resultmain);

ob_end_flush();
?>

但我的问题是子类别不显示为列表

它显示在同一行

请帮我解决这个问题。 我从数据库导出的数据是

**Main**               **Sub**
Alcohol                 BEER
Baby Care               soap , toy
Chilled&Frozen Food     chicken,egg
Fresh Halal Meats       chicken,pizza
Fruits And Vegetables   ale,orange
Grocery                 Savoury ingredients ,Soups,Meals,Canned vegetables...
Gift&Greeting Cards     birthdaycard, valantine card
Household               spoon,knife
Medicine                panadol,priton,amoxixiline
Soft Drinks             cococola,fanta,pepsi

我的子类别包含许多数据,这些数据应作为列出的数据存储  我的预期表应该是

**Main**               **Sub**
    Alcohol                 BEER
    Baby Care               soap
                            toy
    Chilled&Frozen Food     chicken
                            egg
    Fresh Halal Meats       chicken
                            pizza
    Fruits And Vegetables   apple
                            orange
    Grocery                 Savory ingredients
                            Soups
                            Meals
                            vegetables...

    Gift&Greeting Cards     birthday card
                            valentine card
    Household               spoon
                            knife
    Medicine                panadol 
                            priton
                            amoxiciline
    Soft Drinks             pepsi
                            cococola
                            fanta

感谢先进。

1 个答案:

答案 0 :(得分:1)

首先检查sub不为null。 如果它不为null,则用逗号分解并预测你的爆炸数据。