Selectbox multilevel



我当前正在制作产品页面。我需要一个selectbox显示多级别在何处添加产品的类别。需要(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);

返工示例

以下内容允许您使用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>
*/

如果您真正想要一个选择框,则只需要用选项替换select和li标签的UL标签即可。

修改后的PHP:

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 '<select onchange="showProduct(this.value);">';
    while ($row = mysql_fetch_assoc($result)) {
        if ($row['Count'] > 0) {
            echo "<option value='" . $row['link'] . "'>" . $row['label'];
            display_children($row['id'], $level + 1);
            echo "</option>";
        } elseif ($row['Count']==0) {
            echo "<option value='" . $row['link'] . "'>" . $row['label'] . "</option>";
        } else;
    }
    echo "</select>";
}
display_children(0, 1);

唯一的问题是链接在选择框中无法工作。我建议您考虑使用Select和JavaScript的OnChange属性在选择框中选择项目时处理逻辑。

JavaScript:

function showProduct(productLink) {
    //do something with the product value
    // maybe something like...
    location.href = productLink;
}

我认为您可能正在寻找<optgroup>,我从未使用过多个层来使用它,但值得一试。

http://www.htmldog.com/reference/htmltags/optgroup/

最新更新