search
Homephp教程PHP源码php mysql实现无限级分类 树型显示分类关系

<script>ec(2);</script>
无限级分类,主要是通过储存上级分类的id以及分类路径来实现。由于数据的结构简单,所以要将分类的关系由树状显示,我只能想到用递归的方式给于实现,下面是分类数据表结构和自己写的一个树状显示函数,有什么不妥的地方希望大家能指出。
表结构:id字段为分类标识,name字段为分类名,father_id字段为所属父分类的id,path字段为分类路径(储存该分类祖先的集合),isdir判断是否是目录(1为是,0为否)。
显示函数:
//$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 {
$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);
  以上为个人思想,还望大家多多指点。by 5w台灯
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools