数据库的设计 无限分类之一 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; }