用PHP做商品无限级的分类程序_第1页
用PHP做商品无限级的分类程序_第2页
用PHP做商品无限级的分类程序_第3页
用PHP做商品无限级的分类程序_第4页
用PHP做商品无限级的分类程序_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、用PHP做商品无限级的分类程序根据动网的论坛无限级的分类,特开发了PHP版的无限级的分类.即然是PHP,数据表当然是 MYSQL:在应用之前,先在mysql中建立数据表.b_mtype.其中的字段包括:typeid,typename,parentid,paretnstr,rootid,child,orders.具体PHP程序如下:<?php $ToDo=$_GET"ToDo" switch($ToDo) case "add": echo add(); break; case "edit": echo edit(); break;

2、 case "saveadd": echo saveadd(); break; case "saveedit": echo saveedit(); break; case "del": echo del(); break; default:?><table width="96%" border="0" align="center" cellpadding="0" cellspacing="0" class="tbl

3、border"><tr> <td valign="top"><table width="99%" border="0" cellspacing="1" cellpadding="3"> <tr> <td width="55%" height="20" align="center" bgcolor="#698FC7"><span clas

4、s="tblhead"><b>商品类别设置</b></span></td> </tr> </table> <table width="99%" bgcolor="#cccccc" cellspacing="1" cellpadding="3"> <tr bgcolor="#F9F9F9"> <td width="10%">ID</td&g

