php 无限极导入,PHP_php 无限极分类,复制代码 代码如下:<?php /*=== - phpStudy

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

参照文章 http://www.phpchina.com/12823/viewspace_4468.html

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

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 = "last_node.jpg";

}else{

$cata_img = "sp.jpg";

}

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

if(count($path) > 1){

for($i=1;$i

$path_img .= "sp.jpg";

}

}

echo $path_img.$cata_img;

echo "";

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

$path_img = "";

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

$hidden_div = "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());

}

}

?>相关阅读:

设置对象作用域

利用SQL SERVER建立登录WINDOWS帐号

Java、C#泛型的一些个人看法

smarty实例教程

Discuz!在论坛主题中快速插入UCHome相册图片

几则JSP入门知识总结

SQL Server之数据库存档

参考:关于Javascript中实现暂停的几篇文章

Javascript(AJAX)解析XML的代码(兼容FIREFOX/IE)

javascript asp教程第九课--cookies

PHP实例教程:PHP采集程序的思路

解决asp.net ajax sys未定义的解决方法

分析CSS网页布局中易犯的10个错误

Jquery插件之打造自定义的select标签

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP中实现无限极分类可以使用递归函数来实现。以下是一个简单的示例代码: ``` function getTree($data, $pid = 0, $level = 0) { static $tree = array(); foreach($data as $key => $val) { if($val['parent_id'] == $pid) { $val['level'] = $level; $tree[] = $val; unset($data[$key]); getTree($data, $val['id'], $level + 1); } } return $tree; } ``` 这个函数接受一个多维数组、父级ID和当前层级作为参数。首先,我们定义了一个静态变量 `$tree` 用于存储树形结构。然后,我们遍历数据数组,并找出所有父级ID等于当前ID的项目。将它们添加到 `$tree` 数组中,并从原数组中删除。然后,我们递归调用 `getTree()` 函数,将当前ID作为父级ID,并将层级加1。最后,返回 `$tree` 数组。 使用示例: ``` $data = array( array('id' => 1, 'parent_id' => 0, 'name' => 'A'), array('id' => 2, 'parent_id' => 1, 'name' => 'B'), array('id' => 3, 'parent_id' => 1, 'name' => 'C'), array('id' => 4, 'parent_id' => 2, 'name' => 'D'), array('id' => 5, 'parent_id' => 3, 'name' => 'E'), ); $tree = getTree($data); print_r($tree); ``` 输出结果如下: ``` Array ( [0] => Array ( [id] => 1 [parent_id] => 0 [name] => A [level] => 0 ) [1] => Array ( [id] => 2 [parent_id] => 1 [name] => B [level] => 1 ) [2] => Array ( [id] => 4 [parent_id] => 2 [name] => D [level] => 2 ) [3] => Array ( [id] => 3 [parent_id] => 1 [name] => C [level] => 1 ) [4] => Array ( [id] => 5 [parent_id] => 3 [name] => E [level] => 2 ) ) ``` 这里我们使用一个简单的数组作为示例数据,结果返回了树形结构数组,其中每个项目包含其层级信息。你可以根据自己的需要进行调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值