日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
打造PHP的無限分級(jí)類(完整代碼及注釋)

下面提供一個(gè)PHP的無限分級(jí)類代碼,想要實(shí)現(xiàn)無限分級(jí)類的朋友們可以試試。Bug已經(jīng)檢查修正過,直接復(fù)制代碼就能使用(尖括號(hào)需要替換一下)。

創(chuàng)新互聯(lián)公司致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,包括成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、SEO優(yōu)化、網(wǎng)絡(luò)推廣、整站優(yōu)化營銷策劃推廣、電子商務(wù)、移動(dòng)互聯(lián)網(wǎng)營銷等。創(chuàng)新互聯(lián)公司為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制及解決方案,創(chuàng)新互聯(lián)公司核心團(tuán)隊(duì)十多年專注互聯(lián)網(wǎng)開發(fā),積累了豐富的網(wǎng)站經(jīng)驗(yàn),為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設(shè)服務(wù),在網(wǎng)站建設(shè)行業(yè)內(nèi)樹立了良好口碑。

1、數(shù)據(jù)庫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代碼:(直接復(fù)制即可)

 
 
 
  1. < html>  
  2. < head>  
  3. < meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  4. < title>WHB_PHP無限級(jí)分類< /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">商品類別設(shè)置< /span> < a href="?ToDo=add">添加一個(gè)新類別< /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 " ";}  
  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('刪除類別時(shí),該類別下的所有產(chǎn)品將一并被刪除,確定刪除嗎?')){return true;}return false;}">< U>刪除類別< /U>< /a>< ?}else{?>< a href="#" onClick="{if(confirm('該類別含有下屬類別,必須先刪除其下屬類別方能刪除本類別!')){return true;}return false;}">< U>刪除類別< /U>< /a>< ?}?> < /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">創(chuàng)建新的類別< /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"> < /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('參數(shù)有誤,請(qǐng)重新填寫.!');";  
  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級(jí)分類"); }  
  192.  
  193. }  
  194.  
  195. if($ntid == $btype)  
  196. {  
  197. echo "< script language='javascript'>";  
  198. echo "alert('您所指定的typeid值重復(fù)!');";  
  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. //用于調(diào)試 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.          //當(dāng)上級(jí)分類深度大于0的時(shí)候要更新其父類(或父類的父類)的版面數(shù)和相關(guān)排序  
  235.          for ($i=1;$i< =$depth;$i++){  
  236.             //更新其父類版面數(shù)  
  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.             //當(dāng)循環(huán)次數(shù)大于1并且運(yùn)行到最后一次循環(huán)的時(shí)候直接進(jìn)行更新  
  248.             if ($i==$depth && $parentid!=""){  
  249.               $query=mysql_query("update b_mtype set child=child+1 where typeid='$parentid'");  
  250.             }  
  251.           }//for循環(huán)結(jié)束  
  252.           //更新該版面排序以及大于本需要和同在本分類下的版面排序序號(hào)  
  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{  //對(duì)應(yīng)if ($depth>0),當(dāng)上級(jí)分類深度為0的時(shí)候只要更新上級(jí)分類版面數(shù)和該版面排序序號(hào)即可    
  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    ////修改設(shè)置  
  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.     所屬類別不能指定當(dāng)前類別(即自己)為父類別< BR>  
  300.     所屬類別不能指定當(dāng)前類別的子類別為父類別< /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"> < /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('所屬類別不能指定當(dāng)前類別(即自己)為父類別!');";  
  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('所屬類別不能指定當(dāng)前類別的子類別為父類別!');";  
  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('所屬類別不能指定當(dāng)前類別的子類別為父類別!');";  
  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. //需要更新其原來所屬版面信息,包括深度、父級(jí)ID、版面數(shù)、排序、繼承版主等數(shù)據(jù)  
  401. //需要更新當(dāng)前所屬版面信息  
  402. //繼承版主數(shù)據(jù)需要另寫函數(shù)進(jìn)行更新--取消,在前臺(tái)可用typeid in parentstr來獲得  
  403.  
  404. if ($parentid != $btype && !($iparent
    網(wǎng)頁名稱:打造PHP的無限分級(jí)類(完整代碼及注釋)
    網(wǎng)站網(wǎng)址:http://www.dlmjj.cn/article/coohidh.html