數據庫的設計 無限分類之一 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); }
全路徑無限分類的優勢在於 不須要遞歸 關鍵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; }