php只查询一次数据库分类表,实现无限极分类

数据库中字段和记录展示




$db = new Mysql();
$sql = 'select id,pid,name from test order by pid asc';
$result = $db->select($sql);
$result1 = $result;

$result 查询出来的数据格式
Array
(
    [0] => Array
        (
            [id] => 67
            [pid] => 0
            [name] => 系统设置
        )

    [1] => Array
        (
            [id] => 76
            [pid] => 0
            [name] => 其他设置
        )

    [2] => Array
        (
            [id] => 73
            [pid] => 0
            [name] => 用户管理
        )

    [3] => Array
        (
            [id] => 72
            [pid] => 67
            [name] => 导航 管理
        )

    [4] => Array
        (
            [id] => 71
            [pid] => 67
            [name] => 菜单管理
        )
	...
)		

$maxNum = 1000;//设置最大循环次数
    $count = -1;//设置计数
	//默认根节点内容
    $root = array(
        'id' => '0',
        'text' => 'root',
    );
	//辅助,主要作用用于检测节点是否存在
	//注:下面使用的技巧都是使用对象的引用,赋值的不是一个具体值,而是一个引用
    $existsMap = array(
        '0' => &$root,
    );
	//结果记录的长度
    $len = count($result1);
	//计数
    $num = 0;
	//遍历结果集
    while ($num < $len) {
        $count++;
		//如果计数器超过了最大循环次数就退出循环
        if ($count > $maxNum) break;
        $i = $count % $len;//取得下标,取莫的作用是防止下标超出边界
        $obj = $result[$i];//取得当前节点
        if (!$obj) continue;//不存在则跳过
        $pidObj = & $existsMap[$obj['pid']];//检测辅助数组中是否有父节点数据并赋引用值给pidObj
        if (!$pidObj) continue;
		//如果存在pidObj,则设置当前节点在existsMap中
        $existsMap[$obj['id']] = array(
            'id' => $obj['id'],
            'text' => $obj['name'],
        );
		//设置子节点
        if (!$pidObj['children']) {
            $pidObj['children'] = array();
        }
		//设置子节点为刚刚存在辅助数组中得引用
        $pidObj['children'][] = & $existsMap[$obj['id']];
        unset($result[$i]);
        $num++;
    }
	//根据自己的需求,决定是否返回root节点
    return $root['children'];

输出$root结果:
	Array
	(
    [id] => 0
    [text] => root
    [children] => Array
        (
            [0] => Array
                (
                    [id] => 67
                    [text] => 系统设置
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 72
                                    [text] => 导航 管理
                                )

                            [1] => Array
                                (
                                    [id] => 71
                                    [text] => 菜单管理
                                )

                            [2] => Array
                                (
                                    [id] => 70
                                    [text] => 配置管理
                                )

                            [3] => Array
                                (
                                    [id] => 69
                                    [text] => 模型管理
                                )

                            [4] => Array
                                (
                                    [id] => 68
                                    [text] => 分类设置
                                )

                        )

                )

            [1] => Array
                (
                    [id] => 76
                    [text] => 其他设置
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 78
                                    [text] => 其他设置2
                                )

                            [1] => Array
                                (
                                    [id] => 77
                                    [text] => 其他设置1
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 79
                                                    [text] => 其他设置1-1
                                                    [children] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [id] => 81
                                                                    [text] => 其他设置1-1-1
                                                                    [children] => Array
                                                                        (
                                                                            [0] => Array
                                                                                (
                                                                                    [id] => 82
                                                                                    [text] => 其他设置1-1-1-1
                                                                                    [children] => Array
                                                                                        (
                                                                                            [0] => Array
                                                                                                (
                                                                                                    [id] => 87
                                                                                                    [text] => 其他设置1-1-1-1-3
                                                                                                )

                                                                                            [1] => Array
                                                                                                (
                                                                                                    [id] => 65
                                                                                                    [text] => 其他设置1-1-1-1-2
                                                                                                )

                                                                                            [2] => Array
                                                                                                (
                                                                                                    [id] => 83
                                                                                                    [text] => 其他设置1-1-1-1-1
                                                                                                )

                                                                                            [3] => Array
                                                                                                (
                                                                                                    [id] => 88
                                                                                                    [text] => 其他设置1-1-1-1-4
                                                                                                )

                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [id] => 80
                                                    [text] => 其他设置1-2
                                                    [children] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [id] => 84
                                                                    [text] => 其他设置1-2-1
                                                                )

                                                            [1] => Array
                                                                (
                                                                    [id] => 85
                                                                    [text] => 其他设置1-2-2
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

            [2] => Array
                (
                    [id] => 73
                    [text] => 用户管理
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 74
                                    [text] => 用户列表
                                )

                            [1] => Array
                                (
                                    [id] => 75
                                    [text] => 权限分配
                                )

                        )

                )

        )

)


