微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php+ajax 实现无限树列表

首先介绍我实现的是xhprof插件的日志转为无限树状图,先看效果图:

废话不多说,直接看代码:(辛辛苦苦敲了好久才搞定,逻辑比较多,新手多揣摩)

控制器:

PHP

namespace AdminController;

class XhprofController extends AdminbaseController

{

function _initialize()

{

parent::_initialize();

}

//获取当前文件目录

public function index()

{

$file = scandir("./public/xhprof");

$str = ".xhprof";

foreach ($file as $value) {

if (preg_replace("/($str)/","",$value) != $value) {

$dir[] = $value;

}

}

$this->assign("dir",$dir);

$this->display();

}

//获取适当的结构

public function tree($file_name)

{

$file_path = "./public/xhprof/$file_name";

$file_name = str_replace('.','',$file_name);

if (F($file_name)) {

$mainTree = F($file_name);

} else {

if (file_exists($file_path)) {

$fp = fopen($file_path,"r");

$str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来

$array = json_decode($str,true);

$array_keys = array_keys($array);

//按照调用者整理数据

$calls = [];

foreach ($array_keys as $array_key) {

$caller = explode("==>",$array_key)[0];

$beCall = explode("==>",$array_key)[1];

if (!$calls[$caller]) {

$calls[$caller] = [

"caller" => $caller,

"beCalls" => []

];

}

$status = 0;//是否有子目录

foreach ($array_keys as $keyv) {

if (strpos($keyv,$beCall . '==>') !== false && strpos($keyv,"@") === false && $beCall !== null && $beCall !== "count") {

$status = 1;

}

}

if ($beCall !== null) {

$calls[$caller]['beCalls'][] = array('name' => $beCall,"status" => $status,'data' => $array[$array_key]);

}

}

foreach ($calls as &$v) {

$tmpAll = 0;

foreach ($v['beCalls'] as $vv) {

$tmpAll += $vv['data']['wt'];

}

foreach ($v['beCalls'] as &$vv1) {

$vv1['data']['wtp'] = round($vv1['data']['wt'] * 100 / $tmpAll,2);

$vv1['wtp'] = $vv1['data']['wt'];

}

}

$mainTree = $calls;

//缓存$mainTree

F($file_name,$mainTree);

} else {

$mainTree = "";

}

}

array_multisort(array_column($mainTree["main()"]["beCalls"],"wtp"),SORT_DESC,array_column($mainTree["main()"]["beCalls"],"name"),$mainTree["main()"]["beCalls"]);

$this->assign("main",$array["main()"]);

$this->assign("mainTree",$mainTree["main()"]);

$this->assign("level",1);

$this->assign("file_name",$file_name);

$this->assign("file_path",$file_path);

$this->display();

}

public function ajax()

{

$note = htmlspecialchars_decode(I("post.note"));

$file_name = htmlspecialchars_decode(I("post.file_name"));

$file_path = htmlspecialchars_decode(I("post.file_path"));

//处理$mainTree

if (F($file_name)) {

$mainTree = F($file_name);

} else {

if (file_exists($file_path)) {

$fp = fopen($file_path,$mainTree);

} else {

$mainTree = "";

}

}

array_multisort(array_column($mainTree[$note]["beCalls"],array_column($mainTree[$note]["beCalls"],$mainTree[$note]["beCalls"]);

$this->ajaxReturn($mainTree[$note]);

}

}

veiw界面:

<Meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

Xhprof数据分析

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