1

我目前正在制作产品页面。我需要一个选择框,显示在其中添加产品的多级类别。需要类似(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 的选择框?

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

问候,西蒙

4

3 回答 3

1

如果一个选择框是你真正想要的,你只需要用 select 替换 ul 标签,用 option 替换 li 标签。

修改后的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 的 onchange 属性,并在从选择框中选择项目时使用 javascript 来处理逻辑。

Javascript:

function showProduct(productLink) {
    //do something with the product value
    // maybe something like...
    location.href = productLink;
}
于 2012-10-23T20:31:33.387 回答
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>
*/
于 2012-10-23T20:23:06.177 回答
0

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

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

于 2012-10-23T20:15:21.577 回答