打造PHP的无限分级类(完整代码及注释)

开发 后端
本文提供了一组PHP无限分级类的实现代码,修正过Bug若干,可以直接使用。

下面提供一个PHP的无限分级类代码,想要实现无限分级类的朋友们可以试试。Bug已经检查修正过,直接复制代码就能使用(尖括号需要替换一下)。

1、数据库ProductShow中有b_mtype表,建表语句如下:

  1. CREATE TABLE `b_mtype` (  
  2.   `typeid` int(4) NOT NULL default '0',  
  3.   `typename` varchar(50) NOT NULL default '',  
  4.   `parentid` int(4) NOT NULL default '0',  
  5.   `parentstr` varchar(200) NOT NULL default '',  
  6.   `depth` int(4) NOT NULL default '0',  
  7.   `rootid` int(4) NOT NULL default '0',  
  8.   `child` varchar(200) NOT NULL default '',  
  9.   `orders` varchar(200) NOT NULL default '',  
  10.   PRIMARY KEY  (`typeid`)  
  11. ) TYPE=MyISAM;  
  12.  

2、ProductClass.php代码:(直接复制即可)

  1. < html>  
  2. < head>  
  3. < meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  4. < title>WHB_PHP无限级分类< /title>  
  5. < style type="text/css">  
  6. < !--  
  7. .STYLE1 {color: #FF0000}  
  8. body,td,th {  
  9.  font-family: 宋体;  
  10.  font-size: 12px;  
  11. }  
  12. .STYLE2 {  
  13.  color: #FFFFFF;  
  14.  font-size: 14px;  
  15.  font-weight: bold;  
  16. }  
  17. -->  
  18. < /style>  
  19. < /head>  
  20.  
  21. < body>  
  22. < ?php  
  23. ini_set("error_reporting","E_ALL & ~E_NOTICE");//忽略所有警告信息   
  24. require_once("conn.php");//嵌入连接文件  
  25.     @$ToDo=$_GET["ToDo"];  
  26.     switch($ToDo)  
  27.     {          
  28.     case "add":  
  29.     echo add();  
  30.     break;  
  31.      
  32.     case "edit":  
  33.      echo edit();  
  34.     break;  
  35.  
  36.      case "saveadd":  
  37.      echo saveadd();  
  38.     break;  
  39.      
  40.     case "saveedit":  
  41.     echo saveedit();  
  42.     break;  
  43.      
  44.     case "del":  
  45.     echo del();  
  46.     break;  
  47.         
  48.     default:  
  49. ?>  
  50.  
  51. < table width="90%"   border="0" align="center" cellpadding="0" cellspacing="0" class="tblborder">  
  52. < tr>  
  53.      < td valign="top">< table width="100%"   border="0" cellspacing="1" cellpadding="3">  
  54.        < tr>  
  55.            < td width="55%" height="20" align="center" bgcolor="#698FC7">< span class="tblhead">< b>< span class="STYLE2">商品类别设置< /span> < a href="?ToDo=add">添加一个新类别< /a>< /b>< /span>< /td>  
  56.             
  57.         < /tr>  
  58.      < /table>  
  59.        < table width="100%" bgcolor="#cccccc" cellspacing="1" cellpadding="3">  
  60.          < tr bgcolor="#F9F9F9">  
  61.            < td width="6%" height="21">ID< /td>  
  62.            < td width="49%">类别名称< /td>  
  63.            < td width="14%">排序< /td>  
  64.            < td width="31%">操作< /td>  
  65.          < /tr>  
  66.    < ?php  
  67.          
  68.    $query=mysql_query("select * from b_mtype order by rootid,orders");  
  69.    while($arr=mysql_fetch_array($query))  
  70.    {  
  71.      
  72.    ?>  
  73.          < tr>  
  74.            < td   bgcolor="#FFFFFF">< ?php echo $arr["typeid"];?>< /td>  
  75.            < td   bgcolor="#FFFFFF">< ?php if ($arr["depth"]>0) {  
  76.                       for($i=1;$i< =$arr["depth"];$i++){ echo "&nbsp;";}  
  77.      }?>  
  78.  
  79.        < ? if($arr["child"]>0) { echo "+";}else {echo "-";}?>   
  80.        < ? if($arr["parentid"]==0) { echo "< b>";}?>  
  81.        < ? echo $arr["typename"];?>  
  82.        < ? if ($arr["child"]>0) {?>(< ? echo $arr["child"];?>)< ? }?>< /td>  
  83.            < td bgcolor="#FFFFFF">< ?php echo $arr["orders"];?>< /td>  
  84.            < td bgcolor="#FFFFFF">< a href="?ToDo=add&editid=< ?php echo $arr["typeid"];?>">< U>添加子类< /U>< /a>   
  85.      | < a href="?ToDo=edit&editid=< ?php echo $arr["typeid"]?>">< U>编辑类别< /U>< /a>   
  86.             |   < ? if($arr["child"]==0){?>< a href="?ToDo=del&editid=< ? echo $arr["typeid"];?>" onClick="{if(confirm('删除类别时,该类别下的所有产品将一并被删除,确定删除吗?')){return true;}return false;}">< U>删除类别< /U>< /a>< ?}else{?>< a href="#" onClick="{if(confirm('该类别含有下属类别,必须先删除其下属类别方能删除本类别!')){return true;}return false;}">< U>删除类别< /U>< /a>< ?}?>&nbsp;< /td>  
  87.          < /tr>  
  88.    < ?php  
  89.    }  
  90.    ?>  
  91.        < /table>  
  92.      < /td>  
  93.    < /tr>  
  94. < /table>   
  95. < ?php  
  96.      break;  
  97.     }  
  98.     ?>  
  99.  
  100.  
  101. < ?php   ///增加类别  
  102. function add(){  
  103.     @$editid=$_REQUEST["editid"];  
  104. ?>  
  105.     < form name="form1" method="post" action="?ToDo=saveadd">  
  106.          < table width="90%"   align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">  
  107.            < tr bgcolor="#CCCCCC">  
  108.              < td colspan="2" align="center" bgcolor="#698FC7">< span class="STYLE2">创建新的类别< /span>< /td>  
  109.            < /tr>               
  110.            < tr>  
  111.              < td width="25%" bgcolor="#FFFFFF">类别名称:< /td>  
  112.              < td width="75%" bgcolor="#FFFFFF">< input name="typename" type="text" id="typename">< /td>  
  113.            < /tr>  
  114.           < tr>   
  115.             < td width="25%" height=30 bgcolor="#FFFFFF">< U>所属类别< /U>< /td>  
  116.             < td width="75%" bgcolor="#FFFFFF">   
  117.              < select name=btype>  
  118.              < option value="0">做为主类别< /option>  
  119.              < ?   
  120.      $query=mysql_query("select * from b_mtype order by rootid,orders");     
  121.      while($arr=mysql_fetch_array($query)){ ?>  
  122.              < option value="< ? echo $arr["typeid"]?>" < ?php if($editid == $arr['typeid']){ echo " selected"; }?>>  
  123.              < ? if($arr["depth"]>0) {  
  124.                for($i=1;$i< =$arr["depth"];$i++)  
  125.       {  
  126.         echo "-";  
  127.           }  
  128.          
  129.               }?>  
  130.               < ? echo $arr["typename"]?>< /option>  
  131.              < ?  
  132.            }    
  133.               ?>  
  134.              < /select>  
  135.              < /td>  
  136.            < /tr>  
  137.            < tr>  
  138.              < td bgcolor="#FFFFFF">&nbsp;< /td>  
  139.              < td bgcolor="#FFFFFF">< input type="submit" name="Submit" value="保存">  
  140.                < label>  
  141.                < input type="button" name="cancel" id="cancel" value="返回" onclick="history.go(-1);" />  
  142.              < /label>< /td>  
  143.            < /tr>  
  144.          < /table>  
  145. < /form>  
  146. < ? }?>  
  147.  
  148. < ?php    /////保存增加的类别  
  149. function saveadd(){  
  150.  
  151. $query=mysql_query("select typeid from b_mtype order by typeid desc limit 1");  
  152. while($arr=mysql_fetch_array($query)){  
  153. if (!$arr["typeid"]){  
  154.       $postnum=1;  
  155.     
  156. }else{  
  157.       $postnum=$arr["typeid"]+1;  
  158.     
  159. }     
  160. }  
  161. if(!$postnum$postnum=1;  
  162.  
  163. $ntid   =$postnum;  
  164. $tn     =$_REQUEST["typename"];  
  165. $btype =$_REQUEST["btype"];  
  166.  
  167.  
  168. //echo $btype;  
  169.  
  170. if ($ntid=="" or $tn=="")  
  171. {  
  172. echo "< script language='javascript'>";  
  173. echo "alert('参数有误,请重新填写.!');";  
  174. echo "location.href='?';";  
  175. echo "< /script>";  
  176. die();  
  177. }  
  178.  
  179. if ($btype!=0){  
  180.     
  181.     $result=mysql_query("select rootid,typeid,depth,orders,parentstr from b_mtype where typeid='$btype'");  
  182.     $aa=mysql_fetch_array($result);  
  183.     $rootid=$aa['rootid'];  
  184.     //echo "aaaaaaaaaaa";  
  185.     $parentid=$aa['typeid'];  
  186.     $depth=$aa['depth'];  
  187.     $orders=$aa['orders'];  
  188.     $parentstr=$aa['parentstr'];  
  189.     //echo $rootid;  
  190.  
  191.     if(($aa["depth"]+1)>20){   die("本分类限制最多只能有20级分类"); }  
  192.  
  193. }  
  194.  
  195. if($ntid == $btype)  
  196. {  
  197. echo "< script language='javascript'>";  
  198. echo "alert('您所指定的typeid值重复!');";  
  199. echo "location.href='?';";  
  200. echo "< /script>";  
  201. die();  
  202. }  
  203.  
  204. if($btype!=0){  
  205.  
  206.    $depth=$depth+1;  
  207.    $rootid=$rootid;  
  208.    $orders =$ntid;  
  209.    $parentid =$btype;  
  210.    //$child = $child;  
  211.    if ($parentstr=="0"){  
  212.       $parentstr=$btype;  
  213.    }else{  
  214.       $parentstr=$parentstr.",".$btype;  
  215.    }  
  216.  
  217. }else{  
  218.    $depth=0;  
  219.    $rootid=$ntid;  
  220.    $orders=1;  
  221.    $parentid=0;  
  222.    $child=0;  
  223.    $parentstr=0;  
  224. }  
  225.  
  226. //插入类别  
  227. $query=mysql_query("insert into b_mtype values('$ntid','$tn','$parentid','$parentstr','$depth','$rootid','','$orders')") ;  
  228. //用于调试 echo "insert into b_mtype values('$ntid','$tn','$parentid','$parentstr','$depth','$rootid','','$orders')";  
  229.  
  230. if ($btype!=0)  
  231.    {  
  232.       if ($depth>0)  
  233.       {  
  234.          //当上级分类深度大于0的时候要更新其父类(或父类的父类)的版面数和相关排序  
  235.          for ($i=1;$i< =$depth;$i++){  
  236.             //更新其父类版面数  
  237.             if ($parentid!=""){  
  238.                $query=mysql_query("update b_mtype set child=child+1 where typeid='$parentid'");  
  239.             }  
  240.             //得到其父类的父类的版面ID  
  241.             $result=mysql_query("select parentid from b_mtype where typeid='$parentid'");  
  242.             $par=mysql_fetch_array($result);  
  243.     
  244.             if ($par['parentid']!=""){  
  245.               $parentid=$par['parentid'];  
  246.             }  
  247.             //当循环次数大于1并且运行到最后一次循环的时候直接进行更新  
  248.             if ($i==$depth && $parentid!=""){  
  249.               $query=mysql_query("update b_mtype set child=child+1 where typeid='$parentid'");  
  250.             }  
  251.           }//for循环结束  
  252.           //更新该版面排序以及大于本需要和同在本分类下的版面排序序号  
  253.           $query=mysql_query("update b_mtype set orders=orders+1 where rootid='$rootid' and orders>'$orders'");  
  254.           //$orders1=$orders+1;  
  255.           //echo "orders1=".$orders1;  
  256.           $query=mysql_query("update b_mtype set orders='$orders'+1 where typeid='$ntid'");    
  257.     
  258.         }else{  //对应if ($depth>0),当上级分类深度为0的时候只要更新上级分类版面数和该版面排序序号即可    
  259.         $query=mysql_query("update b_mtype set child=child+1 where typeid='$btype'");  
  260.         $result=mysql_query("select max(orders) from b_mtype where typeid='$ntid'");  
  261.         $ord=mysql_fetch_array($result);     
  262.         $query=mysql_query("update b_mtype set orders='$ord[0]'+1 where typeid='$ntid'");  
  263.         }  
  264.       
  265. }  
  266.    
  267. echo "< script language='javascript'>";  
  268. echo "alert('类别添加成功!');";  
  269. echo "location.href='?';";  
  270. echo "< /script>";  
  271. }  
  272. ?>  
  273.  
  274. < ?PHP    ////修改设置  
  275. function edit(){  
  276.    //global $db,$editid,$tn,$arr;  
  277.  
  278. $editid=$_REQUEST["editid"];  
  279.  
  280. $result=mysql_query("select * from b_mtype where typeid='$editid'");  
  281. $tn=mysql_fetch_array($result);  
  282. ?>       
  283. < form   name="form2" action ="?ToDo=saveedit" method="post">         
  284. < input type="hidden" name="editid" value="< ?php echo $editid;?>">  
  285. < table width="90%" border="0"   align=center cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">  
  286. < tr>   
  287. < th height=24 colspan=2 bgcolor="#698FC7" class="STYLE2">编辑类别:< ?PHP echo $tn["typename"];?>< /th>  
  288. < /tr>  
  289. < tr>   
  290. < td width="44%" height=30 bgcolor="#FFFFFF">类别名称< /td>  
  291. < td width="56%" bgcolor="#FFFFFF">   
  292. < input type="text" name="typename" size="35"   value="< ?php echo $tn["typename"];?>">  
  293. < /td>  
  294. < /tr>  
  295.  
  296. < tr>   
  297. < td width="44%" height=30 bgcolor="#FFFFFF">< p>所属类别< /p>  
  298.   < p>    < span class="STYLE1">特别提示:< /span>< br />  
  299.     所属类别不能指定当前类别(即自己)为父类别< BR>  
  300.     所属类别不能指定当前类别的子类别为父类别< /p>< /td>  
  301. < td width="56%" bgcolor="#FFFFFF">   
  302. < select name="class">  
  303. < option value="0">做为主类别< /option>  
  304. < ?   
  305. $query=mysql_query("select * from b_mtype order by rootid,orders");  
  306.  
  307. while($arr=mysql_fetch_array($query))  
  308. { ?>  
  309. < option value="< ?php echo $arr[typeid]?>" < ?php if ($tn["parentid"] == $arr["typeid"]) { echo "selected"; } ?>>< ? if ($arr["depth"]>0) {?>  
  310. < ?for ($i=1;$i< =$arr["depth"];$i++){ echo "-";}?>  
  311. < ? } ?>< ?php echo $arr["typename"]?>< /option>  
  312. < ?  
  313. }  
  314. ?>  
  315. < /select>  
  316. < /td>  
  317. < /tr>  
  318.  
  319. < tr>   
  320. < td width="44%" height=24 bgcolor="#FFFFFF">&nbsp;< /td>  
  321. < td width="56%" bgcolor="#FFFFFF">   
  322. < input type="submit" name="Submit" value="提交修改">  
  323. < /td>  
  324. < /tr>  
  325. < /table>  
  326. < /form>  
  327.  
  328. < ?   
  329. }  
  330.  
  331. ?>  
  332.  
  333.  
  334. < ?php   /////保存修改  
  335. function saveedit(){  
  336.    //global $db,$aa,$bb,$cc,$dd,$ee,$ff,$gg,$ii,$jj,$kk,$ll,$mm,$nn,$qq,$rr;  
  337.    $editid=$_REQUEST["editid"];  
  338.    $btype=$_REQUEST["class"];  
  339.    $tn=$_REQUEST["typename"];  
  340.  
  341.    if($editid == $btype ){  
  342.  echo "< script language='javascript'>";  
  343.     echo "alert('所属类别不能指定当前类别(即自己)为父类别!');";  
  344.     echo "location.href='?';";  
  345.     echo "< /script>";  
  346.     die();   
  347.  }  
  348.  
  349.    $result=mysql_query("select * from b_mtype where typeid='$editid'");  
  350.    $aa=mysql_fetch_array($result);  
  351.    $newtypeid=$aa["typeid"];  
  352.    $typename=$aa["typename"];  
  353.    $parentid=$aa["parentid"];  
  354.    $iparentid=$aa["parentid"];  
  355.    $parentstr=$aa["parentstr"];  
  356.    $depth = $aa["depth"];  
  357.    $rootid = $aa["rootid"];  
  358.    $child = $aa["child"];  
  359.    $orders = $aa["orders"];  
  360.  
  361.    ////判断所指定的类别是否其下属类别  
  362.    if ($parentid ==0){  
  363. if ($btype!= 0) {  
  364. $result=mysql_query("select rootid from b_mtype where typeid='$btype'");  
  365. $b=mysql_fetch_array($result);  
  366. if ($rootid == $bb['rootid']) {  
  367.  echo "< script language='javascript'>";  
  368.     echo "alert('所属类别不能指定当前类别的子类别为父类别!');";  
  369.     echo "location.href='?';";  
  370.     echo "< /script>";  
  371.     die();   
  372.    }   
  373. }  
  374.  
  375.     }else{  
  376. $result=mysql_query("select typeid from b_mtype where parentstr like '%$parentstr%' and typeid='$btype'");  
  377. $cc=mysql_fetch_array($result);  
  378. if ($cc[0]){  
  379.    echo "< script language='javascript'>";  
  380.     echo "alert('所属类别不能指定当前类别的子类别为父类别!');";  
  381.     echo "location.href='?';";  
  382.     echo "< /script>";  
  383.     die();     
  384. }  
  385.    }  
  386.  
  387.    if ($parentid ==0){  
  388. $parentid=$editid;  
  389. $iparentid=0;  
  390.    }  
  391.    mysql_query("update b_mtype set typename='$tn',parentid='$btype' where typeid='$editid'");  
  392.  
  393.    $result1=mysql_query("select max(rootid) from b_mtype");  
  394.    $ss=mysql_fetch_array($result1);  
  395.    $maxrootid=$ss["rootid"]+1;  
  396.    if (!$maxrootid){ $maxrootid=1;}  
  397.     
  398.  
  399. //假如更改了所属类别  
  400. //需要更新其原来所属版面信息,包括深度、父级ID、版面数、排序、继承版主等数据  
  401. //需要更新当前所属版面信息  
  402. //继承版主数据需要另写函数进行更新--取消,在前台可用typeid in parentstr来获得  
  403.  
  404. if ($parentid != $btype && !($iparentid==0 && $btype==0)) {  
  405.     //如果原来不是一级分类改成一级分类  
  406.     //echo "ggg";  
  407.       if ($iparentid>0 && $btype==0)   
  408.       {  
  409.    //更新当前版面数据  
  410.    mysql_query("update b_mtype set depth=0,orders=0,rootid='$editid',parentid=0,parentstr='0' where typeid='$newtypeid'");  
  411.    $parentstr=$parentstr .",";  
  412.    $result=mysql_query("select count(*) from b_mtype where parentstr like '%$parentstr%'");  
  413.    $dd=mysql_fetch_array($result);  
  414.    $postcount=$dd[0];  
  415.     if (emptyempty($postcount))  
  416.     {  
  417.        $postcount=1;  
  418.        }else{  
  419.        $postcount=$postcount+1;  
  420.        }  
  421.    //更新其原来所属类别版面数  
  422.    mysql_query("update b_mtype set child=child-'$postcount' where typeid='$iparentid'");  
  423.    //更新其原来所属类别数据,排序相当于剪枝而不需考虑  
  424.    for ($i=1;$i< =$depth;$i++)  
  425.     {  
  426.     ////得到其父类的父类的版面ID  
  427.     $result2=mysql_query("select parentid from b_mtype where typeid='$iparentid'");  
  428.     $ee=mysql_fetch_array($result2);  
  429.     if (!$ee[0]){  
  430.      $iparentid=$ee[0];  
  431.      mysql_query("update b_mtype set child=child-'$postcount' where typeid='$iparentid'");  
  432.            }  
  433.        } //for end  
  434.     
  435.    if ($child >0){     //m1  
  436.    //更新其下属类别数据  
  437.    //有下属类别,排序不需考虑,更新下属类别深度和一级排序ID(rootid)数据  
  438.    //更新当前版面数据  
  439.      
  440.      $i=0;  
  441.      $query=mysql_query("select * from b_mtype where parentstr like '%$parentstr%'");  
  442.      while($arr=mysql_fetch_array($query)){  
  443.  
  444.      $i++;  
  445.      $mParentStr=strtr($arr['parentstr'],$parentstr," ");  
  446.      mysql_query("update b_mtype set depth=depth-'$depth',rootid='$maxrootid',parentstr='$mParentStr' where typeid='$arr[typeid]");  
  447.      }  
  448.     }   //m1 end  
  449.     }elseif ($iparentid > 0 && $btype >0) {  
  450.        
  451.      //将一个分类别移动到其他分类别下  
  452.      //获得所指定的类别的相关信息  
  453.      $result=mysql_query("select * from b_mtype where typeid='$btype'");  
  454.      $gg=mysql_fetch_array($result);  
  455.      //得到其下属版面数  
  456.      $parentstr=$parentstr .",";  
  457.      $iparentstr=$parentstr.$editid;  
  458.      echo $iparentstr;  
  459.      $result1=mysql_query("select count(*) from b_mtype where parentstr like '%$iparentstr%'");  
  460.      $ii=mysql_fetch_array($result1);  
  461.      $postcount=$ii[0];  
  462.      if (emptyempty($postcount)){ $postcount=1; }  
  463.  
  464.      //在获得移动过来的版面数后更新排序在指定类别之后的类别排序数据  
  465.  
  466.       $query=mysql_query("update b_mtype set orders=orders+'$postcount'+1 where rootid='$gg[rootid]' and orders>'$gg[orders]'");  
  467.      //更新当前版面数据  
  468.       If($gg[parentstr]=="0") {  
  469.        
  470.         // $idepth=$gg[depth]+1;  
  471.         // $iorders=$gg[orders]+1;  
  472.  
  473.          mysql_query("update b_mtype set depth='$gg[depth]'+1,orders='$gg[orders]'+1,rootid='$gg[rootid]',parentid='$btype',parentstr='$gg[typeid]' where typeid='$newtypeid'");  
  474.       }Else{  
  475.       $aparentstr=$gg['parentstr'].",".$gg['typeid'];  
  476.          $idepth=$gg['depth']+1;  
  477.          $iorders=$gg['orders']+1;  
  478.          mysql_query("update b_mtype set depth='$idepth',orders='$iorders',rootid='$gg[rootid]',parentid='$btype',parentstr='$aparentstr' where typeid='$editid'");  
  479.       }  
  480.       $i=1;  
  481.       //如果有则更新下属版面数据  
  482.       //深度为原有深度加上当前所属类别的深度  
  483.    $iparentstr=$parentstr.$newtypeid;  
  484.         $query=mysql_query("select * from b_mtype where parentstr like '%$iparentstr%' order by orders");  
  485.       while($arr=mysql_fetch_array($query)){    // m2  
  486.        $i++;  
  487.            If ($gg['parentstr']=="0") {  
  488.             $iParentStr=$gg['typeid'].",".strtr($arr['parentstr'],$parentstr," ");  
  489.         }Else{  
  490.             $iParentStr=$gg["parentstr"] .",".$gg["typeid"] . "," . strtr($arr['parentstr'],$parentstr," ");  
  491.         }  
  492.     
  493.         $query=mysql_query("update b_mtype set depth=depth+'$gg[depth]'-'$depth'+1,orders='$gg[orders]'+'$i',rootid='$gg[rootid]',parentstr='$iParentStr' where typeid='$arr[typeid]'");  
  494.       }   ///m2 end  
  495.  
  496.       $parentid=$btype;  
  497.       if ($rootid==$gg['rootid']) {   ///m3  
  498.      //在同一分类下移动  
  499.      //更新所指向的上级类别版面数,i为本次移动过来的版面数  
  500.      //更新其父类版面数  
  501.        $query=mysql_query("update b_mtype set child=child+'$i' where (! parentid=0) and typeid='$parentid'");  
  502.        for ($k=1;$k< =$gg['depth'];$k++){  
  503.    //得到其父类的父类的版面ID  
  504.      $result=mysql_query("select parentid from b_mtype where (! parentid=0) and typeid='$parentid'");  
  505.      $vv=mysql_fetch_array($result);  
  506.       if ($vv[0]){  
  507.     $parentid=$vv[0];  
  508.     //更新其父类的父类版面数  
  509.     mysql_query("update b_mtype set child=child+'$i' where (! parentid=0) and   typeid='$parentid'");  
  510.        }  
  511.         
  512.     } // for end  
  513.       //更新其原父类版面数  
  514.       mysql_query("update b_mtype set child=child-'$i' where (! parentid=0) and typeid='$iparentid'");  
  515.       //更新其原来所属类别数据  
  516.  
  517.       for ($k=1;$k< =$depth;$k++){  
  518.      //得到其原父类的父类的版面ID  
  519.      $result1=mysql_query("select parentid from b_mtype where (! parentid=0) and typeid='$iparentid'");  
  520.      $zz=mysql_fetch_array($result1);  
  521.      if ($zz[0]){  
  522.     $iparentid=$zz[0];  
  523.      
  524.     //更新其原父类的父类版面数  
  525.     mysql_query("update b_mtype set child=child-'$i' where (! parentid=0) and   typeid='$iparentid'");  
  526.       }  
  527.      }//for end  
  528.         
  529.      }else{   ////m3 end  
  530.      //更新所指向的上级类别版面数,i为本次移动过来的版面数  
  531.      //更新其父类版面数  
  532.     mysql_query("update b_mtype set child=child+'$i' where typeid='$parentid'");  
  533.      for ($k=1;$k< =$gg["depth"];$k++){  
  534.    //得到其父类的父类的版面ID  
  535.    $result2=mysql_query("select parentid from b_mtype where typeid='$parentid'");  
  536.    $yy=mysql_fetch_array($result2);  
  537.    if ($yy[0]){  
  538.     $parentid=$yy[0];  
  539.     //更新其父类的父类版面数  
  540.     mysql_query("update b_mtype set child=child+'$i' where typeid='$parentid'");  
  541.     }  
  542.     
  543.       } //for end  
  544.       //更新其原父类版面数  
  545.       mysql_query("update b_mtype set child=child-'$i' where typeid='$iparentid'");  
  546.       //更新其原来所属类别数据  
  547.       for ($k=1;$k< =$depth;$k++){  
  548.    //得到其原父类的父类的版面ID  
  549.    $query=mysql_query("select parentid from b_mtype where typeid='$iparentid'");  
  550.    while($arr=mysql_fetch_array($query))  
  551.     {  
  552.          if ($arr[0]){  
  553.           $iparentid=$arr[0];  
  554.           //更新其原父类的父类版面数  
  555.           mysql_query("update b_mtype set child=child-'$i' where typeid='$iparentid'");  
  556.              }  
  557.         }  
  558.        } //for end   
  559.     } ///m3 end    
  560.     }else{  
  561. //如果原来是一级类别改成其他类别的下属类别  
  562. //得到所指定的类别的相关信息  
  563. $result=mysql_query("select * from b_mtype where typeid='$btype'");  
  564. $gg=mysql_fetch_array($result);  
  565. echo $rootid;  
  566. $result1=mysql_query("select count(*) from b_mtype where rootid='$rootid'");  
  567. $qq=mysql_fetch_array($result1);  
  568. $postcount=$qq[0];  
  569. //更新所指向的上级类别版面数,i为本次移动过来的版面数  
  570. $parentid=$btype;  
  571. //更新其父类版面数  
  572. mysql_query("update b_mtype set child=child+'$postcount' where typeid='$parentid'");  
  573.  
  574. for ($k=1;$k< =$gg['depth'];$k++){  
  575.    //得到其父类的父类的版面ID  
  576.    $result2=mysql_query("select parentid from b_mtype where typeid='$parentid'");  
  577.    $rr=mysql_fetch_array($result2);  
  578.    if ($rr[0]){  
  579.     $parentid=$rr[0];  
  580.     //更新其父类的父类版面数  
  581.     mysql_query("update b_mtype set child=child+'$postcount' where typeid='$parentid'");  
  582.    }  
  583.  
  584. ///for end   
  585. //在获得移动过来的版面数后更新排序在指定类别之后的类别排序数据  
  586. mysql_query("update b_mtype set orders=orders+'$postcount'+1 where rootid='$gg[rootid]' and orders>'$gg[orders]'");  
  587. $i=0;  
  588. $query=mysql_query("select * from b_mtype where rootid='$rootid' order by orders");  
  589. while($arr=mysql_fetch_array($query))  
  590. {  
  591.     $i++;  
  592.     if ($arr['parentid'] ==0)  
  593.     {  
  594.    if ($gg['parentstr'] =="0")  
  595.     {   
  596.           $parentstr=$gg['typeid'];  
  597.        }else{  
  598.           $parentstr=$gg['parentstr'] .",".$gg['typeid'];  
  599.        }  
  600.      mysql_query("update b_mtype set depth=depth+'$gg[depth]'+1,orders='$gg[orders]'+'$i',rootid='$gg[rootid]',parentstr='$parentstr',parentid='$btype' where typeid='$arr[typeid]'");  
  601.      
  602.     }else{  
  603.    if ($gg['parentstr'] =="0"){  
  604.          $parentstr=$gg['typeid'] ."," . $arr['parentstr'];  
  605.        }else{  
  606.          $parentstr=$gg['parentstr'] .",".$gg['typeid'] .","$arr['parentstr'];  
  607.        }  
  608.       mysql_query("update b_mtype set depth=depth+'$gg[depth]'+1,orders='$gg[orders]'+'$i',rootid='$gg[rootid]',parentstr='$parentstr' where typeid='$arr[typeid]'");  
  609.  
  610.      }  
  611.    }///while end  
  612. }//else end   
  613. }  
  614. echo "< script language='javascript'>";  
  615. echo "alert('类别修改成功!');";  
  616. echo "location.href='?';";  
  617. echo "< /script>";  
  618. }  
  619. ?>  
  620.  
  621. < ?php   
  622. function   del(){  
  623.  
  624. ////更新其上级版面类别数,如果该类别含有下级类别则不允许删除  
  625. $editid=$_REQUEST["editid"];  
  626. $result=mysql_query("select parentstr,child,depth from b_mtype where typeid='$editid'");  
  627. $aa=mysql_fetch_array($result);  
  628. if ($aa[0]!="") {  
  629. if ($aa[1]>0){  
  630.  echo "< script language='javascript'>";  
  631.     echo "alert('该类别含有下属类别,必须先删除其下属类别方能删除本类别!');";  
  632.     echo "location.href='?';";  
  633.     echo "< /script>";  
  634.     die();   
  635. }  
  636. //如果有上级版面,则更新数据  
  637.  
  638. if ($aa[2]>0){  
  639. $query=mysql_query("update b_mtype set child=child-1 where typeid in ($aa[0])");  
  640. }  
  641. $query=mysql_query("delete from b_mtype where typeid='$editid'");  
  642.  
  643.  
  644. }  
  645. echo "< script language='javascript'>";  
  646. echo "alert('类别删除成功!');";  
  647. echo "location.href='?';";  
  648. echo "< /script>";  
  649. }  
  650. ?>  
  651. < /body>  
  652. < /html>  
  653.  

试试看这个无限分级类吧!

【编辑推荐】

  1. 9个开发人员应该知道的PHP库
  2. 9月编程语言排行榜:PHP的历史性突破
  3. 专题:PHP开发基础入门
  4. PHP 5.3.0发布 新增命名空间等多项特性
  5. PHP命名空间规则解析及高级功能
责任编辑:yangsai 来源: 网易博客
相关推荐

2009-11-17 17:17:50

PHP上传多个文件

2020-04-20 13:45:32

神经网络模型代码

2009-09-08 14:04:29

CheckBox分级选

2009-11-27 16:47:36

PHP无限分类

2022-06-06 12:02:23

代码注释语言

2020-03-16 10:16:19

代码开发工具

2009-12-07 09:13:05

取消PHP上传限制

2015-11-02 09:31:32

2019-01-08 12:39:20

2014-09-12 10:38:01

程序员代码

2012-11-30 11:26:00

代码注释

2014-11-14 17:08:24

代码

2018-12-19 17:20:17

2015-06-08 10:31:30

程序员代码

2021-10-20 14:04:10

代码注释接口

2009-07-29 17:40:28

ASP.NET注释语句

2015-06-16 13:27:31

代码注释代码

2020-11-23 09:21:50

代码Google科技

2009-12-07 15:34:18

PHP类的封装

2009-12-01 17:07:44

PHP类Snoopy
点赞
收藏

51CTO技术栈公众号