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

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

上一篇 / 下一篇  2007-05-13 17:13:44 / 个人分类:php

查看( 4783 ) / 评论( 18 )
演示页面:http://www.zlla.cn/demo/catalog_list.phpPHPChina 开源社区门户mAS C-]}&S-A
至于添加删除之类的功能我就不多写了!仔细看看就知道这么用了.PHPChina 开源社区门户Y4dx q5gf5N Xd t
难的是显示方面PHPChina 开源社区门户)XD]fH:B
希望高手扩展一下!

QUOTE:PHPChina 开源社区门户j W!Q5pDV/S

这是类
FFM3_j)]Im"u0<?php
/X@ gV-? R0/*========================================================
,Jf+H&mWpos-B;?0类名:catalog
M\O)e1NB:H d0功能:无限分级类PHPChina 开源社区门户~1?VW`
方法:
mQU|B2`C1HK0        树形显示分类PHPChina 开源社区门户nJ^](T-^C\
        catalog_show($id) //参数$id 递归调用
#PKF\znFD'U`s0                流程:找到父分类为0所有根分类-> 一直递归取得所有分类并显示       PHPChina 开源社区门户ePi8t;zNQp(P0Q
        添加分类PHPChina 开源社区门户%_s#TbdM6r
                catalog_add($uid,$name) //$uid 父id  //$name 分类名               
ti/U'zK7`!A0                流程:依据$uid,在此id下添加一个新子id
K/b? T0i*mi-i$aB0        删除分类PHPChina 开源社区门户 W+P6{f-w.t
                catalog_del($uid)//参数 $uid 数要删除的分类
[!q!I1cT${ h0        修改分类PHPChina 开源社区门户iio'sdY
                catalog_set($id,$name) //参数 $id 要修改的分类  //参数 $name  新的分类名
(E)`5p,[5CN6Y;?4~+[&N0q0变量:PHPChina 开源社区门户!|[e8e!Y6i
        $config                                //数据库信息-> host,user,pass,dbname
d7R-v X:@,?^0        $catalog_dbname                //分类数据库
u EkK8Kh0D0数据库:PHPChina 开源社区门户J"?$Jn Qt+I;|
        catalog_id                        //分类的自然序号PHPChina 开源社区门户 _YtOG:WH`
        catalog_uid                        //分类的父分类
'Z#b D8o xx4W"aENd0        catalog_name                //分类名
-qJ U] yJ#O1G0        catalog_path_number        //亲缘树数字形式  0:1:2
~_ ` Y.wAQ0        catalog_path_char        //亲缘树字符形式  分类1:分类1.1:分类1.1.1
R*m'o"h B^E|0参照文章http://www.phpchina.com/12823/viewspace_4468.html
c3])s;}(sD0========================================================*/
]6VKk _Q0class catalog{
3a MGhk'j0        var $config;
v|)e6Xy ?(@l0        var $catalog_dbname;PHPChina 开源社区门户:qD6s"^0z7T5a
        var $links;PHPChina 开源社区门户 J+pv C0z
        private function connect(){
z5omkQW(I8]V0                $this->links = mysql_connect($this->config['host'],$this->config['user'],$this->config['pass']) or die("错误: 第".__LINE__."行<br>".mysql_error());PHPChina 开源社区门户%|4j'^i({ K } @]j
                mysql_select_db($this->config['dbname'],$this->links);PHPChina 开源社区门户-E7r4Isz:U0r
                mysql_query("SET NAMES gb2312");
5v8F]|kI2V8]\0        }
S&z*v3[%A4L-Dn3j0       PHPChina 开源社区门户]"RU l2Y t#@
        function catalog_show($uid = 0){
BD]nwM} a1p0                $this->connect();
,a]Aca+@2w(kf0                $sql = "SELECT * FROM ".$this->catalog_dbname. "
Q/@,h*J3@8L0                                        WHERE catalog_uid = ". $uid ."PHPChina 开源社区门户\w%V9Tw(H
                                                 ORDER BY catalog_id ";
*]^3J!spA s)u2\I0                $result = mysql_query($sql,$this->links) or die("错误: 第".__LINE__."行<br>".mysql_error());PHPChina 开源社区门户_#Jso4C }%`
               
