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; 
}
相關文章
相關標籤/搜索