php通過(guò)前序遍歷樹實(shí)現(xiàn)無(wú)需遞歸的無(wú)限極分類
本文實(shí)例講述了php通過(guò)前序遍歷樹實(shí)現(xiàn)無(wú)需遞歸的無(wú)限極分類。分享給大家供大家參考。具體如下:
大家通常都是使用遞歸實(shí)現(xiàn)無(wú)限極分類都知道遞歸效率很低,下面介紹一種改進(jìn)的前序遍歷樹算法,不適用遞歸實(shí)現(xiàn)無(wú)限極分類,在大數(shù)據(jù)量實(shí)現(xiàn)樹狀層級(jí)結(jié)構(gòu)的時(shí)候效率更高。
sql代碼如下:
CREATE TABLE IF NOT EXISTS `category` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(50) NOT NULL, `lft` int(11) NOT NULL, `rgt` int(11) NOT NULL, `order` int(11) NOT NULL COMMENT '排序', `create_time` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ; -- -- 轉(zhuǎn)存表中的數(shù)據(jù) `category` -- INSERT INTO `category` (`id`, `title`, `lft`, `rgt`, `order`, `create_time`) VALUES (1, '頂級(jí)欄目', 1, 20, 1, 1261964806), (2, '編輯后的分類', 16, 19, 50, 1264586212), (4, '公司產(chǎn)品', 10, 15, 50, 1264586249), (5, '榮譽(yù)資質(zhì)', 8, 9, 50, 1264586270), (6, '資料下載', 6, 7, 50, 1264586295), (7, '人才招聘', 4, 5, 50, 1264586314), (8, '留言板', 2, 3, 50, 1264586884), (9, '總裁', 17, 18, 50, 1267771951), (10, '新的分類的子分類', 11, 14, 0, 1400044841), (11, 'PHP點(diǎn)點(diǎn)通-http://www.phpddt.com', 12, 13, 0, 1400044901);
php代碼如下:
<?php
/**
* 純屬測(cè)試
*
* @author Mckee
* @link http://www.phpddt.com
*/
class Category extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function view()
{
$lists = $this->db->order_by('lft', 'asc')->get('category')->result_array();
//相鄰的兩條記錄的右值第一條的右值比第二條的大那么就是他的父類
//我們用一個(gè)數(shù)組來(lái)存儲(chǔ)上一條記錄的右值,再把它和本條記錄的右值比較,如果前者比后者小,說(shuō)明不是父子關(guān)系,就用array_pop彈出數(shù)組,否則就保留
//兩個(gè)循環(huán)而已,沒(méi)有遞歸
$parent = array();
$arr_list = array();
foreach($lists as $item){
if(count($parent)){
while (count($parent) -1 > 0 && $parent[count($parent) -1]['rgt'] < $item['rgt']){
array_pop($parent);
}
}
$item['depath'] = count($parent);
$parent[] = $item;
$arr_list[]= $item;
}
//顯示樹狀結(jié)構(gòu)
foreach($arr_list as $a)
{
echo str_repeat('--', $a['depath']) . $a['title'] . '<br />';
}
}
/**
*
* 插入操作很簡(jiǎn)單找到其父節(jié)點(diǎn),之后把左值和右值大于父節(jié)點(diǎn)左值的節(jié)點(diǎn)的左右值加上2,之后再插入本節(jié)點(diǎn),左右值分別為父節(jié)點(diǎn)左值加一和加二
*/
public function add()
{
//獲取到父級(jí)分類的id
$parent_id = 10;
$parent_category = $this->db->where('id', $parent_id)->get('category')->row_array();
//1.左值和右值大于父節(jié)點(diǎn)左值的節(jié)點(diǎn)的左右值加上2
$this->db->set('lft', 'lft + 2', FALSE)->where(array('lft >' => $parent_category['lft']))->update('category');
$this->db->set('rgt', 'rgt + 2', FALSE)->where(array('rgt >' => $parent_category['lft']))->update('category');
//2.插入新的節(jié)點(diǎn)
$this->db->insert('category', array(
'title' => '新的分類的子分類',
'lft' => $parent_category['lft'] + 1,
'rgt' => $parent_category['lft'] + 2,
'order' => 0,
'create_time' => time()
));
echo 'add success';
}
/**
* 刪除
*
* //1.得到刪除的節(jié)點(diǎn),將右值減去左值然后加1,得到值$width = $rgt - $lft + 1;
* //2.刪除左右值之間的所有節(jié)點(diǎn)
* //3.修改條件為大于本節(jié)點(diǎn)右值的所有節(jié)點(diǎn),操作為把他們的左右值都減去$width
*/
public function delete()
{
//通過(guò)分類id獲取分類
$id = 3;
$category = $this->db->where('id', $id)->get('category')->row_array();
//計(jì)算$width
$width = $category['rgt'] - $category['lft'] + 1;
//1.刪除該條分類
$this->db->delete('category', array('id' => $id));
//2.刪除左右值之間的所有分類
$this->db->delete('category', array('lft >' => $category['lft'], 'lft <' => $category['rgt']));
//3.修改其它節(jié)點(diǎn)的值
$this->db->set('lft', "lft - {$width}", FALSE)->where(array('lft >' => $category['rgt']))->update('category');
$this->db->set('rgt', "rgt - {$width}", FALSE)->where(array('rgt >' => $category['rgt']))->update('category');
echo 'delete success';
}
//編輯,
public function edit()
{
//不用說(shuō)了, 直接通過(guò)id編輯
$id = 2;
$this->db->update('category', array(
'title' => '編輯后的分類'
), array(
'id' => $id
));
echo 'edit success';
}
}
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- php無(wú)限極分類實(shí)現(xiàn)的兩種解決方法
- PHP超牛逼無(wú)限極分類生成樹方法
- php無(wú)限極分類遞歸排序?qū)崿F(xiàn)方法
- php 無(wú)限極分類
- PHP實(shí)現(xiàn)無(wú)限極分類生成分類樹的方法
- PHP無(wú)限極分類函數(shù)的實(shí)現(xiàn)方法詳解
- 深入淺析PHP無(wú)限極分類的案例教程
- php實(shí)現(xiàn)smarty模板無(wú)限極分類的方法
- thinkphp5使用無(wú)限極分類
- 淺談PHP無(wú)限極分類原理
- PHP實(shí)現(xiàn)無(wú)限極分類的兩種方式示例【遞歸和引用方式】
- php無(wú)限極分類實(shí)現(xiàn)方法分析
相關(guān)文章
學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)抽象工廠模式
這篇文章主要介紹了php設(shè)計(jì)模式中的抽象工廠模式,使用php實(shí)現(xiàn)抽象工廠模式,感興趣的小伙伴們可以參考一下2015-12-12
php函數(shù)之子字符串替換 str_replace
php函數(shù)str_replace: 返回一個(gè)字符串或者數(shù)組。該字符串或數(shù)組是將 subject 中全部的 search 都被 replace 替換之后的結(jié)果。2011-03-03
PHP 存儲(chǔ)文本換行實(shí)現(xiàn)方法
在文本存儲(chǔ)時(shí)使用\n如果發(fā)現(xiàn)沒(méi)有效果, 這時(shí)可以使用\r\n就可以了,希望對(duì)有需要的朋友有所幫助。2010-01-01
解析如何去掉CodeIgniter URL中的index.php
本篇文章是對(duì)如何去掉CodeIgniter URL中index.php的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP中單引號(hào)和雙引號(hào)的區(qū)別詳解
看好多代碼有時(shí)候用單引號(hào)或雙引號(hào)實(shí)現(xiàn)包含字符串的內(nèi)容,其實(shí)簡(jiǎn)單個(gè)概括下雙引號(hào)中的變量可以解析,單引號(hào)就是絕對(duì)的字符串,下面這篇文章主要給大家介紹了關(guān)于PHP中單引號(hào)和雙引號(hào)區(qū)別的相關(guān)資料,需要的朋友可以參考下2023-01-01

