用php和js实现简单的树结构(点击收缩与拉伸)
script type=text/javascript src=jquery.js/script script language=javascript type=text/javascript function show(eventName) { $(#eventName).children().toggle(); } /script //树的节点,id是每个节点的唯一标志,pid表示他的父节点 ,name表示节点的
//树的节点,id是每个节点的唯一标志,pid表示他的父节点 ,name表示节点的名称
$result[1] = array('id'=>2,'pid'=>0,'name'=>'公司2');
$result[2] = array('id'=>3,'pid'=>2,'name'=>'公司2的子公司1');
$result[3] = array('id'=>4,'pid'=>1,'name'=>'公司1的子公司1');
$result[4] = array('id'=>5,'pid'=>2,'name'=>'公司2的子公司2');
$result[5] = array('id'=>6,'pid'=>3,'name'=>'公司2的子公司1的子公司1');
$result[6] = array('id'=>7,'pid'=>3,'name'=>'公司2的子公司1的子公司2');
$result[7] = array('id'=>8,'pid'=>3,'name'=>'公司2的子公司1的子公司3');
$result[8] = array('id'=>9,'pid'=>8,'name'=>'公司2的子公司1的子公司3');
$result[9] = array('id'=>10,'pid'=>9,'name'=>'公司2的子公司1的子公司3');
//简易类
class tree
{
//创建树
function tree($rs,$idName,$pidName,$nodeName)
{$this->idName = $idName;//当前节点的id
$this->pidName = $pidName;//父节点的id
$this->nodeName = $nodeName;
$tree = array();
foreach((array)$rs as $k=>$v)
{
$tree[$v[$pidName]][] = $v;
}
$this->treeArray = $tree;
}
//显示树
{
if( $this->treeArray[$root] )
{
foreach($this->treeArray[$root] as $k=>$v)
{
$t = $v[$this->idName];
$p = $v[$this->pidName];
$name=$v[$this->nodeName];
$str = str_repeat(" ",$deep*4)."|-".str_repeat("-",$deep);
";
if($this->treeArray[$t] )
{
}
$html .="";
}
}
return $html;
}
}
$tree = new tree($result,'id','pid','name');
echo $tree->showTree(0,0);
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
