国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

分享一個php實現無限級分類程序代碼

2024-05-04 23:06:21
字體:
來源:轉載
供稿:網友

無限級分類是所有程序開發中會碰到的一個問題,下面我來介紹php+mysql實現的一個無限級分類程序,有需要的朋友可參考參考,下面給大家看看我的數據庫結構吧,數據庫的名字為 fa_category

Field Type Comment

cid int(11) 分類id

catename varchar(255) 分類名字

catetype int(1) 分類類型,1為單頁面,2為普通分類

catdir varchar(255) 英文目錄

display int(1) 1為顯示,2為不顯示

keywords varchar(255) 欄目關鍵字

description text 欄目描述

ctime int(11) 創建時間

parentid int(11) 父節點id,最高節點父節點為0

我們使用一個parentid字段來記錄父節點的id,如果parentid為0,則為root,不多說,我們看看代碼怎么寫吧,我們要實現的功能就是如圖片所示:

怎么樣把它這樣顯示呢?這個問題我想了很久,先看看這段SQL語句吧,代碼如下:

  1. SELECT c.cat_id, c.cat_name, c.measure_unit, c.parent_id, c.is_show, c.show_in_nav, c.grade         ,c.sort_order, COUNT( s.cat_id ) AS has_children 
  2. FROM ecs_category AS c 
  3. LEFT JOIN ecs_category AS s ON s.parent_id = c.cat_id 
  4. GROUP BY c.cat_id 
  5. ORDER BY c.parent_id, c.sort_order ASC 

用左連接連接一個表,返回一個字段 has_children,這個字段是記錄有多少子節點,看看代碼吧:

  1. public function getCategory($catid=0,$re_type = true,$selected=0) 
  2.     { 
  3.         $db      =  new Public_DataBase(); 
  4.         $sql  =  'select c.cid,c.catename,c.catetype,c.ctime,c.parentid,count(s.cid) as has_children from '
  5.         __MYSQL_PRE.'category as c left join '
  6.         __MYSQL_PRE.'category as s on s.parentid=c.cid group by c.cid order by c.parentid asc'
  7.         $res          =  $db->selectTable($sql); 
  8.         $cateInfo     =    self::getChildTree($catid,$res); 
  9.         if($re_type==true) 
  10.         { 
  11.                $select   =    ''
  12.               foreach($cateInfo as $val
  13.               { 
  14.                  $select .= '<option value="' . $val['cid'] . '" '
  15.                  $select .= ($selected == $val['cid']) ? "selected='ture'" : ''
  16.                  $select .= '>'
  17.                   if($val['level']>0) 
  18.                   {           
  19.                    $select .= str_repeat('&nbsp;'$val['level'] * 4); 
  20.                   } 
  21.                   $select .= htmlspecialchars(addslashes($val['catename']), ENT_QUOTES) . '</option>'
  22.               } 
  23.               return $select
  24.         } 
  25.          
  26.         else 
  27.         { 
  28.             foreach($cateInfo as $key=>$val
  29.             { 
  30.                   if($val['level']>0) 
  31.                   {           
  32.                            $cateInfo[$key]['catename']    =    "|".str_repeat('&nbsp;'$val['level'] * 8)."└─".$val['catename']; 
  33.                            
  34.                   } 
  35.             } 
  36.             return $cateInfo
  37.         } 
  38.          
  39.          
  40.     } 
  41.      
  42.     /** 
  43.      * 通過父ID遞歸得到所有子節點樹 
  44.      * @param int $catid  上級分類 
  45.      * @param array $arr  含有所有分類的數組 
  46.      * @return array 
  47.      */ 
  48.     public function getChildTree($catid,$arr
  49.     { 
  50.         $level = $last_cat_id = 0; 
  51.         while (!emptyempty($arr)) 
  52.         { 
  53.             foreach($arr as $key=>$value
  54.             { 
  55.                 $cid     =  $value['cid']; 
  56.                 if ($level == 0 && $last_cat_id == 0) 
  57.                 { 
  58.                    if ($value['parentid'] > 0) 
  59.                    { 
  60.                        break
  61.                    } 
  62.                    $options[$cid]          =     $value
  63.                    $options[$cid]['level'] =     $level
  64.                    $options[$cid]['id']    =     $cid
  65.                    $options[$cid]['name']  =     $value['catename']; 
  66.                    unset($arr[$key]); 
  67.                    if ($value['has_children'] == 0) 
  68.                    { 
  69.                         continue
  70.                    } 
  71.                    $last_cat_id  = $cid
  72.                    $cat_id_array = array($cid); 
  73.                    $level_array[$last_cat_id] = ++$level
  74.                    continue
  75.       
  76.                 } 
  77.                 if ($value['parentid'] == $last_cat_id
  78.                 { 
  79.                         $options[$cid]          = $value
  80.                         $options[$cid]['level'] = $level
  81.                         $options[$cid]['id']    = $cid
  82.                         $options[$cid]['name']  = $value['catename']; 
  83.                         unset($arr[$key]); 
  84.                         if ($value['has_children'] > 0) 
  85.                         { 
  86.                             if (end($cat_id_array) != $last_cat_id
  87.                             { 
  88.                                 $cat_id_array[] = $last_cat_id
  89.                             } 
  90.                                 $last_cat_id    = $cid
  91.                                 $cat_id_array[] = $cid
  92.                                 $level_array[$last_cat_id] = ++$level
  93.                         } 
  94.                 } 
  95.                 elseif ($value['parentid'] > $last_cat_id
  96.                 { 
  97.                     break
  98.                 } 
  99.             } 
  100.              
  101.              $count = count($cat_id_array); 
  102.              if ($count > 1) 
  103.              { 
  104.                  $last_cat_id = array_pop($cat_id_array); 
  105.                   
  106.              } 
  107.              elseif ($count == 1) 
  108.              { 
  109.                if ($last_cat_id != end($cat_id_array)) 
  110.                { 
  111.                    $last_cat_id = end($cat_id_array); 
  112.                } 
  113.                else 
  114.                { 
  115.                     $level = 0; 
  116.                     $last_cat_id = 0; 
  117.                     $cat_id_array = array(); 
  118.                     continue
  119.                } 
  120.              } 
  121.              if ($last_cat_id && isset($level_array[$last_cat_id])) 
  122.              { 
  123.                 $level = $level_array[$last_cat_id]; 
  124.              } 
  125.              else 
  126.              { 
  127.                  $level = 0; 
  128.              } 
  129.         } 
  130.                     return $options
  131.     } 

用smarty的一個循環就可以把它顯示出來,效果和上面圖片的一樣!大家有什么問題可以給我留言。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北川| 兰州市| 元谋县| 开江县| 成都市| 马龙县| 邻水| 漳州市| 云和县| 南投县| 灵台县| 崇阳县| 邮箱| 招远市| 方城县| 江津市| 浦北县| 醴陵市| 建始县| 谷城县| 信阳市| 左权县| 达孜县| 新安县| 扎兰屯市| 青田县| 井陉县| 邵阳县| 巴楚县| 漳州市| 团风县| 麻城市| 临武县| 临朐县| 庄河市| 石景山区| 海安县| 治多县| 阆中市| 阳原县| 马公市|