...
...
每一个分类都需要记录它的父级id,当为顶级分类时,父级id为0。这样无论哪个分类,都可以通过父级id一层层的去查明它所有的父级,以便清楚知道它所属何种分类,层级深度为几。
3.封装成函数
php无限分类下拉列表的代码实现
<?php
include(‘db.inc.php‘);
function getList($pid=0,&$result=array(),$spac=0){
$spac = $spac + 2;
$sql = "select * from deeploop 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;
}
function displayCate($pid=0, $selected=0){
$rs = getList($pid);
$str =‘‘;
$str .= "<select name=‘cate‘>";
foreach($rs as $key=>$val){
$selectedstr = ‘‘;
if($val[‘id‘] == $selected){
$selectedstr = "selected";
}
$str .= "<option {$selectedstr}>{$val[‘catename‘]}</option>";
}
return $str .= "</select>";
}
echo displayCate(7,2);
无限分类分类导航LINK的代码实现
<?php
include(‘db.inc.php‘);
function getCatePath($cid, &$result=array()){
$sql = "select * from deeploop where id = $cid";
$rs = mysql_query($sql);
$row = mysql_fetch_assoc($rs);
if($row){
$result[] = $row;
getCatePath($row[‘pid‘], $result);
}
krsort($result);
return $result;
}
// $rs = getCatePath(10);
// print_r($rs);
// echo "<br/>";
// foreach($rs as $key=>$val){
// echo "<a href=‘cate.php?cid={$val[‘id‘]}‘>{$val[‘catename‘]} </a> >";
// }
function displayCatePath($cid,$url="cate.php?cid="){
$res = getCatePath($cid);
$str = ‘‘;
foreach($res as $k=>$val){
$str .= "<a href=‘cate.php?cid={$val[‘id‘]}‘>{$val[‘catename‘]} </a> >";
}
return $str;
}
echo displayCatePath(10,‘cate.php?page=1&id=‘);
移动分类(不能移到自己及其子类下面)
删除分类(只能删除最底层的分类,即不含子分类的分类)
全路径无限分类的原理
1.原理:
以一个字段把它所有父级id按顺序记录下来以此来实现的无限分类叫做全路径无限分类。
优点:
查询方便
缺点:
增加、移动分类时数据维护稍显复杂
利用全路径字段(形如1,2,3,4,这样)加上ID组成新字段正序排列,然后再利用字段长度(以.分割)来计算出层级深度。
例如:
select id,catename,path,CONCAT(path,‘,‘,id) AS fullpath from likecate WHERE 1 order by fullpath asc;
全路径无限分类下拉列表的实现:
<?php
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*2).‘|--‘.$row[‘catename‘];
$result[] = $row;
}
return $result;
}
$res = likecate();
print_r($res);
echo "<br/>";
echo "<select name=‘cate‘>";
foreach($res as $key=>$val){
echo "<option>{$val[‘catename‘]}</option>";
}
echo "</select>";
全路径无限分类导航LINK代码的实现:
<?php
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*2).‘|--‘.$row[‘catename‘];
$result[] = $row;
}
return $result;
}
// $res = likecate();
// print_r($res);
// echo "<br/>";
// echo "<select name=‘cate‘>";
// foreach($res as $key=>$val){
// echo "<option>{$val[‘catename‘]}</option>";
// }
// echo "</select>";
function displayCateList(){
$res = likecate();
$str = "<select name=‘cate‘>";
foreach($res as $key=>$val){
$str .= "<option>{$val[‘catename‘]}</option>";
}
return $str .= ‘</select>‘;
}
echo displayCateList();
function getPathCate($cateid){
$sql = "select *,concat(path,‘,‘,id) as fullpath from likecate where id = $cateid";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$ids = $row[‘fullpath‘];
$sql = "select * from likecate where id in ($ids) order by id asc";
$res = mysql_query($sql);
$result = array();
while($row = mysql_fetch_assoc($res)){
$result[] = $row;
}
return $result;
}
$rs = getPathCate(4);
// print_r($rs);
function displayCatePath($cateid, $link=‘cate.php?cid=‘){
$res = getPathCate($cateid);
$str = ‘‘;
foreach($res as $k=>$v){
$str .= "<a href=‘{$link}{$v[‘id‘]}‘>{$v[‘catename‘]}</a> >";
}
return $str;
}
echo displayCatePath(4,‘cate.php?p=1&cid=‘);