我正在制作一个产品页面。我需要一个显示多级的选择框
在其中添加产品的类别。需要类似(php/mysql动态):

<option>Workstations</option>
<option>--Macs</option>
<option>--Windows</option>
<option>Software</option>
<option>--Image editing</option>
<option>----Pro</option>
<option>----Semi pro</option>

我的mysql字段是,cat_id,cat_name,cat_parent。
我有下面的代码,那么有没有什么方法可以定制它来使用一个安装了ul/li的selectbox呢?
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);
    echo "<ul>";
    while ($row = mysql_fetch_assoc($result)) {
        if ($row['Count'] > 0) {
            echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a>";
            display_children($row['id'], $level + 1);
            echo "</li>";
        } elseif ($row['Count']==0) {
            echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
        } else;
    }
    echo "</ul>";
}

display_children(0, 1);

你好,西蒙

最佳答案

像这样的事情可以做到:

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);

重写示例
下面将允许您用一个查询完成整个过程。由于闭包的原因,它使用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>
*/

09-25 16:49
查看更多