1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > php 无级分类源代码 php+mysql无限级分类程序代码

php 无级分类源代码 php+mysql无限级分类程序代码

时间:2022-02-16 04:04:25

相关推荐

php 无级分类源代码 php+mysql无限级分类程序代码

无限级分类主要就是数据库中表的存储,一个是父ID,一个是子ID通过他们来查询父级关系,然后出来我们想要的

表结构:id字段为分类标识,name字段为分类名,father_id字段为所属父分类的id,path字段为分类路径(储存该分

类祖先的集合),isdir判断是否是目录(1为是,0为否)。

例1

代码如下

复制代码

//$count为分类等级

sort_list($str,$fatherid,$count)

{

$rs = $this->sql->re_datas("select * from sort where father_id = fatherid");

$num = $this->sql->sql_numrows();

$i=0;

$n = 1;

while(isset($rs[$i]))

{

$name = "";

for($n = 1 ; $n < $count ; $n++)

{

$name.="│ ";

}

if($i+1==$num)

{

$name.="└─".$rs[$i][name];

}

else

{

$name.="├─".$rs[$i][name];

}

if($rs[$i][isdir])

{

$str.="".$name."";

}

else

{

$str.=$name";

}

$temp = $count+1;

$str = $this->sort_list($str,$rs[$i][id],$temp);

$i++;

}

return $str;

}

其中$this->sql对象为sql操作类对象,re_datas()函数返回查到的数组,sql_numrows()函数返回查询到的数目

调用方法:$sort_list = sort_list($sort_list,0,1);

实例上2

id 编号

fid 父分类编号

class_name 分类名

path 分类路径,以 id 为节点,组成类似 ,1,2,3,4, 这样的字符串

———————————————————————————-

可以假设有如下的数据

id fid class_name path

—————————————————-

1 0 分类1 , 1,

2 0 分类2 , 2,

3 1 分类1-1 , 1,3,

4 1 分类1-2 , 1,4,

5 2 分类2-1 , 2,5,

6 4 分类1-2-1 , 1,4,6,

—————————————————-

代码如下

复制代码

代码

代码如下

复制代码

$conn = mysql_connect ( 'localhost', 'root', 'root' );

mysql_select_db ( 'wanggou123', $conn );

mysql_query ( 'set names UTF8' );

$sql = "select id,concat(catpath,'-',id) as abspath,name from category order by abspath";

$query = mysql_query ( $sql );

while ( $row=mysql_fetch_array($query)) {

/**

* 第一种展示方法

*/

/*$space = str_repeat ( ' ', count ( explode ( '-', $row ['abspath'] ) ) - 1 );

echo $space . $row ['name'] . '

';*/

/**

第二种展示方法

*/

$space = str_repeat ( '——', count ( explode ( '-', $row ['abspath'] ) ) - 1 );

$option .= '' . $space . $row ['name'] . '

';

}

echo $option;

exit();

echo '' . $option . '';

发布php中文网,转载请注明出处,感谢您的尊重!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。