从3个mysql表中提取相关数据并输出为列表项

时间:2012-09-27 08:18:38

标签: php menu html-lists

我有一系列MySQL表格,用于创建菜单系统。

这些是表:Section - SubCategory1 - SubCategory2

数据涉及以下方面: 第1节是菜单中的主要标题。 SubCategory1是Section表的子类别 Subcategory1进一步细化了Subcategory2部分。

它们包含以下内容:

SECTION
idSection, strSection, strSectionURL

SUBCATEGORY1
idSubCategory1, idSection, strSubCategory1

SUBCATEGORY2
idSubcategory2, idSubCategory1, strSubCategory2

如您所见,每个都有对其上方表格的引用,该表格始终是它所属的部分或子部分的参考编号。

我需要在一系列无序列表中按如下方式输出,以便我可以将其格式化为菜单系统。     

        
  • 部分
  •         
                  
    • 子类别1
    •                 
                            
      • SUBCATEGORY2
      •                 
              
        

非常感谢任何帮助。

菲尔

1 个答案:

答案 0 :(得分:0)

使用此

$sql = mysql_query("select * from section");
echo "<ul>";
while($row = mysql_fetch_array($sql))
{
    echo "<li>$row[strSection]</li>";
    $sql1 = mysql_query("select * from SUBCATEGORY1 where idSection ='$row[strSection]' ");
    echo "<ul>";
    while($row1 = mysql_fetch_array($sql1))
    {
        echo "<li>$row1[strSubCategory1]</li>";
        $sql2 = mysql_query("select * from SUBCATEGORY2 where strSubCategory1 ='$row[strSubCategory1]' ");
         echo "<ul>";
         while($row2 = mysql_fetch_array($sql2))
         {
              echo "<li>$row1[strSubCategory2]</li>";
         }
         echo "</ul>";
    }
    echo "</ul>";
}
echo "</ul>";

但强烈建议您使用MysqliPDO代替mysql。这将按照您指定的列表顺序打印。