登录  /  注册

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

怪我咯
发布: 2017-07-10 14:09:33
原创
1334人浏览过

至于添加删除之类的功能我就不多写了!仔细看看就知道这么用了. 难的是显示方面 希望高手扩展一下! 这是类

 代码如下:

<?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[&#39;host&#39;],$this->config[&#39;user&#39;],$this->config[&#39;pass&#39;]) or die("错误: 第".LINE."行<br>".mysql_error()); 
mysql_select_db($this->config[&#39;dbname&#39;],$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."行<br>".mysql_error()); 
if(mysql_num_rows($result) > 0){ 
while ($row = mysql_fetch_assoc($result)){   
if($this->sun_catalog($row[&#39;catalog_id&#39;])){//判断有没有子分类 
$cata_img = "<img  id = &#39;img".$row[&#39;catalog_id&#39;]."&#39; src=&#39;./img/last_node.jpg&#39; ōnclick=&#39;click_catalog(".$row[&#39;catalog_id&#39;].")&#39;/ alt="php 无限极分类以及使用递归实现的排序方法" >"; 
}else{ 
$cata_img = "<img  src=&#39;./img/sp.jpg&#39;/ alt="php 无限极分类以及使用递归实现的排序方法" >"; 
} 
$path = explode(":",$row[&#39;catalog_path_number&#39;]); 
if(count($path) > 1){ 
for($i=1;$i<count($path);$i++){ 
$path_img .= "<img  src=&#39;./img/sp.jpg&#39;/ alt="php 无限极分类以及使用递归实现的排序方法" >"; 
} 
} 
echo $path_img.$cata_img; 
echo "<a class=&#39;menu&#39; href = &#39;javascrīpt:send_id(".$row[&#39;catalog_id&#39;].")&#39;>"; 
echo $row[&#39;catalog_name&#39;]."</a><br>"; 
$path_img = ""; 
if($this->sun_catalog($row[&#39;catalog_id&#39;])){   
$hidden_p = "style=&#39;display:none&#39;";   
echo "<p id = &#39;p".$row[&#39;catalog_id&#39;]."&#39; ".$hidden_p.">";   
$this->catalog_show($row[&#39;catalog_id&#39;]); 
echo "</p>"; 
}   
} 
}   
} 
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."行<br>".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 = &#39;".$uid."&#39;"; 
$result = mysql_query($sql,$this->links) 
or die("错误: 第".LINE."行<br>".mysql_error()); 
$row = mysql_fetch_assoc($result); 
$fid_path_number = $row[&#39;catalog_path_number&#39;];//id的数字亲缘树 
$fid_path_char = $row[&#39;catalog_path_char&#39;];//id的字符亲缘树 
//插入数据 先插入行->再找到最新插入的id, 在依据这个id进行修改 
$sql = "Insert INTO ".$this->catalog_dbname."(catalog_uid,catalog_name) 
VALUES(".$uid.",&#39;".$name."&#39;)"; 
$result = mysql_query($sql,$this->links) 
or die("错误: 第".LINE."行<br>".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 &#39;".$this->catalog_dbname."&#39; 
SET 
catalog_path_number = &#39;".$catalog_path_number."&#39;, 
catalog_path_char = &#39;".$catalog_path_char."&#39; 
Where 
catalog_id = ".$catalog_id;   
mysql_query($sql,$this->links) 
or die("错误: 第".LINE."行<br>".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."行<br>".mysql_error()); 
} 
function catalog_set($id,$name){ 
$this->connect(); 
$sql = "Update ".$this->catalog_dbname." 
SET 
catalog_name = &#39;".$name."&#39; 
Where 
catalog_id = ".$id;   
mysql_query($sql,$this->links) 
or die("错误: 第".LINE."行<br>".mysql_error()); 
} 
} 
?>
登录后复制

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

function order ($array,$pid=0){
    $arr = array();
        
    foreach($array as $v){
        if($v[&#39;pid&#39;]==$pid){
            $arr[] = $v;
            $arr = array_merge($arr,order($array,$v[&#39;id&#39;]));
        }
    }
    return $arr;
}
登录后复制

以上就是php 无限极分类以及使用递归实现的排序方法的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号