欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

全路径无限分类下拉列表的实现

程序员文章站 2023-10-23 23:37:42
最终效果图: 代码详情: ......

最终效果图:

全路径无限分类下拉列表的实现

全路径无限分类下拉列表的实现

代码详情:

include('db.inc.php');
function likecate($path=''){
    sql = "select id,catename,path,concat(path,',',id) as fullpath from likecate order by fullpath asc";
    $res = mysql_query($sql);
    $result = array();
    while($row = mysql_fetch_assoc($res)){
        $deep = count(explode(',',trim($row['fullpath'],','))); //取数组的深度,去除逗号
        $row['catename'] = str_repeat('  ',$deep).'|--'.$row['catename'];
        $result[] = $row;
    }
    return $result;
}
$res = likecate();
echo "<select name= cate>";
foreach ($res as $key => $value) {
    echo "<option> {$value['catename']}</option>";
}
echo "</select>";
print_r($res);