3级子类别系统在第3级搞砸了

时间:2012-01-05 14:03:17

标签: php mysql arrays menu

我想创建一个包含如下菜单的页面:

main1
 >sub1.1
   >sub1.1.1
   >sub1.1.2
 >sub1.2
   >sub1.2.1
main2
 >sub2.1
   >sub2.1.1
   >sub2.1.2
 >sub2.2
   >sub2.2.1    

但目前正在发生的是:

main1
 >sub1.1
 >sub1.2
   >sub1.1.1
   >sub1.1.2
   >sub1.2.1
main2
 >sub2.1
 >sub2.2
   >sub2.1.1
   >sub2.1.2
   >sub2.2.1 

这是我的代码:

<?php 

$categories = array();

// main-categories
$sql = 'SELECT * FROM forum_market_main ORDER BY market_main_id ASC';
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
    $categories[$row['market_main_id']] = $row;
}

// sub-categories
$sql = 'SELECT * FROM forum_market_sub ORDER BY market_sub_id ASC';
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
    $categories[$row['market_main_id']]['forum_market_sub'][] = $row;
}

// sub2-categories
$sql = 'SELECT * FROM forum_market_sub2 ORDER BY market_sub2_id ASC';
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
    $categories[$row['market_main_id']]['forum_market_sub']['forum_market_sub2'][] = $row;
}

print_r($categories);

?>

您可以在此处查看:click - 查看源模式以便于理解。

我的奖金问题将是一个PHP代码或什么使得整个菜单系统显示得很好。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我建议看看这个click,了解如何为分层数据设计数据库。