php 递归实现无限极分类和排序_php 无限极分类以及使用递归实现的排序方法

至于添加删除之类的功能我就不多写了!仔细看看就知道这么用了.

难的是显示方面

希望高手扩展一下!

这是类

代码如下:<?php

/*========================================================

类名:catalog

功能:无限分级类

方法:

树形显示分类

catalog_show($id) //参数$id 递归调用

流程:找到父分类为0所有根分类-> 一直递归取得所有分类并显示

添加分类

catalog_add($uid,$name) //$uid 父id //$name 分类名

流程:依据$uid,在此id下添加一个新子id

删除分类

catalog_del($uid)//参数 $uid 数要删除的分类

修改分类

catalog_set($id,$name) //参数 $id 要修改的分类 //参数 $name 新的分类名

变量:

$config //数据库信息-> host,user,pass,dbname

$catalog_dbname //分类数据库名

数据库:

catalog_id //分类的自然序号

catalog_uid //分类的父分类

catalog_name //分类名

catalog_path_number //亲缘树数字形式 0:1:2

catalog_path_char //亲缘树字符形式 分类1:分类1.1:分类1.1.1

========================================================*/

class catalog{

var $config;

var $catalog_dbname;

var $links;

private function connect(){

$this->links = mysql_connect($this->config['host'],$this->config['user'],$this->config['pass']) or die("错误: 第".LINE."行
".mysql_error());

mysql_select_db($this->config['dbname'],$this->links);

mysql_query("SET NAMES gb2312");

}

function catalog_show($uid = 0){

$this->connect();

$sql = "Select * FROM ".$this->catalog_dbname. "

Where catalog_uid = ". $uid ."

orDER BY catalog_id ";

$result = mysql_query($sql,$this->links) or die("错误: 第".LINE."行
".mysql_error());

if(mysql_num_rows($result) > 0){

while ($row = mysql_fetch_assoc($result)){

if($this->sun_catalog($row['catalog_id'])){//判断有没有子分类

$cata_img = "";

}else{

$cata_img = "";

}

$path = explode(":",$row['catalog_path_number']);

if(count($path) > 1){

for($i=1;$i

$path_img .= "";

}

}

echo $path_img.$cata_img;

echo "";

echo $row['catalog_name']."
";

$path_img = "";

if($this->sun_catalog($row['catalog_id'])){

$hidden_p = "style='display:none'";

echo "

";

$this->catalog_show($row['catalog_id']);

echo "

";

}

}

}

}

private function sun_catalog($uid){//判断是否有子分类

$sql = "Select * FROM ".$this->catalog_dbname. "

Where catalog_uid = ". $uid ."

orDER BY catalog_id ";

$result = mysql_query($sql,$this->links) or die("错误: 第".LINE."行
".mysql_error());

if(mysql_num_rows($result) > 0){

return true;

}else{

return false;

}

}

function catalog_add($uid,$name){

//获取父id的亲缘树

$this->connect();

$sql = "Select * FROM ".$this->catalog_dbname."

Where catalog_id = '".$uid."'";

$result = mysql_query($sql,$this->links)

or die("错误: 第".LINE."行
".mysql_error());

$row = mysql_fetch_assoc($result);

$fid_path_number = $row['catalog_path_number'];//id的数字亲缘树

$fid_path_char = $row['catalog_path_char'];//id的字符亲缘树

//插入数据 先插入行->再找到最新插入的id, 在依据这个id进行修改

$sql = "Insert INTO ".$this->catalog_dbname."(catalog_uid,catalog_name)

VALUES(".$uid.",'".$name."')";

$result = mysql_query($sql,$this->links)

or die("错误: 第".LINE."行
".mysql_error());

$catalog_id = mysql_insert_id();//获取自己的id

$catalog_path_number = $fid_path_number.":".$catalog_id;//得到自己的数字亲缘数

$catalog_path_char = $fid_path_char.":".$name;//得到自己的字符亲缘数

$sql = "Update '".$this->catalog_dbname."'

SET

catalog_path_number = '".$catalog_path_number."',

catalog_path_char = '".$catalog_path_char."'

Where

catalog_id = ".$catalog_id;

mysql_query($sql,$this->links)

or die("错误: 第".LINE."行
".mysql_error());

}

function catalog_del($id){

$this->connect();

$sql = "Delete FROM ".$this->catalog_dbname."

Where catalog_id = ".$id;

mysql_query($sql,$this->links)

or die("错误: 第".LINE."行
".mysql_error());

}

function catalog_set($id,$name){

$this->connect();

$sql = "Update ".$this->catalog_dbname."

SET

catalog_name = '".$name."'

Where

catalog_id = ".$id;

mysql_query($sql,$this->links)

or die("错误: 第".LINE."行
".mysql_error());

}

}

?>

下面主要介绍了php无限极分类递归排序实现方法,通过一个简单的递归函数实现无限递归分类排序,是非常实用的技巧,需要的朋友可以参考下function order ($array,$pid=0){

$arr = array();

foreach($array as $v){

if($v['pid']==$pid){

$arr[] = $v;

$arr = array_merge($arr,order($array,$v['id']));

}

}

return $arr;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Laravel 中,实现无限级分类可以使用递归关联来完成。具体来说,我们可以使用一个 `Category` 模型来表示分类,每个分类包含一个 `parent_id` 字段来表示其父分类的 ID。如果 `parent_id` 为 0,则表示该分类为顶级分类。然后,我们可以在 `Category` 模型中定义一个 `children` 方法来表示其子分类,如下所示: ```php class Category extends Model { public function children() { return $this->hasMany(Category::class, 'parent_id'); } } ``` 在上面的代码中,我们使用 `hasMany` 方法来定义 `Category` 模型与自身的一对多关系。具体来说,我们将 `Category` 模型作为第一个参数传递给 `hasMany` 方法,表示我们要关联的模型是 `Category`,然后将 `parent_id` 字段作为第二个参数传递给 `hasMany` 方法,表示我们要使用 `parent_id` 字段来关联两个模型。这样,我们就可以通过 `$category->children` 来获取该分类的所有子分类了。 接下来,我们可以使用递归函数来遍历无限级分类。具体来说,我们可以定义一个名为 `getTree` 的静态方法,该方法接受一个 `$parentId` 参数表示要获取的分类的父分类 ID,然后返回一个包含所有子分类分类树数组。实现代码如下: ```php class Category extends Model { public function children() { return $this->hasMany(Category::class, 'parent_id'); } public static function getTree($parentId = 0) { $categories = self::where('parent_id', $parentId)->get(); $tree = []; foreach ($categories as $category) { $tree[] = [ 'id' => $category->id, 'name' => $category->name, 'children' => self::getTree($category->id), ]; } return $tree; } } ``` 在上面的代码中,我们首先使用 `where` 方法来获取指定父分类 ID 的所有分类,然后使用 `foreach` 循环遍历这些分类,构建分类树数组。具体来说,我们将每个分类的 ID 和名称添加到数组中,并递归调用 `getTree` 方法来获取该分类的子分类。最后,我们返回分类树数组。 使用上面的代码,我们可以轻松地获取无限级分类。例如,要获取所有顶级分类及其子分类,可以这样写: ```php $categories = Category::getTree(); ``` 要获取指定分类及其子分类,可以这样写: ```php $categories = Category::getTree($categoryId); ``` 其中,`$categoryId` 表示指定分类的 ID。 需要注意的是,上面的代码虽然简单,但在处理大量分类时可能会导致性能问题。因此,建议在数据库中添加一个 `depth` 字段来表示分类的深度,然后在查询分类使用 `withDepth` 方法来预加载分类的深度,以便更高效地处理大量分类。具体来说,我们可以这样定义 `Category` 模型: ```php class Category extends Model { public function children() { return $this->hasMany(Category::class, 'parent_id'); } public function parent() { return $this->belongsTo(Category::class, 'parent_id'); } public function scopeWithDepth($query) { $table = $this->getTable(); $query->selectRaw("{$table}.*, (SELECT COUNT(*) FROM {$table} AS t WHERE t.id = {$table}.id OR t.{$this->getKeyName()} = {$table}.{$this->getKeyName()}) AS depth"); } } ``` 在上面的代码中,我们定义了一个名为 `withDepth` 的本地作用域,该作用域使用 `selectRaw` 方法来查询分类及其深度。具体来说,我们使用 `COUNT(*)` 函数来统计分类的深度,并将结果保存到 `depth` 字段中。这样,在查询分类时,我们就可以使用 `$query->withDepth()` 方法来预加载分类的深度了。例如,要获取所有分类及其深度,可以这样写: ```php $categories = Category::withDepth()->get(); ``` 获取分类深度后,我们就可以使用递归函数来遍历分类树了。具体来说,我们可以修改 `getTree` 方法,将分类深度作为第二个参数传递给该方法,然后使用 `$category->depth` 来判断分类的深度。如果分类的深度等于指定深度,则将该分类添加到分类树数组中。实现代码如下: ```php class Category extends Model { public function children() { return $this->hasMany(Category::class, 'parent_id'); } public function parent() { return $this->belongsTo(Category::class, 'parent_id'); } public function scopeWithDepth($query) { $table = $this->getTable(); $query->selectRaw("{$table}.*, (SELECT COUNT(*) FROM {$table} AS t WHERE t.id = {$table}.id OR t.{$this->getKeyName()} = {$table}.{$this->getKeyName()}) AS depth"); } public static function getTree($parentId = 0, $depth = null) { $query = self::where('parent_id', $parentId); if ($depth !== null) { $query->where('depth', $depth); } $categories = $query->get(); $tree = []; foreach ($categories as $category) { if ($depth === null || $category->depth == $depth) { $tree[] = [ 'id' => $category->id, 'name' => $category->name, 'children' => self::getTree($category->id, $depth !== null ? $depth + 1 : null), ]; } } return $tree; } } ``` 在上面的代码中,我们首先修改 `getTree` 方法,将分类深度作为第二个参数传递给该方法。然后,在查询分类时,我们使用 `where` 方法来限制分类的深度。如果指定了分类深度,则只查询指定深度的分类;否则,查询所有分类。最后,在遍历分类时,我们使用 `$category->depth` 来判断分类的深度,如果分类的深度等于指定深度,则将该分类添加到分类树数组中。 使用上面的代码,我们可以轻松地获取无限级分类。例如,要获取所有顶级分类及其子分类,可以这样写: ```php $categories = Category::getTree(); ``` 要获取指定分类及其子分类,可以这样写: ```php $categories = Category::getTree($categoryId); ``` 其中,`$categoryId` 表示指定分类的 ID。如果要获取指定深度的分类,可以将深度作为第三个参数传递给 `getTree` 方法,例如: ```php $categories = Category::getTree(0, 2); ``` 这样,就可以获取所有顶级分类及其子分类的前两级了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值