PHP实现无限极分类图文教程
程序员文章站
2022-06-28 23:46:05
一般来说实现无限极分类都是使用递归或者迭代的方式,小伙伴们看下本文的实现方式吧。
1,数据库设计:
2,代码:
复制代码 代码如下:/** * @...
一般来说实现无限极分类都是使用递归或者迭代的方式,小伙伴们看下本文的实现方式吧。
1,数据库设计:
2,代码:
复制代码 代码如下:
/**
* @author koma
* @todo php无限极分类
*/ $cn = mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('t', $cn) or die(mysql_error());
mysql_query('set names utf8');
/**
* 从顶层逐级向下获取子类
* @param number $pid
* @param array $lists
* @param number $deep
* @return array
*/ function getlists($pid = 0, &$lists = array(), $deep = 1) {
$sql = 'select * from category where pid='.$pid;
$res = mysql_query($sql);
while ( ($row = mysql_fetch_assoc($res)) !== false ) {
$row['catename'] = str_repeat(' ', $deep).'|---'.$row['catename'];
$lists[] = $row;
getlists($row['id'], $lists, ++$deep); //进入子类之前深度+1 --$deep; //从子类退出之后深度-1 }
return $lists;
}
function displaylists($pid = 0, $selectid = 1) {
$result = getlists($pid);
$str = '<select>';
foreach ( $result as $item ) {
$selected = "";
if ( $selectid == $item['id'] ) {
$selected = 'selected';
}
$str .= '<option '.$selected.'>'.$item['catename'].'</option>';
}
return $str .= '</select>';
} /**
* 从子类开始逐级向上获取其父类
* @param number $cid
* @param array $category
* @return array:
*/ function getcategory($cid, &$category = array()) {
$sql = 'select * from category where id='.$cid.' limit 1';
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if ( $row ) {
$category[] = $row;
getcategory($row['pid'], $category);
}
krsort($category); //逆序,达到从父类到子类的效果 return $category;
}
function displaycategory($cid) {
$result = getcategory($cid);
$str = "";
foreach ( $result as $item ) {
$str .= '<a href="'.$item['id'].'">'.$item['catename'].'</a>>';
}
return substr($str, 0, strlen($str) - 1);
}
echo displaylists(0, 3);
echo displaycategory(13);
* @author koma
* @todo php无限极分类
*/ $cn = mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('t', $cn) or die(mysql_error());
mysql_query('set names utf8');
/**
* 从顶层逐级向下获取子类
* @param number $pid
* @param array $lists
* @param number $deep
* @return array
*/ function getlists($pid = 0, &$lists = array(), $deep = 1) {
$sql = 'select * from category where pid='.$pid;
$res = mysql_query($sql);
while ( ($row = mysql_fetch_assoc($res)) !== false ) {
$row['catename'] = str_repeat(' ', $deep).'|---'.$row['catename'];
$lists[] = $row;
getlists($row['id'], $lists, ++$deep); //进入子类之前深度+1 --$deep; //从子类退出之后深度-1 }
return $lists;
}
function displaylists($pid = 0, $selectid = 1) {
$result = getlists($pid);
$str = '<select>';
foreach ( $result as $item ) {
$selected = "";
if ( $selectid == $item['id'] ) {
$selected = 'selected';
}
$str .= '<option '.$selected.'>'.$item['catename'].'</option>';
}
return $str .= '</select>';
} /**
* 从子类开始逐级向上获取其父类
* @param number $cid
* @param array $category
* @return array:
*/ function getcategory($cid, &$category = array()) {
$sql = 'select * from category where id='.$cid.' limit 1';
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if ( $row ) {
$category[] = $row;
getcategory($row['pid'], $category);
}
krsort($category); //逆序,达到从父类到子类的效果 return $category;
}
function displaycategory($cid) {
$result = getcategory($cid);
$str = "";
foreach ( $result as $item ) {
$str .= '<a href="'.$item['id'].'">'.$item['catename'].'</a>>';
}
return substr($str, 0, strlen($str) - 1);
}
echo displaylists(0, 3);
echo displaycategory(13);
3,效果图:
是不是很简单呢,小伙伴们可以直接拿去用哈,不收版权费^_^