function index() { $list = M("Menu")->order("sort desc,id asc")->select(); vendor("Tree.Tree"); $tree = new Tree(); $tree->Tree($list, 'pid'); $list = $tree->getArray(); foreach ($list as $key => $val) { $list[$key]['count'] = M("Menu")->where("pid=" . $val['id'])->count(); } $this->assign('list', $list); $this->display('index'); }
public function getarraymenu() { $result = M("Menu")->select(); import('Tree', APP_PATH . 'Common', '.php'); $tree = new Tree(); // new 之前请记得包含tree文件! $tree->tree($result); // 数据格式请参考 tree方法上面的注释! // 如果使用数组, 请使用 getArray方法 $result = $tree->getArray(); // 下拉菜单选项使用 get_tree方法 // $tree->get_tree(); if ($result) { return $result; } }
public function add() { $id = I('get.id', 0, 'int'); $categoryModel = D('category'); if ($id) { //新增 $data = $categoryModel->getOne($id); $this->assign('data', $data); } //所有文类 $Allcategory = $categoryModel->getAllTree(); Vendor('AXE.tree.tree'); $tree = new \Tree($Allcategory); $Allcategory = $tree->getArray(); $this->assign('allcategory', $Allcategory); $this->display('add'); }
/** * 获取所有分类 */ private function getCategory() { //获取所有分类 $d = D('Category'); $category = $d->getAllTree(); Vendor('AXE.tree.tree'); $tree = new \Tree($category); $category = $tree->getArray(); return $category; }
<?php ini_set("display_errors", "On"); error_reporting(E_ALL | E_STRICT); header("Content-Type: text/html;charset=utf-8"); #require( "Helper.php" ); include 'Helper.php'; include "Tree.php"; echo Helper::createGuid(); $data = array(array('id' => '1', 'parentid' => 0, 'name' => '一级栏目一'), array('id' => '2', 'parentid' => 0, 'name' => '一级栏目二'), array('id' => '3', 'parentid' => 1, 'name' => '二级栏目一'), array('id' => '4', 'parentid' => 1, 'name' => '二级栏目二'), array('id' => '5', 'parentid' => 2, 'name' => '二级栏目三'), array('id' => '6', 'parentid' => 3, 'name' => '三级栏目一'), array('id' => '7', 'parentid' => 3, 'name' => '三级栏目二')); $tree = new Tree(); // new 之前请记得包含tree文件! $tree->tree($data); // 数据格式请参考 tree方法上面的注释! // 如果使用数组, 请使用 getArray方法 $arr = $tree->getArray(); //echo "<pre>"; //var_dump( $arr ); // 下拉菜单选项使用 get_tree方法 $str = "<option value=\$id>\$spacer\$name</option>"; $res = $tree->get_tree('parentid', $str); echo '<select>' . $res . '</select>';