-- -- 表的結構 `category` -- CREATE TABLE IF NOT EXISTS `category` ( `id` int(11) NOT NULL AUTO_INCREMENT, `type` int(11) NOT NULL COMMENT '1為文章類型2為產品類型3為下載類型', `title` varchar(50) NOT NULL, `lft` int(11) NOT NULL, `rgt` int(11) NOT NULL, `lorder` int(11) NOT NULL COMMENT '排序', `create_time` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ; -- -- 匯出表中的資料 `category` -- INSERT INTO `category` (`id`, `type`, `title`, `lft`, `rgt`, `lorder`, `create_time`) VALUES (1, 1, '頂級欄目', 1, 18, 1, 1261964806), (2, 1, '公司簡介', 14, 17, 50, 1264586212), (3, 1, '新聞', 12, 13, 50, 1264586226), (4, 2, '公司產品', 10, 11, 50, 1264586249), (5, 1, '榮譽資質', 8, 9, 50, 1264586270), (6, 3, '資料下載', 6, 7, 50, 1264586295), (7, 1, '人才招聘', 4, 5, 50, 1264586314), (8, 1, '留言板', 2, 3, 50, 1264586884), (9, 1, '總裁', 15, 16, 50, 1267771951); /** * 顯示樹,把所有的節點都顯示出來。 * 1、先得到根結點的左右值(預設根節點的title為“頂級目錄”)。 * 2、查詢左右值在根節點的左右值範圍內的記錄,並且根據左值排序。 * 3、如果本次記錄右值大於前次記錄的右值則為子分類,輸出時候加空格。 * @return array **/ function display_tree(){ //獲得root左邊和右邊的值 $arr_lr = $this->category->where("title = '頂級欄目'")->find(); //print_r($arr_lr); if($arr_lr){ $right = array(); $arr_tree = $this->category->query("SELECT id, type, title, rgt FROM category WHERE lft >= ". $arr_lr['lft'] ." AND lft <=".$arr_lr['rgt']." ORDER BY lft"); foreach($arr_tree as $v){ if(count($right)){ while ($right[count($right) -1] < $v['rgt']){ array_pop($right); } } $title = $v['title']; if(count($right)){ $title = '|-'.$title; } $arr_list[] = array('id' => $v['id'], 'type' => $type, 'title' => str_repeat(' ', count($right)).$title, 'name' =>$v['title']); $right[] = $v['rgt']; } return $arr_list; } } |