前端效果展示



javascript写法:

var list = [{"id":"67","pid":"0","name":"\u7cfb\u7edf\u8bbe\u7f6e"},{"id":"76","pid":"0","name":"\u5176\u4ed6\u8bbe\u7f6e"},{"id":"73","pid":"0","name":"\u7528\u6237\u7ba1\u7406"},{"id":"72","pid":"67","name":"\u5bfc\u822a \u7ba1\u7406"},{"id":"71","pid":"67","name":"\u83dc\u5355\u7ba1\u7406"},{"id":"70","pid":"67","name":"\u914d\u7f6e\u7ba1\u7406"},{"id":"69","pid":"67","name":"\u6a21\u578b\u7ba1\u7406"},{"id":"68","pid":"67","name":"\u5206\u7c7b\u8bbe\u7f6e"},{"id":"74","pid":"73","name":"\u7528\u6237\u5217\u8868"},{"id":"75","pid":"73","name":"\u6743\u9650\u5206\u914d"},{"id":"78","pid":"76","name":"\u5176\u4ed6\u8bbe\u7f6e2"},{"id":"77","pid":"76","name":"\u5176\u4ed6\u8bbe\u7f6e1"},{"id":"79","pid":"77","name":"\u5176\u4ed6\u8bbe\u7f6e1-1"},{"id":"80","pid":"77","name":"\u5176\u4ed6\u8bbe\u7f6e1-2"},{"id":"81","pid":"79","name":"\u5176\u4ed6\u8bbe\u7f6e1-1-1"},{"id":"84","pid":"80","name":"\u5176\u4ed6\u8bbe\u7f6e1-2-1"},{"id":"85","pid":"80","name":"\u5176\u4ed6\u8bbe\u7f6e1-2-2"},{"id":"82","pid":"81","name":"\u5176\u4ed6\u8bbe\u7f6e1-1-1-1"},{"id":"87","pid":"82","name":"\u5176\u4ed6\u8bbe\u7f6e1-1-1-1-3"},{"id":"65","pid":"82","name":"\u5176\u4ed6\u8bbe\u7f6e1-1-1-1-2"},{"id":"83","pid":"82","name":"\u5176\u4ed6\u8bbe\u7f6e1-1-1-1-1"},{"id":"88","pid":"82","name":"\u5176\u4ed6\u8bbe\u7f6e1-1-1-1-4"}];


var maxNum = 1000;
var count = -1;
var root = {id:"0", text:"root"};
var existsMap = {"0":root};
var len = list.length;
var num = 0;
while(num < len) {
	
	count ++;
	if(maxNum < count) {
		break;
	}

	var i = count % len;
	var obj = list[i];	
		
	if(obj == -1) {
		continue;
	}
	
	var pidObj = existsMap[obj.pid];
	if(pidObj == null) {
		continue;
	}
	
	var treeNode = {id:obj.id, text:obj.name};
	existsMap[obj.id] = treeNode;
	
	if(pidObj['children'] == null) {
		pidObj['children'] = [];
	}
	pidObj['children'].push(treeNode);
	
	list[i] = -1;
	num ++;	
}

console.log(root)


总结:要点就是使用对象的引用

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值