像这样的东西,也许(未经测试):
$tree = array();
while ($row = mysql_fetch_assoc($result)) {
$tree[$row['section_id']]['name'] = $row['section'];
$tree[$row['section_id']]['categories'][$row['category_id']]['name'] = $row['category'];
$tree[$row['section_id']]['categories'][$row['category_id']]['subcategories'][$row['subcategory_id']]['name'] = $row['subcategory'];
}
echo '<ul>';
foreach ($tree as $sec_id => $section) {
printf('<li><a href="test.php?sec=%d">%s</a><ul>', $sec_id, $section['name']);
foreach ($section['categories'] as $cat_id => $category) {
printf('<li><a href="test.php?sec=%d&cat=%d">%s</a><ul>', $sec_id, $cat_id, $category['name']);
foreach ($category['subcategories'] as $scat_id => $subcategory) {
printf('<li><a href="test.php?sec=%d&cat=%d&scat=%d">%s</a></li>', $sec_id, $cat_id, $scat_id, $subcategory['name']);
}
echo '</ul></li>';
}
echo '</ul></li>';
}
echo '</ul>';