如何使用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 . '|--');
}
}
};