无限极分类不知pid_PHP实现无限极分类
生活随笔
收集整理的這篇文章主要介紹了
无限极分类不知pid_PHP实现无限极分类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
導讀:說到無限極分類,這個在程序中是常見的一個功能點了。實現的方式也有很多種,今天著重分享一下涉及到數據庫的無線分類,可以使用遞歸處理,也可以使用循環查詢數據庫處理。但是我們考慮到數據庫的性能問題,都不建議采用循環查庫。都是直接設計好數據表,直接查庫,通過代碼層實現。
1.我們實現準備好數據表,代碼結構如下。
CREATE TABLE `bg_cate` ( `cate_Id` int(30) unsigned NOT NULL AUTO_INCREMENT, `cate_ParentId` int(30) unsigned DEFAULT '0', `cate_Name` varchar(100) NOT NULL, `cate_Intro` varchar(500) DEFAULT NULL, `cate_Order` int(30) unsigned DEFAULT '0', `cate_Icon` varchar(100) DEFAULT NULL, PRIMARY KEY (`cate_Id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=34 ; INSERT INTO `bg_cate` (`cate_Id`, `cate_ParentId`, `cate_Name`, `cate_Intro`, `cate_Order`, `cate_Icon`) VALUES (4, 0, '往事如風', '記錄往事', 0, 'icons/6.gif'), (5, 0, '水煮三國', '品位三國智慧', 0, 'icons/3.gif'), (2, 0, '技術學習', '平時學習的一些筆記,歡迎批評指正。', 0, 'icons/18.gif'), (3, 0, '生活點滴', '記錄生活點滴', 0, 'icons/2.gif'), (6, 0, '梔子花開', '青春無限', 0, 'icons/8.gif'), (7, 0, '假日休閑', '悠閑、自在', 0, 'icons/24.gif'), (8, 2, 'html', 'html學習', 0, 'icons/1.gif'), (9, 2, 'css', 'css學習', 0, 'icons/1.gif'), (10, 2, 'php', 'php學習', 0, 'icons/18.gif'), (11, 10, 'php基礎知識', 'php基礎知識', 0, 'icons/1.gif'), (12, 10, 'oop', 'oop', 0, 'icons/1.gif'), (13, 10, 'php安全', '講述php安全', 0, 'icons/1.gif'), (14, 10, 'seagull framework', 'seagull framework', 0, 'icons/1.gif'), (15, 2, 'javascript', 'javascript學習', 0, 'icons/1.gif'), (16, 2, '設計模式', NULL, 0, 'icons/1.gif'), (17, 2, '軟件工程', '軟件工程學習', 0, 'icons/1.gif'), (18, 3, '廈門生活', '廈門生活', 0, 'icons/8.gif'), (19, 3, '大學生活', '大學生活', 0, 'icons/8.gif'), (20, 3, '童年生活', '童年生活', 0, 'icons/15.gif'), (21, 19, '學習', '學習', 0, 'icons/1.gif'), (22, 19, '運動', '運動', 0, 'icons/16.gif'), (23, 19, '旅游', '旅游', 0, 'icons/24.gif'), (24, 22, '排球', '排球', 0, 'icons/9.gif'), (25, 22, '籃球', '籃球', 0, 'icons/9.gif'), (26, 22, '羽毛球', '羽毛球', 0, 'icons/9.gif'), (27, 22, '乒乓球', '乒乓球', 0, 'icons/9.gif');2.查詢數據表數據
select * from bg_cate cate3.代碼遞歸處理數據,是數據結構呈現為樹狀結構
function getTree($data, $pId) {$tree = [];foreach($data as $k => $v){if($v['cate_ParentId'] == $pId){ $v['children'] = getTree($data, $v['cate_Id']);$tree[] = $v;}}return $tree; } // 這里的0就是頂級分類中的cate_ParentId $tree = getTree($data, 0);前面屬于個人案例,下面分享一個網上比較簡潔的遞歸算法。
<?php // 這里為了篇幅就不展現運行效果,這段代碼確保是可以正常運行的。 直接使用即可。下面分享自己使用該demo實現的。 function genTree5($items) { foreach ($items as $item) $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']]; return isset($items[0]['son']) ? $items[0]['son'] : array(); } /*** 將數據格式化成樹形結構* @author Xuefen.Tong* @param array $items* @return array */ function genTree9($items) {$tree = array(); //格式化好的樹foreach ($items as $item)if (isset($items[$item['pid']]))$items[$item['pid']]['son'][] = &$items[$item['id']];else$tree[] = &$items[$item['id']];return $tree; } $items = array(1 => array('id' => 1, 'pid' => 0, 'name' => '江西省'),2 => array('id' => 2, 'pid' => 0, 'name' => '黑龍江省'),3 => array('id' => 3, 'pid' => 1, 'name' => '南昌市'),4 => array('id' => 4, 'pid' => 2, 'name' => '哈爾濱市'),5 => array('id' => 5, 'pid' => 2, 'name' => '雞西市'),6 => array('id' => 6, 'pid' => 4, 'name' => '香坊區'),7 => array('id' => 7, 'pid' => 4, 'name' => '南崗區'),8 => array('id' => 8, 'pid' => 6, 'name' => '和興路'),9 => array('id' => 9, 'pid' => 7, 'name' => '西大直街'),10 => array('id' => 10, 'pid' => 8, 'name' => '東北林業大學'),11 => array('id' => 11, 'pid' => 9, 'name' => '哈爾濱工業大學'),12 => array('id' => 12, 'pid' => 8, 'name' => '哈爾濱師范大學'),13 => array('id' => 13, 'pid' => 1, 'name' => '贛州市'),14 => array('id' => 14, 'pid' => 13, 'name' => '贛縣'),15 => array('id' => 15, 'pid' => 13, 'name' => '于都縣'),16 => array('id' => 16, 'pid' => 14, 'name' => '茅店鎮'),17 => array('id' => 17, 'pid' => 14, 'name' => '大田鄉'),18 => array('id' => 18, 'pid' => 16, 'name' => '義源村'),19 => array('id' => 19, 'pid' => 16, 'name' => '上壩村'), ); echo "<pre>"; print_r(genTree5($items)); print_r(genTree9($items));function getTree($data, $pid) {$list = [];foreach($data as $key=>$val) {if($val['pid'] == $pid){$val['children'] = getTree($data, $val['id']);$list[] = $val;}}return $list; } print_r(getTree($items, 0));以上內容希望幫助到大家,很多PHPer在進階的時候總會遇到一些問題和瓶頸,業務代碼寫多了沒有方向感,不知道該從那里入手去提升,對此我整理了一些資料,包括但不限于:分布式架構、高可擴展、高性能、高并發、服務器性能調優、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點高級進階干貨需要的可以免費分享給大家,需要
PHP進階架構師>>>視頻、面試文檔免費獲取?shimo.im或 者關注咱們下面的知乎專欄
PHP大神進階?zhuanlan.zhihu.com總結
以上是生活随笔為你收集整理的无限极分类不知pid_PHP实现无限极分类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python对象一定要删除引用吗_Pyt
- 下一篇: 展示动图_DNF:多GIF动图展示,暗夜