php递归实现无限分类生成下拉列表的函数
(编辑:jimmy 日期: 2024/11/20 浏览:3 次 )
复制代码 代码如下:
/*—————————————————— */
//– 递归实现无限分类生成下拉列表函数
//– $tpl->assign('sort_list',createSortOptions ());
//– $tpl->assign('sort_list',createSortOptions ($sort_id));
/*—————————————————— */
function createSortOptions ($selected=0,$parent_id=0,$n=-1)
{
global $db;
$sql = "SELECT * FROM `@__article_sort` WHERE `parent_id` = '{$parent_id}'";
$options = ";
static $i = 0;
if ($i == 0)
{
$options .= '<option value="0″ >请选择</option>';
}
$res = $db->query ($sql);
if ($res)
{
$n++;
while ($row = $db->fetch_assoc ($res))
{
$i++;
$options .="<option value='{$row['sort_id']}'";
if ($row['sort_id'] == $selected)
{
$options .=' selected ';
}
$options .=">".str_repeat(' ',$n*3).$row['sort_name']."</option>\n";
$options .=createSortOptions ($selected,$row['sort_id'],$n);
}
}
return $options;
}
/*—————————————————— */
//– 递归实现无限分类生成下拉列表函数
//– $tpl->assign('sort_list',createSortOptions ());
//– $tpl->assign('sort_list',createSortOptions ($sort_id));
/*—————————————————— */
function createSortOptions ($selected=0,$parent_id=0,$n=-1)
{
global $db;
$sql = "SELECT * FROM `@__article_sort` WHERE `parent_id` = '{$parent_id}'";
$options = ";
static $i = 0;
if ($i == 0)
{
$options .= '<option value="0″ >请选择</option>';
}
$res = $db->query ($sql);
if ($res)
{
$n++;
while ($row = $db->fetch_assoc ($res))
{
$i++;
$options .="<option value='{$row['sort_id']}'";
if ($row['sort_id'] == $selected)
{
$options .=' selected ';
}
$options .=">".str_repeat(' ',$n*3).$row['sort_name']."</option>\n";
$options .=createSortOptions ($selected,$row['sort_id'],$n);
}
}
return $options;
}
下一篇:php用数组返回无限分类的列表数据的代码