N[v3m pH Q-?+C bs0                if(mysql_num_rows($result) > 0){PHPChina 开源社区门户?8ule~} O
                        while ($row = mysql_fetch_assoc($result)){       PHPChina 开源社区门户*w4@%R7LP1pp
                                if($this->sun_catalog($row['catalog_id'])){//判断有没有子分类
~0M/K\RF ]Z0                                        $cata_img = "<img id = 'img".$row['catalog_id']."' src='./img/last_node.jpg' ōnclick='click_catalog(".$row['catalog_id'].")'/>";
^6{er%O0                                }else{
HA]E?2m4v7u^;[6T0                                        $cata_img = "<img src='./img/sp.jpg'/>";
(KLU P&p%X%Q0                                }PHPChina 开源社区门户(E~h,D7@"ew/O
               
go`Vn(l2zN C0                                $path = explode(":",$row['catalog_path_number']);PHPChina 开源社区门户7D*T(J1VFY*N[
                                if(count($path) > 1){
"B'm w7yoM `\`0                                        for($i=1;$i<count($path);$i++){PHPChina 开源社区门户[Ol9GH)iP'?\)~
                                                $path_img .= "<img src='./img/sp.jpg'/>";PHPChina 开源社区门户lTK+n{P#c7X[
                                        }PHPChina 开源社区门户(P2X(`"lm:}I
                                }PHPChina 开源社区门户"^.{9L;r v"l Q_jL
                                echo $path_img.$cata_img;PHPChina 开源社区门户 Y'k"xc tp"G
                                echo "<a class='menu' href = 'javascrīpt:send_id(".$row['catalog_id'].")'>";
JB]6k1z.DJ5N7o0                                echo $row['catalog_name']."</a><br>";
JB[J3NE0                                $path_img = "";PHPChina 开源社区门户4F j P1FZ,N
                                if($this->sun_catalog($row['catalog_id'])){                                       
O4s;~0Du1HGD0                                        $hidden_div  = "style='display:none'";                                               PHPChina 开源社区门户S LIRyJ
                                        echo "<div id = 'div".$row['catalog_id']."' ".$hidden_div.">";                                       PHPChina 开源社区门户}PV2U!`s1i a `(Q0x
                                        $this->catalog_show($row['catalog_id']);
o-lj\Ig0                                        echo "</div>";
%o3[&d:\tN)ib]`x-[0                                }                                               PHPChina 开源社区门户'x{ HF(L/`re$U2|
                        }
b LS2W HllmN~-G0                }               
1{)@u4E0U$}0        }
%x Y}!rx"Y ~T0        private function sun_catalog($uid){//判断是否有子分类PHPChina 开源社区门户z#a8EFo n;l
                $sql = "SELECT * FROM ".$this->catalog_dbname. "
J#qM?#d0                                        WHERE catalog_uid = ". $uid ."
2FC_9u di'J r2l0                                                 ORDER BY catalog_id ";
UE%OMP3?V6o,W0                $result = mysql_query($sql,$this->links) or die("错误: 第".__LINE__."行<br>".mysql_error());PHPChina 开源社区门户6u LA`:VuS.E)y"W
                if(mysql_num_rows($result) > 0){PHPChina 开源社区门户$uFo"b x{
                        return true;PHPChina 开源社区门户/F,I*J r,I"?d
                }else{
"C0cV*\SN p e%Z1|0                        return false;
1LC?7_0V0                }
V+^(BMx'W"w9R)cT#B0       
4g;EAt%_:J)@ hG.M3v0        }
~ WQ+D5A|0        function catalog_add($uid,$name){PHPChina 开源社区门户3~H,} ^8mm pxe
                //获取父id的亲缘树
I0C_I.}7G ^s0                $this->connect();
f3D S#n_0                $sql = "SELECT * FROM ".$this->catalog_dbname."PHPChina 开源社区门户 U,}Yul-EXNW
                                         WHERE catalog_id = '".$uid."'";PHPChina 开源社区门户h?7VM"Z9q ^;|
                $result = mysql_query($sql,$this->links)
p { ]&PR!q&N0                                                or die("错误: 第".__LINE__."行<br>".mysql_error());PHPChina 开源社区门户@(P c&\#g#o
                $row = mysql_fetch_assoc($result);PHPChina 开源社区门户pt1c8_9C
                $fid_path_number = $row['catalog_path_number'];//id的数字亲缘树
@D F"n^ o3WW0                $fid_path_char = $row['catalog_path_char'];//id的字符亲缘树
D:U&U%D J!n0                //插入数据 先插入行->再找到最新插入的id, 在依据这个id进行修改PHPChina 开源社区门户3`;QK$l F A
                $sql = "INSERT INTO ".$this->catalog_dbname."(catalog_uid,catalog_name)
*M @2|d KR1D0                                        VALUES(".$uid.",'".$name."')";PHPChina 开源社区门户ECXB di)?hMdQ
                $result = mysql_query($sql,$this->links)
4qlR m+N$k0                                                or die("错误: 第".__LINE__."行<br>".mysql_error());PHPChina 开源社区门户(d+G;M%]"|.C
                $catalog_id = mysql_insert_id();//获取自己的id
q2?RFN#B~+fB0                $catalog_path_number = $fid_path_number.":".$catalog_id;//得到自己的数字亲缘数PHPChina 开源社区门户A%p7z$PODi
                $catalog_path_char = $fid_path_char.":".$name;//得到自己的字符亲缘数
`&r*]@&wr0               
;zb0v}:F cOdN9F)V(NA0                $sql = "UPDATE '".$this->catalog_dbname."'
$X(a1NX1I$XM(T!^ Ls0                                        SET
mw-sO'gF5y RW"kf0                                                catalog_path_number = '".$catalog_path_number."',
V4KS-wh:t0                                                catalog_path_char = '".$catalog_path_char."'
\r"cp$YPrlpi u0                                        WHERE
N%cm^6]&~Od-A0                                                catalog_id = ".$catalog_id;       
GF2]2q6F%P9h M0                mysql_query($sql,$this->links)
/W/T V Pe9k;d0                                or die("错误: 第".__LINE__."行<br>".mysql_error());       PHPChina 开源社区门户B1K0~+i l$l FN
                       PHPChina 开源社区门户2[1HX3@L3|
        }
@:n+Pg H,r M0       
y"NmC#N0        function catalog_del($id){PHPChina 开源社区门户/S4MJ'{]
                $this->connect();
o-\ o6?w {n z6}0                $sql = "DELETE FROM ".$this->catalog_dbname."PHPChina 开源社区门户?2c$p_H ?
                                        WHERE catalog_id = ".$id;PHPChina 开源社区门户,E%Xs M ]
                mysql_query($sql,$this->links)PHPChina 开源社区门户:mJ*v&e?^G7sM
                                or die("错误: 第".__LINE__."行<br>".mysql_error());
8e!@6s ``%l6G'_0        }PHPChina 开源社区门户DM@D"b
        function catalog_set($id,$name){PHPChina 开源社区门户.YMi%c*km-b~n4H
                $this->connect();
;k4q8vto0|p!^h!B0                $sql = "UPDATE ".$this->catalog_dbname."
s:T$eVx"^l0                                        SETPHPChina 开源社区门户yL^`1]E?5xG Kx
                                                catalog_name = '".$name."'
&o Z2M.P)D5l$x3@\0                                        WHEREPHPChina 开源社区门户4q.JY U"Vj|
                                                catalog_id = ".$id;       PHPChina 开源社区门户J'H }7qNg
                mysql_query($sql,$this->links)