5、t; <td width="32%">类别名称</td> <td width="22%">排序</td> <td width="40%">操作</td> </tr> <?php $query=mysql_query("select * from b_mtype order by rootid,orders"); /echo $query; while($arr=mysql_fetch_array($query) ?> &

6、lt;tr> <td bgcolor="#FFFFFF"><?php echo $arr"typeid"?></td> <td bgcolor="#FFFFFF"><?php if ($arr"depth">0) for($i=1;$i<=$arr"depth"$i+) echo "&nbsp;" ?> <? if($arr"child">0) echo &q

7、uot;+"else echo "-"?> <? if($arr"parentid"=0) echo "<b>"?> <? echo $arr"typename"?> <? if ($arr"child">0) ?>(<? echo $arr"child"?>)<? ?></td> <td bgcolor="#FFFFFF"><?ph

8、p echo $arr"orders"?></td> <td bgcolor="#FFFFFF"><a href="mer_type.php?ToDo=add&editid=<?php echo $arr"typeid"?>"><U>添加版面</U></a> | <a href="mer_type.php?ToDo=edit&editid=<?php echo $arr"type

9、id"?>"><U>基本设置</U></a> | <? if($arr"child"=0)?><a href="mer_type.php?ToDo=del&editid=<? echo $arr"typeid"?>" onClick="if(confirm('删除将包括该类别的所有内容,确定删除吗?')return true;return false;"><U>删除</U

10、></a><?else?><a href="#" onClick="if(confirm('该类别含有下属类别,必须先删除其下属类别方能删除本类别!')return true;return false;"><U>删除</U></a><?>&nbsp;< /td> </tr> <?php ?> </table> </td></tr></table> <?p

11、hp break; ?><?php /增加类别function add()/ global $db,$postnum,$editid; $editid=$_REQUEST"editid" $query=mysql_query("select typeid from b_mtype order by typeid desc limit 1");while($arr=mysql_fetch_array($query)if (!$arr"typeid") $postnum=1; else $postnum=$arr"t

12、ypeid"+1; if(!$postnum) $postnum=1;?> <form name="form1" method="post" action="mer_type.php?ToDo=saveadd"> <table width="94%" align="center" border="0" cellpadding="3" cellspacing="1"> <tr bgcolor=

13、"#CCCCCC"> <td colspan="2" bgcolor="#698FC7"><span class="tblhead"><b>创建新的类别</b></span></td> </tr> <input type="hidden" name="newtypeid" value="<? echo $postnum;?>" /> <t

14、r> <td width="13%">类别名称:</td> <td width="87%"><input name="typename" type="text" id="typename"></td> </tr> <tr> <td width="52%" height=30 bgcolor="#FFFFFF"><U>所属类别</U>

15、</td> <td width="48%" bgcolor="#FFFFFF"> <select name=btype> <option value="0">做为主类别</option> <? $query=mysql_query("select * from b_mtype order by rootid,orders"); while($arr=mysql_fetch_array($query) ?> <option value=&q

16、uot;<? echo $arr"typeid"?>" <?php if($editid = $arr'typeid') echo " selected" ?>> <? if($arr"depth">0) for($i=1;$i<=$arr"depth"$i+) echo "-" ?> <? echo $arr"typename"?></option> <? ?>

17、; </select> </td> </tr> <tr> <td>&nbsp;</td> <td><input type="submit" name="Submit" value="保存"></td> </tr> </table></form><? ?><?php /保存增加的类别function saveadd()$ntid =$_REQUEST"newt

18、ypeid"$tn =$_REQUEST"typename"$btype =$_REQUEST"btype"/echo $btype;if ($ntid="" or $tn="")die( "参数有误,请重新填写." );if ($btype!=0) $result=mysql_query("select rootid,typeid,depth,orders,parentstr from b_mtype where typeid='$btype'")

19、; $aa=mysql_fetch_array($result); $rootid=$aa'rootid' /echo "aaaaaaaaaaa" $parentid=$aa'typeid' $depth=$aa'depth' $orders=$aa'orders' $parentstr=$aa'parentstr' /echo $rootid; if($aa"depth"+1)>20) die("本分类限制最多只能有20级分类"); if($nti

20、d = $btype)die("您所指定的typeid值重复。");if($btype!=0)$depth=$depth+1;$rootid=$rootid;$orders =$ntid;$parentid =$btype;/$child = $child;if ($parentstr="0") $parentstr=$btype;else $parentstr=$parentstr.",".$btype;else$depth=0;$rootid=$ntid;$orders=1;$parentid=0;$child=0;$parent

21、str=0;$query=mysql_query("insert into b_mtype values('$ntid','$tn','$parentid','$parentstr','$depth','$rootid','','$orders','')") ; if ($btype!=0) if ($depth>0) /当上级分类深度大于0的时候要更新其父类(或父类的父类)的版面数和相关排序 for ($i=1;$i<

22、=$depth;$i+) /更新其父类版面数 if ($parentid!="") $query=mysql_query("update b_mtype set child=child+1 where typeid='$parentid'"); /得到其父类的父类的版面ID $result=mysql_query("select parentid from b_mtype where typeid='$parentid'"); $par=mysql_fetch_array($result); if ($

23、par'parentid'!="") $parentid=$par'parentid' /当循环次数大于1并且运行到最后一次循环的时候直接进行更新 if ($i=$depth && $parentid!="") $query=mysql_query("update b_mtype set child=child+1 where typeid='$parentid'"); /for循环结果/更新该版面排序以及大于本需要和同在本分类下的版面排序序号 $query=mysql_

24、query("update b_mtype set orders=orders+1 where rootid='$rootid' and orders>'$orders'"); /$orders1=$orders+1; /echo "orders1=".$orders1; $query=mysql_query("update b_mtype set orders='$orders'+1 where typeid='$ntid'");else /当上级分类深度为0的时

25、候只要更新上级分类版面数和该版面排序序号即可 $query=mysql_query("update b_mtype set child=child+1 where typeid='$btype'"); $result=mysql_query("select max(orders) from b_mtype where typeid='$ntid'"); $ord=mysql_fetch_array($result); $query=mysql_query("update b_mtype set orders=&#

26、39;$ord0'+1 where typeid='$ntid'");echo "类别填加成功" ?><?PHP /修改设置function edit()/global $db,$editid,$tn,$arr;$editid=$_REQUEST"editid"$result=mysql_query("select * from b_mtype where typeid='$editid'");$tn=mysql_fetch_array($result);?> <

27、;form name="form2" action ="mer_type.php?ToDo=saveedit" method="post"> <input type="hidden" name="editid" value="<?php echo $editid;?>"><table width="90%" border="0" align=center cellpadding="3&quo

28、t; cellspacing="1" bgcolor="#CCCCCC"><tr> <th height=24 colspan=2>编辑类别:<?PHP echo $tn"typename"?></th></tr><tr> <td width="52%" height=30 bgcolor="#FFFFFF">类别名称</td><td width="48%" bgcol

29、or="#FFFFFF"> <input type="text" name="typename" size="35" value="<?php echo $tn"typename"?>"></td></tr><tr> <td width="52%" height=30 bgcolor="#FFFFFF"><U>所属类别</U><B

30、R>所属类别不能指定为当前类别<BR>所属类别不能指定为当前版面的下属类别</td><td width="48%" bgcolor="#FFFFFF"> <select name="class"><option value="0">做为主类别</option><? $query=mysql_query("select * from b_mtype order by rootid,orders");while($a

31、rr=mysql_fetch_array($query) ?><option value="<?php echo $arrtypeid?>" <?php if ($tn"parentid" = $arr"typeid") echo "selected" ?>><? if ($arr"depth">0) ?><?for ($i=1;$i<=$arr"depth"$i+) echo "-"

32、?><? ?><?php echo $arr"typename"?></option><?></select></td></tr><tr> <td width="52%" height=24 bgcolor="#FFFFFF">&nbsp;</td><td width="48%" bgcolor="#FFFFFF"> <input type=&q

33、uot;submit" name="Submit" value="提交修改"></td></tr></table></form><? ?><?php /保存修改function saveedit()/global $db,$aa,$bb,$cc,$dd,$ee,$ff,$gg,$ii,$jj,$kk,$ll,$mm,$nn,$qq,$rr;$editid=$_REQUEST"editid"$btype=$_REQUEST"class"

34、$tn=$_REQUEST"typename"if($editid = $btype ) die ("所属论坛不能指定自己"); $result=mysql_query("select * from b_mtype where typeid='$editid'");$aa=mysql_fetch_array($result);$newtypeid=$aa"typeid"$typename=$aa"typename"$parentid=$aa"parentid"

35、$iparentid=$aa"parentid"$parentstr=$aa"parentstr"$depth = $aa"depth"$rootid = $aa"rootid"$child = $aa"child"$orders = $aa"orders"/判断所指定的类别是否其下属类别if ($parentid =0)if ($btype!= 0) $result=mysql_query("select rootid from b_mtype where typ

36、eid='$btype'");$b=mysql_fetch_array($result);if ($rootid = $bb'rootid') die("您不能指定该版面的下属论坛作为所属论坛11"); else$result=mysql_query("select typeid from b_mtype where parentstr like '%$parentstr%' and typeid='$btype'");$cc=mysql_fetch_array($result);

37、if ($cc0) die("您不能指定该版面的下属论坛作为所属论坛2"); if ($parentid =0)$parentid=$editid;$iparentid=0;mysql_query("update b_mtype set typename='$tn',parentid='$btype' where typeid='$editid'");$result1=mysql_query("select max(rootid) from b_mtype");$ss=mysql_fet

38、ch_array($result1);$maxrootid=$ss"rootid"+1;if (!$maxrootid) $maxrootid=1;/假如更改了所属类别/需要更新其原来所属版面信息,包括深度、父级ID、版面数、排序、继承版主等数据/需要更新当前所属版面信息/继承版主数据需要另写函数进行更新-取消,在前台可用typeid in parentstr来获得if ($parentid != $btype && !($iparentid=0 && $btype=0) /如果原来不是一级分类改成一级分类 /echo "ggg&q

39、uot; if ($iparentid>0 && $btype=0) echo "第一部分" /更新当前版面数据 mysql_query("update b_mtype set depth=0,orders=0,rootid='$editid',parentid=0,parentstr='0' where typeid='$newtypeid'"); $parentstr=$parentstr ."," $result=mysql_query("selec

40、t count(*) from b_mtype where parentstr like '%$parentstr%'"); $dd=mysql_fetch_array($result); $postcount=$dd0; echo "postcount=".$postcount; if (empty($postcount) $postcount=1; else $postcount=$postcount+1; /更新其原来所属类别版面数 mysql_query("update b_mtype set child=child-'$

41、postcount' where typeid='$iparentid'"); /更新其原来所属类别数据,排序相当于剪枝而不需考虑 for ($i=1;$i<=$depth;$i+) /得到其父类的父类的版面ID $result2=mysql_query("select parentid from b_mtype where typeid='$iparentid'"); $ee=mysql_fetch_array($result2); if (!$ee0) $iparentid=$ee0; mysql_query(&q

42、uot;update b_mtype set child=child-'$postcount' where typeid='$iparentid'"); /for end if ($child >0) /m1 /更新其下属类别数据 /有下属类别,排序不需考虑,更新下属类别深度和一级排序ID(rootid)数据 /更新当前版面数据 $i=0; $query=mysql_query("select * from b_mtype where parentstr like '%$parentstr%'"); while

43、($arr=mysql_fetch_array($query) $i+; $mParentStr=strtr($arr'parentstr',$parentstr," "); mysql_query("update b_mtype set depth=depth-'$depth',rootid='$maxrootid',parentstr='$mParentStr' where typeid='$arrtypeid"); /m1 end elseif ($iparentid >

44、 0 && $btype >0) echo "第二部分" /将一个分类别移动到其他分类别下 /获得所指定的类别的相关信息 $result=mysql_query("select * from b_mtype where typeid='$btype'"); $gg=mysql_fetch_array($result); /得到其下属版面数 $parentstr=$parentstr ."," $iparentstr=$parentstr.$editid; echo $iparentstr; $re

45、sult1=mysql_query("select count(*) from b_mtype where parentstr like '%$iparentstr%'"); $ii=mysql_fetch_array($result1); $postcount=$ii0; echo "postcount1=".$postcout; if (empty($postcount) $postcount=1; /在获得移动过来的版面数后更新排序在指定类别之后的类别排序数据 $query=mysql_query("update b_mt

46、ype set orders=orders+'$postcount'+1 where rootid='$ggrootid' and orders>'$ggorders'"); /更新当前版面数据 If($ggparentstr="0") / $idepth=$ggdepth+1; / $iorders=$ggorders+1; mysql_query("update b_mtype set depth='$ggdepth'+1,orders='$ggorders'+1,

47、rootid='$ggrootid',parentid='$btype',parentstr='$ggtypeid' where typeid='$newtypeid'"); Else $aparentstr=$gg'parentstr'.",".$gg'typeid' $idepth=$gg'depth'+1; $iorders=$gg'orders'+1; mysql_query("update b_mtype set de

48、pth='$idepth',orders='$iorders',rootid='$ggrootid',parentid='$btype',parentstr='$aparentstr' where typeid='$editid'"); $i=1; / echo "ghh" /如果有则更新下属版面数据 /深度为原有深度加上当前所属类别的深度 $iparentstr=$parentstr.$newtypeid; $query=mysql_query("selec

49、t * from b_mtype where parentstr like '%$iparentstr%' order by orders"); while($arr=mysql_fetch_array($query) / m2 $i+; If ($gg'parentstr'="0") $iParentStr=$gg'typeid'.",".strtr($arr'parentstr',$parentstr," "); Else $iParentStr=$gg&q

50、uot;parentstr" .",".$gg"typeid" . "," . strtr($arr'parentstr',$parentstr," "); echo "iParentStr=".$iParentStr; $query=mysql_query("update b_mtype set depth=depth+'$ggdepth'-'$depth'+1,orders='$ggorders'+'

51、$i',rootid='$ggrootid',parentstr='$iParentStr' where typeid='$arrtypeid'"); /m2 end $parentid=$btype; if ($rootid=$gg'rootid') /m3 /在同一分类下移动 /更新所指向的上级类别版面数,i为本次移动过来的版面数 /更新其父类版面数 $query=mysql_query("update b_mtype set child=child+'$i' where (! pa

52、rentid=0) and typeid='$parentid'"); for ($k=1;$k<=$gg'depth'$k+) /得到其父类的父类的版面ID $result=mysql_query("select parentid from b_mtype where (! parentid=0) and typeid='$parentid'"); $vv=mysql_fetch_array($result); if ($vv0) $parentid=$vv0; /更新其父类的父类版面数 mysql_quer

53、y("update b_mtype set child=child+'$i' where (! parentid=0) and typeid='$parentid'"); / for end /更新其原父类版面数 mysql_query("update b_mtype set child=child-'$i' where (! parentid=0) and typeid='$iparentid'"); /更新其原来所属类别数据 for ($k=1;$k<=$depth;$k+) /得

54、到其原父类的父类的版面ID $result1=mysql_query("select parentid from b_mtype where (! parentid=0) and typeid='$iparentid'"); $zz=mysql_fetch_array($result1); if ($zz0) $iparentid=$zz0; /更新其原父类的父类版面数 mysql_query("update b_mtype set child=child-'$i' where (! parentid=0) and typeid=&

55、#39;$iparentid'"); /for end else /m3 end echo "ccc" /更新所指向的上级类别版面数,i为本次移动过来的版面数 /更新其父类版面数 mysql_query("update b_mtype set child=child+'$i' where typeid='$parentid'"); for ($k=1;$k<=$gg"depth"$k+) /得到其父类的父类的版面ID $result2=mysql_query("sele

56、ct parentid from b_mtype where typeid='$parentid'"); $yy=mysql_fetch_array($result2); if ($yy0) $parentid=$yy0; /更新其父类的父类版面数 mysql_query("update b_mtype set child=child+'$i' where typeid='$parentid'"); /for end /更新其原父类版面数 mysql_query("update b_mtype set ch

57、ild=child-'$i' where typeid='$iparentid'"); /更新其原来所属类别数据 for ($k=1;$k<=$depth;$k+) /得到其原父类的父类的版面ID $query=mysql_query("select parentid from b_mtype where typeid='$iparentid'"); while($arr=mysql_fetch_array($query) if ($arr0) $iparentid=$arr0; /更新其原父类的父类版面数 m

58、ysql_query("update b_mtype set child=child-'$i' where typeid='$iparentid'"); /for end /m3 end else echo "第三种情况"/如果原来是一级类别改成其他类别的下属类别/得到所指定的类别的相关信息$result=mysql_query("select * from b_mtype where typeid='$btype'");$gg=mysql_fetch_array($result);ec

59、ho $rootid;$result1=mysql_query("select count(*) from b_mtype where rootid='$rootid'");$qq=mysql_fetch_array($result1);$postcount=$qq0;/更新所指向的上级类别版面数,i为本次移动过来的版面数$parentid=$btype;/更新其父类版面数mysql_query("update b_mtype set child=child+'$postcount' where typeid='$parentid'");for ($k=1;$k<=$gg'depth'$k+) /得到其父类的父类的版面ID $result2=mysql_query("select parentid from b_mtype where typeid='$parentid'"); $rr=mysql_fetch_array($result2)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论