php中经常使用的几种无限分类查询技术

数据库的设计 无限分类之一 已父ID实现的无限分类mysql

//采用递归或者循环的思路
function getList($pid=0,&$result=array(),$spac=0)
{
    $spac = $spac+2;
    $sql = "select * from deepcate where pid = $pid";
    $res = mysql_query($sql);
    while($row=mysql_fetch_assoc($res))
    {
        $row['catename'] = str_repeat(' ',$spac) . '|--' . $row['catename'];
        $result[] = $row;
        getList($row['id'],$result,$spac);
    }
    return $result;
}
$rs = getList();
print_r($rs);
}

全路径无限分类

全路径无限分类技术:用一個字段記錄全部子分類id

全路径无限分类的优势在于 不须要递归 关键SQL语句是: $sql = 'select id,name,path,concat(path,"-",id) as fulpath from goods order by fulpath asc';sql

function likedate($path="")
{
    $sql ="select id,name,path,concat(path,"-",id) as fulpath from goods order by fulpath 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; 
}
相关文章
相关标签/搜索