简体   繁体   English

如何使用PHP / MYSQL下拉选择

[英]How To Dropdown Select using PHP/MYSQL

i have this function for print categories in dropdown menu. 我有这个function在打印类别的dropdown菜单。

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>';
    }

this worked but i need to selected value/id when $_GET['id'] = value Like This: 这可行,但是当$_GET['id'] = value时,我需要选择value/id像这样:

<select>
<option value="1">cat1</option>
<option selected value="2">cat2</option>  <-- THIS Print Selected
<option value="3">subcat2</option>
<option value="4">cat3</option>
<option value="5">cat4</option>
</select>

in example $_GET['id'] = 2 , so selected option with value 2. how to select this? 在示例中$_GET['id'] = 2,因此选择的value 2的选项。如何选择呢?

Try this code in your script. 在脚本中尝试此代码。 Defines a variable with "selected" value if the value from GET is the same as the option value. 如果GET中的值与选项值相同,则使用“选定”值定义变量。

Notice the $sel variable. 注意$ 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 . '|--');
        }
    }
};

You're going to want to compare your values in the template like so: 您将要像这样比较模板中的值:

$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 . '|--');
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM