php 无限极分类_php教程-查字典教程网
php 无限极分类
php 无限极分类
发布时间:2016-12-29 来源:查字典编辑
摘要:复制代码代码如下:

复制代码 代码如下:

<?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

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

classcatalog{

var$config;

var$catalog_dbname;

var$links;

privatefunctionconnect(){

$this->links=mysql_connect($this->config['host'],$this->config['user'],$this->config['pass'])ordie("错误:第".__LINE__."行<br>".mysql_error());

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

mysql_query("SETNAMESgb2312");

}

functioncatalog_show($uid=0){

$this->connect();

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

Wherecatalog_uid=".$uid."

orDERBYcatalog_id";

$result=mysql_query($sql,$this->links)ordie("错误:第".__LINE__."行<br>".mysql_error());

if(mysql_num_rows($result)>0){

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

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

$cata_img="<imgid='img".$row['catalog_id']."'src='./img/last_node.jpg'ōnclick='click_catalog(".$row['catalog_id'].")'/>";

}else{

$cata_img="<imgsrc='./img/sp.jpg'/>";

}

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

if(count($path)>1){

for($i=1;$i<count($path);$i++){

$path_img.="<imgsrc='./img/sp.jpg'/>";

}

}

echo$path_img.$cata_img;

echo"<aclass='menu'href='javascrīpt:send_id(".$row['catalog_id'].")'>";

echo$row['catalog_name']."</a><br>";

$path_img="";

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

$hidden_div="";

echo"<divid='div".$row['catalog_id']."'".$hidden_div.">";

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

echo"</div>";

}

}

}

}

privatefunctionsun_catalog($uid){//判断是否有子分类

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

Wherecatalog_uid=".$uid."

orDERBYcatalog_id";

$result=mysql_query($sql,$this->links)ordie("错误:第".__LINE__."行<br>".mysql_error());

if(mysql_num_rows($result)>0){

returntrue;

}else{

returnfalse;

}

}

functioncatalog_add($uid,$name){

//获取父id的亲缘树

$this->connect();

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

Wherecatalog_id='".$uid."'";

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

ordie("错误:第".__LINE__."行<br>".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="InsertINTO".$this->catalog_dbname."(catalog_uid,catalog_name)

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

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

ordie("错误:第".__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'".$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)

ordie("错误:第".__LINE__."行<br>".mysql_error());

}

functioncatalog_del($id){

$this->connect();

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

Wherecatalog_id=".$id;

mysql_query($sql,$this->links)

ordie("错误:第".__LINE__."行<br>".mysql_error());

}

functioncatalog_set($id,$name){

$this->connect();

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

SET

catalog_name='".$name."'

Where

catalog_id=".$id;

mysql_query($sql,$this->links)

ordie("错误:第".__LINE__."行<br>".mysql_error());

}

}

?>

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新php教程学习
    热门php教程学习
    编程开发子分类