像这样的东西可以解决问题:
function display_children($parent, $level) {
$result = mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
$indent = str_repeat('-', $level);
$tpl = '<option value="%s">%s</option>';
while ($row = mysql_fetch_assoc($result)) {
echo sprintf($tpl, $row['link'], $indent . $row['label']);
if ($row['Count'] > 0) {
display_children($row['id'], $level + 1);
}
}
}
我认为你也可以稍微简化你的查询
$result = mysql_query("SELECT a.id, a.label, a.link, (SELECT COUNT(id) FROM `menu` WHERE parent = a.id) AS Count FROM `menu` a WHERE a.parent=" . $parent);
重做示例
以下将允许您通过 1 个查询完成整个过程。由于闭包,它使用 PHP 5.3。
// I assume this is how your data comes out of MySQL
$items = array(
array('id' => 1, 'label' => 'Item One', 'link' => 'Link One', 'parent' => 0),
array('id' => 2, 'label' => 'Child One', 'link' => 'Link Two', 'parent' => 1),
array('id' => 3, 'label' => 'Child Two', 'link' => 'Link Three', 'parent' => 1),
array('id' => 4, 'label' => 'Item Two', 'link' => 'Link Four', 'parent' => 0),
array('id' => 5, 'label' => 'Child One', 'link' => 'Link Five', 'parent' => 4),
array('id' => 6, 'label' => 'Child Two', 'link' => 'Link Six', 'parent' => 4),
array('id' => 7, 'label' => 'Child Three', 'link' => 'Link Six', 'parent' => 6),
array('id' => 8, 'label' => 'Child Four', 'link' => 'Link Six', 'parent' => 6),
);
// Loop using references to build a tree
$childs = array();
foreach($items as &$item)
{
$childs[$item['parent']][] = &$item;
}
unset($item);
foreach($items as &$item)
{
if(isset($childs[$item['id']]))
{
$item['children'] = $childs[$item['id']];
}
}
// We now have a tree with 'children' key set on each node that has children
$tree = $childs[0];
// Prepare a template and recursive closure (note reference on &$print)
$tpl = '<option value="%s">%s %s</option>';
$print = function($item, $indent = '') use (&$print, $tpl)
{
echo sprintf($tpl, $item['link'], $indent, $item['label']) . "\n";
if(isset($item['children']))
{
foreach($item['children'] as $child)
{
$print($child, $indent . '-');
}
}
};
echo '<select onchange="showProduct(this.value);">';
// Call the function for each top-level node
foreach($tree as $row)
{
$print($row);
}
echo '</select>';
/*
<select onchange="showProduct(this.value);">
<option value="Link One"> Item One</option>
<option value="Link Two">- Child One</option>
<option value="Link Three">- Child Two</option>
<option value="Link Four"> Item Two</option>
<option value="Link Five">- Child One</option>
<option value="Link Six">- Child Two</option>
<option value="Link Six">-- Child Three</option>
<option value="Link Six">-- Child Four</option>
</select>
*/