php自定义函数之递归实现无限分类生成下拉列表

/*—————————————————— */
//– 递归实现无限分类生成下拉列表函数
//– $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(‘&nbsp;’,$n*3).$row['sort_name'].”</option>\n”;
$options .=createSortOptions ($selected,$row['sort_id'],$n);
}
}
return $options;
}


Leave Your Comment

Your email will not be published or shared. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">

*
请输入图片中的字符以验证你并非垃圾机器人. 点击图片收听验证码的语音版.
点击这里收听此验证码的语音版本