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

首頁(yè) > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

php通過(guò)前序遍歷樹(shù)實(shí)現(xiàn)無(wú)需遞歸的無(wú)限極分類(lèi)

2024-04-25 20:38:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了php通過(guò)前序遍歷樹(shù)實(shí)現(xiàn)無(wú)需遞歸的無(wú)限極分類(lèi)。分享給大家供大家參考。具體如下:

大家通常都是使用遞歸實(shí)現(xiàn)無(wú)限極分類(lèi)都知道遞歸效率很低,下面介紹一種改進(jìn)的前序遍歷樹(shù)算法,不適用遞歸實(shí)現(xiàn)無(wú)限極分類(lèi),在大數(shù)據(jù)量實(shí)現(xiàn)樹(shù)狀層級(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, '編輯后的分類(lèi)', 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, '新的分類(lèi)的子分類(lèi)', 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();

//相鄰的兩條記錄的右值第一條的右值比第二條的大那么就是他的父類(lèi)

//我們用一個(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;

}

//顯示樹(shù)狀結(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í)分類(lèi)的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' => '新的分類(lèi)的子分類(lèi)',

'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ò)分類(lèi)id獲取分類(lèi)

$id = 3;

$category = $this->db->where('id', $id)->get('category')->row_array();

//計(jì)算$width

$width = $category['rgt'] - $category['lft'] + 1;

//1.刪除該條分類(lèi)

$this->db->delete('category', array('id' => $id));

//2.刪除左右值之間的所有分類(lèi)

$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' => '編輯后的分類(lèi)'

), array(

'id' => $id

));

echo 'edit success';

}

}

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 武清区| 盐城市| 施秉县| 郓城县| 开封县| 区。| 华容县| 洞头县| 鄂州市| 秦安县| 石林| 临湘市| 岑溪市| 都兰县| 屯门区| 翁源县| 璧山县| 鲁甸县| 郸城县| 健康| 铜川市| 宿迁市| 安龙县| 额尔古纳市| 巴彦淖尔市| 凤冈县| 凤阳县| 响水县| 喀喇沁旗| 酒泉市| 巴里| 唐山市| 嘉善县| 阳春市| 随州市| 蓝田县| 双柏县| 兰州市| 临猗县| 凌云县| 增城市|