N3J4o z$D0D0B0                                or die("错误: 第".__LINE__."行<br>".mysql_error());PHPChina 开源社区门户$Kx~{T!s)b
               PHPChina 开源社区门户g0] Haqmg)T.s1}B
        }PHPChina 开源社区门户~b0b#vok wZ#\
       
iD"z@$U0       
j4XH,u-O?0}PHPChina 开源社区门户,[-^RN;v`
?>

QUOTE:

f*E x2d5BF;k0
这是dome
XC ]/CS_N9NafE0<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
"c-fB3qE,F CZ0<html xmlns="http://www.w3.org/1999/xhtml">
Y| j+A7I L)F0<head>PHPChina 开源社区门户am6`0o+]N/v
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
BgRAI4t[0<title>Untitled Document</title>
IZncB&lao&e0<scrīpt language="javascrīpt" type="text/javascrīpt">PHPChina 开源社区门户VV9P(Bb\
        function click_catalog(id){
5x,A2U|*| c Vu0                //alert(bottom);PHPChina 开源社区门户 c RK2u T
                var div_hidden = "div"+id;
/|Ms(K P6r3[0                var img_show = "img"+id;PHPChina 开源社区门户/V Te/F;T#x9m
                if(getid(div_hidden).style.display == "none"){
sG!syz _m0                        getid(div_hidden).style.display = "block";                       
7t1G%K5z}!s,wFW-joh0                        getid(img_show).src = "./img/open_node.jpg";PHPChina 开源社区门户I!~;Z/^SV,v K,p
                }else{PHPChina 开源社区门户h8O G/`+WQ^
                        getid(div_hidden).style.display = "none";
FBilP;J,r4i0                        getid(img_show).src = "./img/last_node.jpg";
,nT:mq7fd4U(X0                }
/ApDM Jj c)l uG0        }
j,O5]S9CU(K0        function getid(input_name){
A.ha7C@0                return document.getElementById(input_name);       PHPChina 开源社区门户cy y.z4A"r.RI
        }
eB9UQ0s1P^0        function send_id(id){
0e(x0_ _ F0                alert(id);
I/\ i7ch5}[0        }
b G-bwm,k3a0</scrīpt>PHPChina 开源社区门户;?ML&IsA
<style>
7UD"\6t"s.O7`0body{
2D^} ZQYy[0        BACKGROUND: #FEFEFE no-repeat right bottom;
?d fw cX HL0        FONT-FAMILY: 'Lucida Grande','Lucida Sans Unicode','宋体','新宋体',arial,verdana,sans-serif;PHPChina 开源社区门户3E'h*h'w r2l
        COLOR: #666;
;r{0_ W yA M0        FONT-SIZE:13px;PHPChina 开源社区门户&V0I"b A^
        LINE-HEIGHT:140%;PHPChina 开源社区门户RV\ M L*y
        padding:0px;PHPChina 开源社区门户Wh5~:DT H-Eo
}PHPChina 开源社区门户@Q h7H&Zj1l7@1G;U
a.menu:hover{
.xn"x%\H6x+C!g3u0        background-color:#B5DAFB;
7e1NC T;G&eL0        border:1px solid #0066CC;
1SK UEe\%o)j0}PHPChina 开源社区门户u#?:]'I? z
a.menu,a.menu:visited{
`#x'U8{)py0        text-decoration : none;PHPChina 开源社区门户v2z,`&iI XKrDAZ
        COLOR: #666;
X _;den3| I0}
7xVz\go0
:D i"Jfi+s0</style>
SkWnTUo'o0</head>
2I] y2D ^}3l s'\v0<body>PHPChina 开源社区门户vrz T#qQ@$Q+s
<b>分类列表</b>PHPChina 开源社区门户#A`t.l&n;a&EM
<div>PHPChina 开源社区门户5Rs?/SL-N#a
<?phpPHPChina 开源社区门户zj^'s+B
include("./catalog.php");PHPChina 开源社区门户yN0w#}!|3iMj
$catalog = new catalog;
k"CeM*g5T0V0//这里自己配置数据库信息和数据库名!PHPChina 开源社区门户} Y4I T5s6i8pN.]
$catalog->config = array(PHPChina 开源社区门户 dS7B:q4m
                                        'host' => '127.0.0.1',PHPChina 开源社区门户c Ii;` JJPk
                                        'user' => 'root',
Ei$lgd,x:al!Z9U;H0                                        'pass' => '',
]Q^_*ydV2i0                                        'dbname' =>'storage_sql'
5X#}GO9J-Q*}f0                                        );
A!R e N fI G L2l0$catalog->catalog_dbname = 't_catalog';
|aAM.R'I1Vz!x U0$catalog->catalog_show();                                       
5r c(q3LL6N1I0?>PHPChina 开源社区门户 ]} escA~8l6Q
<div>
:| R_)} bK0</body>PHPChina 开源社区门户JOK?,ZJ
</html>

TAG: PHP php

PHPChina 我的成长之路 寶貝豬豬 发布于2007-05-13 17:19:00
不错,支持下
zwws的个人空间 zwws 发布于2007-05-13 17:23:36
很好哦,支持一下下
个人空间 coolsail 发布于2007-05-13 17:33:30
不错,支持开源.PHPChina|php论坛|Zend中国&z|2],y,\"J0Z
顶一个.
Lni_wang的PHP技术Blog wangyl 发布于2007-05-13 20:55:23
呵呵!谢谢!
fly1983 fly1983 发布于2007-05-13 21:45:34
支持~
gdak47lh发布于2007-05-16 16:37:21
不错 支持一下
phpchina---我的开心家园 feng123 发布于2007-05-16 16:46:53
收藏了!!
welefen welefen 发布于2007-05-16 16:57:56
不错 加到CODE里 这样有语法加亮 容易阅读
fengyun的个人空间 fengyun 发布于2007-05-16 17:09:57
不错

F-l}xMPHPChina|php论坛|Zend中国
:) :)
Lni_wang的PHP技术Blog wangyl 发布于2007-05-17 17:33:59
不知道这么加亮,代码放到CODE里就加亮了??
gregry发布于2007-06-29 14:35:16
呵呵,亲缘算法的
janleo发布于2007-06-29 15:49:50
如果把表及数据发上来,就更直观了
azone的个人空间 azone 发布于2007-06-29 16:22:40
对啊,怎么不数据表结构发出来啊?这样看的很费劲!

a1MzqcDy)jPhp China|php论坛|php教程|php mysql|php源码|php安装|php手册|php学习|zend|Zend中国|PHPChina
代码加亮是放在php块中的!
快乐基因的个人空间 快乐基因 发布于2007-06-29 17:05:18

QUOTE:

原帖由 welefen 于 2007-5-16 16:57 发表
9zy$C
_}-r;EtFwww.phpchina.com
不错 加到CODE里 这样有语法加亮 容易阅读
PHPChina 开源社区门户2[5ct.c
v/cg&}!k8o

顶上
yepiaobo的个人空间 yepiaobo 发布于2007-06-30 09:00:54
我来帮楼主高亮一下。。

kylingood的个人空间 kylingood 发布于2007-07-06 20:50:58
:lol :L
ycl_jay 袁昌亮——当幸福来敲门 ycl_jay 发布于2008-08-12 16:01:57
ding
顶了 thankyou
ycl_jay 袁昌亮——当幸福来敲门 ycl_jay 发布于2008-08-12 16:03:04
就是一个递归吗  数据大了慢死了
我来说两句

(可选)

数据统计

  • 访问量: 10196
  • 日志数: 51
  • 图片数: 3
  • 书签数: 3
  • 建立时间: 2007-02-07
  • 更新时间: 2008-02-27

RSS订阅

Open Toolbar