php layui树形实现,树形菜单文档 - layui.tree-示例

/**

* Created by 小雨在线.

* User: 飛天

* Date: 2017/8/19 0019

* Time: 16:04

*/

require('../PHPTree.class.php');

//原始数据, 从数据库读出

/*$data = array(

array(

'id'=>1,

'name'=>'book',

'parent_id'=>0

),

array(

'id'=>2,

'name'=>'music',

'parent_id'=>0

),

array(

'id'=>3,

'name'=>'book1',

'parent_id'=>1

),

array(

'id'=>4,

'name'=>'book2',

'parent_id'=>3

)

);

$r = PHPTree::makeTree($data);

echo json_encode($r);

*/

define("HOST", "aaaa"); //主机名

define("USER", "bbbb"); //账号

define("PASS", "ccccc"); //密码

define("DBNAME", "ddd-database"); //数据库名

$conn = new mysqli(HOST, USER, PASS);

$conn->select_db(DBNAME);

$sql = "SELECT catename AS name,id,pid AS pId,cateorder FROM wuxianfenlei_deepcate ORDER BY id DESC";

$result = $conn->query($sql);

$data1 = [];

while ($row = $result->fetch_assoc()) {

$data1[] = $row;

}

//echo json_encode($data1);

$r = PHPTree::makeTree($data1, array(

'primary_key' => 'id',

'parent_key' => 'pId'

));

echo json_encode($r);

/**

* @name PHPTree

* @author crazymus < QQ:291445576 >

* @des PHP生成树形结构,无限多级分类

* @version 1.2.0

* @Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

* @updated 2015-08-26

*/

class PHPTree

{

protected static $config = array(

/* 主键 */

'primary_key' => 'id',

/* 父键 */

'parent_key' => 'parent_id',

/* 展开属性 */

'expanded_key' => 'expanded',

/* 叶子节点属性 */

'leaf_key' => 'leaf',

/* 孩子节点属性 */

'children_key' => 'children',

/* 是否展开子节点 */

'expanded' => false

);

/* 结果集 */

protected static $result = array();

/* 层次暂存 */

protected static $level = array();

/**

* @name 生成树形结构

* @param array 二维数组

* @return mixed 多维数组

*/

public static function makeTree($data, $options = array())

{

$dataset = self::buildData($data, $options);

$r = self::makeTreeCore(0, $dataset, 'normal');

return $r;

}

/* 生成线性结构, 便于HTML输出, 参数同上 */

public static function makeTreeForHtml($data, $options = array())

{

$dataset = self::buildData($data, $options);

$r = self::makeTreeCore(0, $dataset, 'linear');

return $r;

}

/* 格式化数据, 私有方法 */

private static function buildData($data, $options)

{

$config = array_merge(self::$config, $options);

self::$config = $config;

extract($config);

$r = array();

foreach ($data as $item) {

/** @var TYPE_NAME $primary_key */

$id = $item[$primary_key];

/** @var TYPE_NAME $parent_key */

$parent_id = $item[$parent_key];

$r[$parent_id][$id] = $item;

}

return $r;

}

/* 生成树核心, 私有方法 */

private static function makeTreeCore($index, $data, $type = 'linear')

{

extract(self::$config);

foreach ($data[$index] as $id => $item) {

if ($type == 'normal') {

if (isset($data[$id])) {

/** @var TYPE_NAME $expanded_key */

$item[$expanded_key] = self::$config['expanded'];

/** @var TYPE_NAME $children_key */

$item[$children_key] = self::makeTreeCore($id, $data, $type);

} else {

/** @var TYPE_NAME $leaf_key */

$item[$leaf_key] = true;

}

$r[] = $item;

} else if ($type == 'linear') {

/** @var TYPE_NAME $parent_key */

$parent_id = $item[$parent_key];

self::$level[$id] = $index == 0 ? 0 : self::$level[$parent_id] + 1;

$item['level'] = self::$level[$id];

self::$result[] = $item;

if (isset($data[$id])) {

self::makeTreeCore($id, $data, $type);

}

$r = self::$result;

}

}

return $r;

}

}

?>

效果:

0818b9ca8b590ca3270a3433284dd417.png

参考资料:

http://www.layui.com/doc/modules/tree.html

https://my.oschina.net/crazymus/blog/491174

http://git.oschina.net/jiusem/PHPTree

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值