Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP/MYSQL下拉选择_Php_Mysql - Fatal编程技术网

如何使用PHP/MYSQL下拉选择

如何使用PHP/MYSQL下拉选择,php,mysql,Php,Mysql,我在下拉菜单中为打印类别提供了功能 function Cat_Parent(){ $result = mysql_query("SELECT id, name, parent FROM cats ORDER BY name"); $items = array(); while($row = mysql_fetch_array($result)) { $items[] = array('id' => $row['id'], 'label' =>

我在
下拉菜单中为打印类别提供了
功能

function Cat_Parent(){
    $result = mysql_query("SELECT id, name, parent FROM cats ORDER BY name");
    $items = array();
     while($row = mysql_fetch_array($result))
         { $items[] = array('id' => $row['id'], 'label' => $row['name'], 'parent' => $row['parent']);
     } 


// 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 name="parent[]" value="%s">%s %s</option>';
$print = function($item, $indent = '') use (&$print, $tpl)
{
    echo sprintf($tpl, $item['id'], $indent, $item['label']) . "\n";

    if(isset($item['children']))
    {
        foreach($item['children'] as $child)
        {
            $print($child, $indent . '|--');
        }
    }
};

echo '<select name="parent"><option name="parent[]" value="0">------</option>';
// Call the function for each top-level node
foreach($tree as $row)
{
    $print($row);
}
echo '</select>';
    }

在示例
$\u GET['id']
=2中,因此选择了带有
值的选项。如何选择此选项?

您将要比较模板中的值,如下所示:

$tpl = '<option name="parent[]" %s value="%s">%s %s</option>';
$print = function($item, $indent = '') use (&$print, $tpl)
{
    echo sprintf($tpl, $item['id'] == 2 ? 'selected="selected"' : '', $item['id'], $indent, $item['label']) . "\n";

    if(isset($item['children']))
    {
        foreach($item['children'] as $child)
        {
            $print($child, $indent . '|--');
        }
    }
};
$tpl='%s%s';
$print=函数($item,$indent='')使用(&$print,$tpl)
{
echo sprintf($tpl,$item['id']==2?'selected=“selected”:'',$item['id'],$indent,$item['label'])。“\n”;
如果(isset($item['children']))
{
foreach($child'项目]作为$child)
{
$print($child,$indent.|--');
}
}
};

在脚本中尝试此代码。如果GET中的值与选项值相同,则定义具有“选定”值的变量

请注意$sel变量

// Prepare a template and recursive closure (note reference on &$print)
$tpl = '<option name="parent[]"%s value="%s">%s %s</option>';
$print = function($item, $indent = '') use (&$print, $tpl)
{
    $sel = (isset($_GET['id']) && $_GET['id'] == $item['id']) ? 'selected' : ' ';
    echo sprintf($tpl, $sel, $item['id'], $indent, $item['label']) . "\n";

    if(isset($item['children']))
    {
        foreach($item['children'] as $child)
        {
            $print($child, $indent . '|--');
        }
    }
};
//准备模板和递归闭包(注意&$print上的参考)
$tpl='%s%s';
$print=函数($item,$indent='')使用(&$print,$tpl)
{
$sel=(isset($\u-GET['id'])和&$\u-GET['id']==$item['id'])?“已选”:“”;
echo sprintf($tpl、$sel、$item['id']、$indent、$item['label'])。“\n”;
如果(isset($item['children']))
{
foreach($child'项目]作为$child)
{
$print($child,$indent.|--');
}
}
};
// Prepare a template and recursive closure (note reference on &$print)
$tpl = '<option name="parent[]"%s value="%s">%s %s</option>';
$print = function($item, $indent = '') use (&$print, $tpl)
{
    $sel = (isset($_GET['id']) && $_GET['id'] == $item['id']) ? 'selected' : ' ';
    echo sprintf($tpl, $sel, $item['id'], $indent, $item['label']) . "\n";

    if(isset($item['children']))
    {
        foreach($item['children'] as $child)
        {
            $print($child, $indent . '|--');
        }
    }
};