祝大家新年快乐!新年新气象!

看了别人的一个关于无限分类的文章,自己也写了一个!(开源)

2007-05-13 17:13:44 / 个人分类:php

演示页面:http://www.zlla.cn/demo/catalog_list.php
"[4Hjb J2Y0 至于添加删除之类的功能我就不多写了!仔细看看就知道这么用了.PHPChina 开源社区门户\Q1^'[Q8O
难的是显示方面PHPChina 开源社区门户:_j9U TU"H ]&D M
希望高手扩展一下!

QUOTE:

这是类PHPChina 开源社区门户6FlI$o OsAKo
<?php
&[rrqp(_0 /*========================================================PHPChina 开源社区门户]aYNHS9A8[Z'i_
类名:catalogPHPChina 开源社区门户m7?b,g|6GOrk
功能:无限分级类
:~;B Qq9Ag0 方法:PHPChina 开源社区门户%[|-oqa ?6t
        树形显示分类PHPChina 开源社区门户%vS(iZ|t@[ FB
        catalog_show($id) //参数$id 递归调用
.sW;MM9Rn}0                 流程:找到父分类为0所有根分类-> 一直递归取得所有分类并显示        PHPChina 开源社区门户P)w2XF c5@.e0D*q1?)f#?
        添加分类
?-e1_ k ?0yI{*@0                 catalog_add($uid,$name) //$uid 父id  //$name 分类名                PHPChina 开源社区门户"YoWXFK!F-^
                流程:依据$uid,在此id下添加一个新子idPHPChina 开源社区门户3g)~2Y%gcj
        删除分类
i.V.fzU3T0                 catalog_del($uid)//参数 $uid 数要删除的分类
4DUMK4?0         修改分类
4i!F4al*j"Q w0                 catalog_set($id,$name) //参数 $id 要修改的分类  //参数 $name  新的分类名
Ltdx/T \0 变量:PHPChina 开源社区门户$aKe4f6D*j1n-k
        $config                                //数据库信息-> host,user,pass,dbname
&?X }$hd0         $catalog_dbname                //分类数据库PHPChina 开源社区门户b1[u7xZ8C{M
数据库:PHPChina 开源社区门户jo)V(iW
        catalog_id                        //分类的自然序号PHPChina 开源社区门户)B;h)E%T|W
        catalog_uid                        //分类的父分类
O0c+j n3Pm~/I0         catalog_name                //分类名
|M`l'MYH0         catalog_path_number        //亲缘树数字形式  0:1:2PHPChina 开源社区门户iDBJ;mj+n6h$eG
        catalog_path_char        //亲缘树字符形式  分类1:分类1.1:分类1.1.1
4N t$r W.X;qIf,p0 参照文章 http://www.phpchina.com/12823/viewspace_4468.html
4s?*y6{/N)JG0 ========================================================*/PHPChina 开源社区门户4YU uh Hf X%ZM
class catalog{PHPChina 开源社区门户M J~!a3c7vkz2?.?
        var $config;PHPChina 开源社区门户XX w`GN$Z*`
        var $catalog_dbname;
8|dh@%J0         var $links;
:]xU/__7bsc0         private function connect(){PHPChina 开源社区门户u+P&Een4cN
                $this->links = mysql_connect($this->config['host'],$this->config['user'],$this->config['pass']) or die("错误: 第".__LINE__."行<br>".mysql_error());
tg z+Y7[7`5{U+?"e0                 mysql_select_db($this->config['dbname'],$this->links);
^_V~8uUC$e7[&y,H0                 mysql_query("SET NAMES gb2312");PHPChina 开源社区门户/E hP ]eE#}'g
        }
F6Z@L&Z&iIj0        
3C8g%_z `4U0         function catalog_show($uid = 0){PHPChina 开源社区门户6p%R-j.t PT^[.tX5]
                $this->connect();PHPChina 开源社区门户0yg1s1A%S
                $sql = "SELECT * FROM ".$this->catalog_dbname. "
$],M r&af i0                                         WHERE catalog_uid = ". $uid ."
e [Wb3iUy0                                                  ORDER BY catalog_id ";PHPChina 开源社区门户.K/D%x7NJ%dg
                $result = mysql_query($sql,$this->links) or die("错误: 第".__LINE__."行<br>".mysql_error());
GvGT J2mH0\N0                
Pn4UF)Hf0                 if(mysql_num_rows($result) > 0){
H+TQcZ#S7L9V0                         while ($row = mysql_fetch_assoc($result)){       
$R.uP;]7V0                                 if($this->sun_catalog($row['catalog_id'])){//判断有没有子分类
&QI(^M q1Fw$Q0                                         $cata_img = "<img id = 'img".$row['catalog_id']."' src='./img/last_node.jpg' ōnclick='click_catalog(".$row['catalog_id'].")'/>";
Ya$vd4zk0                                 }else{
,j&kB[)o-T8I0                                         $cata_img = "<img src='./img/sp.jpg'/>";
g1L5sfPjM0                                 }
Hf L8E2|-GYr[&G0                 PHPChina 开源社区门户;J5`SMBR
                                $path = explode(":",$row['catalog_path_number']);
0XgQ)aF(L%Ad0                                 if(count($path) > 1){
]-T7S0{j:RM0                                         for($i=1;$i<count($path);$i++){PHPChina 开源社区门户'r!}[3EJW
                                                $path_img .= "<img src='./img/sp.jpg'/>";
h,L/B-I2[ c R!r"[|0                                         }PHPChina 开源社区门户*hl"S3tl
                                }
E@2yP[ YND)m0                                 echo $path_img.$cata_img;PHPChina 开源社区门户_A8_[ D Z#[6N9H
                                echo "<a class='menu' href = 'javascrīpt:send_id(".$row['catalog_id'].")'>";
j;q,P0V!d6S$V0                                 echo $row['catalog_name']."</a><br>";
"S}F:zCha0                                 $path_img = "";PHPChina 开源社区门户7BQ k{h(l
                                if($this->sun_catalog($row['catalog_id'])){                                       
%GYDOl`HO0                                         $hidden_div  = "style='display:none'";                                                PHPChina 开源社区门户%JlAP2{7b
                                        echo "<div id = 'div".$row['catalog_id']."' ".$hidden_div.">";                                        PHPChina 开源社区门户FoN;lQ e5M6P_Z
                                        $this->catalog_show($row['catalog_id']);PHPChina 开源社区门户(rJ g)GV.tT(DH y
                                        echo "</div>";
@T;U}o:Jm-S$r9y0                                 }                                                PHPChina 开源社区门户c_ |W_O7Y%g|z
                        }PHPChina 开源社区门户e,o4W!Mc g
                }                PHPChina 开源社区门户y;sq7N1l'`0xTk
        }PHPChina 开源社区门户v)iG%{uj&^
        private function sun_catalog($uid){//判断是否有子分类
&YN2Nu6KH0                 $sql = "SELECT * FROM ".$this->catalog_dbname. "
2g:C Ppk/x^0                                         WHERE catalog_uid = ". $uid ."
_p)zXz.[2e K0                                                  ORDER BY catalog_id ";PHPChina 开源社区门户oN%Q*KG
                $result = mysql_query($sql,$this->links) or die("错误: 第".__LINE__."行<br>".mysql_error());
R)iXs1p `NM0                 if(mysql_num_rows($result) > 0){PHPChina 开源社区门户0~%ob1\9xG2ilykP
                        return true;
H%IyIj1q'H,oYT0                 }else{
'I!k:N W {MATD3l0                         return false;
([SM8x c4B0                 }
0Es,n$o ~%S8k }^0        
!s*G w-LHZnf0         }PHPChina 开源社区门户lZ,fc|,N
        function catalog_add($uid,$name){PHPChina 开源社区门户| r!M"N-T
                //获取父id的亲缘树
^-T O R5E0                 $this->connect();
D8VnD]%A$D0                 $sql = "SELECT * FROM ".$this->catalog_dbname."PHPChina 开源社区门户*L%J\IW)FP2]
                                         WHERE catalog_id = '".$uid."'";
zZt)xTWoG5Y!{0                 $result = mysql_query($sql,$this->links)
(|kXKSK+Te0                                                 or die("错误: 第".__LINE__."行<br>".mysql_error());PHPChina 开源社区门户.mj|H}
                $row = mysql_fetch_assoc($result);PHPChina 开源社区门户X7dK"]3h-^+~.JN
                $fid_path_number = $row['catalog_path_number'];//id的数字亲缘树
Ke}J.}q1T$\6{l0                 $fid_path_char = $row['catalog_path_char'];//id的字符亲缘树PHPChina 开源社区门户*q G;T_6A$k
                //插入数据 先插入行->再找到最新插入的id, 在依据这个id进行修改PHPChina 开源社区门户;RO._)Y&p%Cv-G7]z
                $sql = "INSERT INTO ".$this->catalog_dbname."(catalog_uid,catalog_name)
2qI(eLIA0                                         VALUES(".$uid.",'".$name."')";
9?#F [ ~%^L0                 $result = mysql_query($sql,$this->links)
kYwT0wd.[0                                                 or die("错误: 第".__LINE__."行<br>".mysql_error());
k LB,J+_'y0                 $catalog_id = mysql_insert_id();//获取自己的idPHPChina 开源社区门户aT*| k$xa h:Ub|:R
                $catalog_path_number = $fid_path_number.":".$catalog_id;//得到自己的数字亲缘数
7~:W,f!t1H@\_ Y0                 $catalog_path_char = $fid_path_char.":".$name;//得到自己的字符亲缘数
9t/w)B C2c,Kjg'v0                 PHPChina 开源社区门户 M2pK\,}f#v V
                $sql = "UPDATE '".$this->catalog_dbname."'
9oM2`7A;Di9cT~1Av0                                         SET
6l}A mh,R,B l#R0                                                 catalog_path_number = '".$catalog_path_number."',PHPChina 开源社区门户9c{pBd"g.{({
                                                catalog_path_char = '".$catalog_path_char."'PHPChina 开源社区门户eD9Zp-t6qg/]d.q b&p
                                        WHERE
'j{w8L.g/Td N0                                                 catalog_id = ".$catalog_id;        PHPChina 开源社区门户fTy:Uw-?;d
                mysql_query($sql,$this->links)PHPChina 开源社区门户nN^@h]?Y
                                or die("错误: 第".__LINE__."行<br>".mysql_error());        PHPChina 开源社区门户%OB(U9@3x
                       
(NYnA](|g2U0         }PHPChina 开源社区门户O+p J-}!o1?
        PHPChina 开源社区门户'D p6T8g;fu*`
        function catalog_del($id){
1R9NB%? iP/Br`0                 $this->connect();
?+g[B'[ ~_`0                 $sql = "DELETE FROM ".$this->catalog_dbname."PHPChina 开源社区门户1V0dd"?Z$W
                                        WHERE catalog_id = ".$id;PHPChina 开源社区门户D:N ](w:Pb*y
                mysql_query($sql,$this->links)
kdiYz(g*`{$Hg0                                 or die("错误: 第".__LINE__."行<br>".mysql_error());
/G"L S0k"jrC!P8H~0         }PHPChina 开源社区门户wGA^C a*ul sV
        function catalog_set($id,$name){
p l&m*~D0                 $this->connect();PHPChina 开源社区门户Ke(j6F$v};~
                $sql = "UPDATE ".$this->catalog_dbname."PHPChina 开源社区门户L q*spi(Q3Y_
                                        SETPHPChina 开源社区门户6C'P)U6\7A
                                                catalog_name = '".$name."'PHPChina 开源社区门户j5I#^1W8`%H/Z2w
                                        WHEREPHPChina 开源社区门户_o v0a#G
                                                catalog_id = ".$id;       
Gr;b(@#@e M0                 mysql_query($sql,$this->links)PHPChina 开源社区门户)i`j)h(H uSc|1n
                                or die("错误: 第".__LINE__."行<br>".mysql_error());PHPChina 开源社区门户(K%p0a4e-LAQ t
                PHPChina 开源社区门户W\U0}[o#Qs5Z
        }PHPChina 开源社区门户F'j r[-XV {I6a
       
*b$j,vy8?7v0        
.o-h1M0k z*?x,p0 }PHPChina 开源社区门户LL c nN/^i d
?>

QUOTE:

这是dome
H ? |#[v @0 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">PHPChina 开源社区门户3E,u+l$H*o YV
<html xmlns="http://www.w3.org/1999/xhtml">PHPChina 开源社区门户Y$zP~V,J#N'x
<head>
$bq2v(zh*~@F,IJ0 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
7s~)]~/m*B0\0 <title>Untitled Document</title>
`*H]5?Kw~"h0 <scrīpt language="javascrīpt" type="text/javascrīpt">PHPChina 开源社区门户QRE Mw$s1r(nw3W
        function click_catalog(id){PHPChina 开源社区门户 Vc| r{,ac.H1D W|^U
                //alert(bottom);
Hi7Y!o1F~*O3o d0                 var div_hidden = "div"+id;
i5[e*J-IH!e4c~0                 var img_show = "img"+id;
O4Q5D!F/j%hh0                 if(getid(div_hidden).style.display == "none"){
'V2c;Y^v(~3GTs0                         getid(div_hidden).style.display = "block";                       
h5Lw0`!f ?0                         getid(img_show).src = "./img/open_node.jpg";
3LY wJTu:L7Z'`0                 }else{
.n#s%K5\H'J0                         getid(div_hidden).style.display = "none";
,IX%t%\K0bC/Lg0                         getid(img_show).src = "./img/last_node.jpg";PHPChina 开源社区门户Z)noO:?3Uf%Yy
                }PHPChina 开源社区门户p3X5NDOo7WAn~"lP
        }PHPChina 开源社区门户H&_/D3w;Q)h
        function getid(input_name){
M^!p{(t-B J0                 return document.getElementById(input_name);       
m oo#Q%Cm4|e0         }PHPChina 开源社区门户8^ H2~m`ONc
        function send_id(id){
/GA!JelJ0                 alert(id);PHPChina 开源社区门户-GG n7qNN x z3I
        }
"w;xQ+g7o0RR0 </scrīpt>
%O$Y9hLb8V3^&B4W0 <style>
P+qF5[G _0 body{
%~!x*v(AK-{ V0         BACKGROUND: #FEFEFE no-repeat right bottom;
(Ys{0Y)X6~0         FONT-FAMILY: 'Lucida Grande','Lucida Sans Unicode','宋体','新宋体',arial,verdana,sans-serif;
-hX"s _}T!y1Kl.a0         COLOR: #666;PHPChina 开源社区门户1c {YJ7I6pl'P1_
        FONT-SIZE:13px;
nbH8vtN0         LINE-HEIGHT:140%;
'A/` u!c]#Bv0         padding:0px;
'YV\;c}+r9kAi0 }
-_5L |B"p0 a.menu:hover{
rV+dj5?7y]mR0         background-color:#B5DAFB;PHPChina 开源社区门户&|ei6E8^6L!X,G
        border:1px solid #0066CC;PHPChina 开源社区门户"xx o1d"z \,sWR
}PHPChina 开源社区门户O;XLH~Q
a.menu,a.menu:visited{
WxL"W|V[I0         text-decoration : none;PHPChina 开源社区门户V\?uk]+`
        COLOR: #666;
iWtPo2k.M0 }
2~0Gc:q|T"ZV5h,X0
w Z"Vg Zo0 </style>
7R2`Cs7D-bNU-n0 </head>
SLM S1{l3Z n0 <body>PHPChina 开源社区门户 OMj2P.P^&Z
<b>分类列表</b>
gr0G:O9@0 <div>PHPChina 开源社区门户D,V!_Ddt
<?php
zn"O k(UA`6@6C0 include("./catalog.php");PHPChina 开源社区门户$i#iJ"djk&@
$catalog = new catalog;PHPChina 开源社区门户 QRc.E;|H{;L M%X
//这里自己配置数据库信息和数据库名!PHPChina 开源社区门户5O#m#\1G4Y1K f gMRt
$catalog->config = array(PHPChina 开源社区门户@g c]P
                                        'host' => '127.0.0.1',
(^%Wjq9Z0                                         'user' => 'root',PHPChina 开源社区门户%Ex f yK)cU:_
                                        'pass' => '',
\!fYAZ6en"h0                                         'dbname' =>'storage_sql'
1ko w|F0A'U5u%a0                                         );PHPChina 开源社区门户`Cv$G6m8CiG
$catalog->catalog_dbname = 't_catalog';
:y ` S"rz3V_#}u0 $catalog->catalog_show();                                        PHPChina 开源社区门户mUSup*?*h~1\Dxu
?>
[+jy3t6iGD-n0 <div>
0NvHy8F0 </body>PHPChina 开源社区门户1X-bh/FW0V)~%m
</html>

TAG: PHP php

ycl_jay 袁昌亮——当幸福来敲门 ycl_jay 发布于2008-08-12 16:03:04
就是一个递归吗  数据大了慢死了
ycl_jay 袁昌亮——当幸福来敲门 ycl_jay 发布于2008-08-12 16:01:57
ding
顶了 thankyou
kylingood的个人空间 kylingood 发布于2007-07-06 20:50:58
:lol :L
yepiaobo的个人空间 yepiaobo 发布于2007-06-30 09:00:54
我来帮楼主高亮一下。。

快乐基因的个人空间 快乐基因 发布于2007-06-29 17:05:18

QUOTE:

原帖由 welefen 于 2007-5-16 16:57 发表
9HP1~
@\JPhp China|php论坛|php教程|php mysql|php源码|php安装|php手册|php学习|zend|Zend中国|PHPChina
不错 加到CODE里 这样有语法加亮 容易阅读
www.phpchina.comi$_;?m
x5M!bV8u0B

顶上
azone的个人空间 azone 发布于2007-06-29 16:22:40
对啊,怎么不数据表结构发出来啊?这样看的很费劲!
%g/QO8}x`e代码加亮是放在php块中的!
janleo发布于2007-06-29 15:49:50
如果把表及数据发上来,就更直观了
gregry发布于2007-06-29 14:35:16
呵呵,亲缘算法的
Lni_wang的PHP技术Blog wangyl 发布于2007-05-17 17:33:59
不知道这么加亮,代码放到CODE里就加亮了??
fengyun的个人空间 fengyun 发布于2007-05-16 17:09:57
不错Php China|php论坛|php教程|php mysql|php源码|php安装|php手册|php学习|zend|Zend中国|PHPChinaeD4ae3gyh EG
:) :)
welefen welefen 发布于2007-05-16 16:57:56
不错 加到CODE里 这样有语法加亮 容易阅读
phpchina---我的开心家园 feng123 发布于2007-05-16 16:46:53
收藏了!!
gdak47lh发布于2007-05-16 16:37:21
不错 支持一下
fly1983 fly1983 发布于2007-05-13 21:45:34
支持~
Lni_wang的PHP技术Blog wangyl 发布于2007-05-13 20:55:23
呵呵!谢谢!
个人空间 coolsail 发布于2007-05-13 17:33:30
不错,支持开源.PHPChina|php论坛|Zend中国
^ K-K1[)f1~

顶一个.
zwws的个人空间 zwws 发布于2007-05-13 17:23:36
很好哦,支持一下下
PHPChina 我的成长之路 寶貝豬豬 发布于2007-05-13 17:19:00
不错,支持下
Open Toolbar