Php 选择框多级

Php 选择框多级,php,mysql,drop-down-menu,Php,Mysql,Drop Down Menu,我目前正在制作一个产品页面。我需要一个显示多级的选择框 要在其中添加产品的类别。需要类似php/mysql动态的东西: <option>Workstations</option> <option>--Macs</option> <option>--Windows</option> <option>Software</option> <option>--Image editing</o

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

尊敬的Simon,我想你可能正在寻找我从未在多个层次上使用过它,但值得一试


像这样的东西可以达到目的:

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);
        }
    }
}
修改后的示例

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

如果选择框是您真正想要的,您只需将ul标签替换为select,将li标签替换为option

修改的PHP:


是否确实要使用select进行导航?这不是一种非常方便用户的方式。用户可以深入查看的列表更为典型。此外,您确实不希望使用mysql_*进行数据库交互。它几乎被弃用了。你应该看看mysqli或PDO,他可能想使用多个select?一个用于工作站,一个用于软件??请帮助我们帮助您。这是一个管理网站的页面,所以我需要一个多级选择框!!!:-你们是对的,如果它是一个正常的前端,它不会是非常不友好的用户!这样一来,您只能有一个optgroup级别,不能将optgroup嵌套在另一个optgroup中。无论哪种方式,除了IE for Mac之外,大多数用户代理都不会将optgroups呈现为子菜单,并且会显示select中的所有内容,这可能会导致非常长的、不可用的选择。我觉得不会有很多条目。我也不知道它已经停止使用了,诅咒!哇。好像我忘记解释什么了。我不需要多层次的链接。我只需要选择框,这样用户就可以选择一个类别,其中的值中有一个类别id:-我不确定如何使用您的代码。如何在修改后的示例中显示selectbox/只需在最后一个foreach blockit之前和之后回显它=什么?我想我的脑子卡住了我真的很感谢你的帮助!到目前为止,它与阵列一起工作。然后如何添加SQL?我假设我仍然需要做一个递归函数,或者当它是一个选择框时,有没有更简单的方法?$result=mysql\u querySELECT cat\u id、cat\u name、cat\u link、cat\u parent FROM categories;$items=数组;而$row=mysql\u fetch\u array$sql{$items[]=array'id'=>$data['cat\u id'],'label'=>$data['cat\u name'],'link'=>$data['cat\u link'],'parent'=>$data['cat\u parent'];}
// 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>
*/
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);
function showProduct(productLink) {
    //do something with the product value
    // maybe something like...
    location.href = productLink;
}