Home >Backend Development >PHP Tutorial >迎国庆,送你一棵树_PHP

迎国庆,送你一棵树_PHP

WBOY
WBOYOriginal
2016-06-01 12:36:411153browse

class Tree {
var $data = array();
var $tpl = '$块
';
var $block = ' $主图标 $副图标 $文字 $子树 ';

var $images = array( // 节点图片
array("images/tree_L.gif","images/tree_T.gif","images/icon-page.gif"),
array("images/OpenFolder_L.gif","images/OpenFolder_T.gif","images/FolderClose.gif","images/FolderOpen.gif"),
array("images/CloseFolder_L.gif","images/CloseFolder_T.gif","images/FolderClose.gif","images/FolderOpen.gif"),
array("none","images/tree_I.gif"),
);

var $bond = array("","数据加载中...");
var $all = false; // 是否产生一棵子树

/**
* Tree 构造函数
*/
function Tree() {
}
/**
* table 方法
* 功能 产生以表格方式组织的数据
* 说明 主要用于调试
*/
function table($format="",$head="") {
if(count($this->data) out = "";
$out = "";
if($head == "") {
foreach($this->data as $v) break;
$head = array_keys($v);
}
foreach($head as $v)
$out .= "";
$out .= "";
foreach($this->data as $value) {
$out .= "";
foreach($head as $k)
$out .= "";
$out .= "";
}
return $this->out = "$out
$v
$value[$k]
";
}
/**
* node_all 方法
* 功能 根据模板构造全部数据
*/
function node_all($id, $key=array("id","pid","text","link")) {
$this->all = true;
return $this->node($id,$key);
}
/**
* index 方法
* 功能 索引树
*/
function index($start=0, $key=array("id","pid")) {
$fun = create_function('$a,$b'
,"if(\$a[$key[1]] == \$b[$key[1]]) return 0;"
."return \$a[$key[1]] > \$b[$key[1]]?1:-1;");
$out = array();
foreach($this->data as $k=>$v) {
if($v[$key[1]] == $start)
$out[] = $v;
else
$this->insert($v,&$out,$key);
}
return $this->data = $out;
}
/**
* insert 方法
* 功能 插入节点
*/
function insert($value,$array="",$key=array("id","pid")) {
if($array == "") $array =& $this->data;
if(count($array) $array[] = $value;
return true;
}
$k = $m = $n = 0;
$ar = array();
foreach($array as $t) {
$k++;
if($t[$key[0]] == $value[$key[1]]) $m = $k;
if($t[$key[1]] == $value[$key[1]] || in_array($t[$key[1]],$ar)) {
$n = $k;
$ar[] = $t[$key[0]];
}
}
$k = max($m,$n);
if($k == 0) return false;
$array = array_merge(array_slice($array,0,$k),array($value),array_slice($array,$k));
return true;
}
/**
* update 方法
* 功能 修改节点
*/
function update() {
}
/**
* delete 方法
* 功能 删除节点
*/
function delete() {
}
/**
* move 方法
* 功能 移动节点
*/
function move() {
}
/**
* display 方法
* 功能 显示
*/
function display($type="node_all",$key=array("id","pid","text","link")) {
switch($type) {
case "table":
$this->table();
break;
case "node_all":
$this->out = $this->node_all(0,$key);
break;
}
echo $this->out;
$this->javascript();
}
function javascript() {
static $key;
if(! empty($key)) return;
echo <script><br />function tree_onclick() {<br /> el = event.srcElement;<br /> switch(event.srcElement.tagName) {<br /> case "A":<br /> case "IMG":<br /> while(el.tagName != "TR") el = el.parentElement;<br /> break;<br /> default:<br /> return;<br /> }<br /> if(el.bs == 0) return; // bs=0 表示是叶节点; =1 为L接; >1 为T接<br /> var pos = el.rowIndex;<br /> var el1 = el.parentElement.rows[pos+1];<br /> if(el1.style.display == "block") {<br /> el1.style.display = "none";<br /> el.cells[0].children[0].src = (el.bs==1?images[0][0]:images[0][1]);<br /> if(el.cells[1].children[0]) el.cells[1].children[0].src = images[1][2];<br /> }else {<br /> el1.style.display = "block";<br /> el.cells[0].children[0].src = (el.bs==1?images[1][0]:images[1][1]);<br /> if(el.cells[1].children[0]) el.cells[1].children[0].src = images[1][3];<br /> }<br /> el = el1.cells[1];<br /> if(el.innerHTML == bond) Tree_load.src = "?node="+el.value;<br />}<br />myload = 0;<br />function tree_load_end() { if(myload != 0) el.innerHTML = myload; }<br /></script>

JS;
$js = "<script>\n";<br /> $js .= "images = new Array(\nnew Array(";<br /> foreach($this->images[1] as $v) $js .= "'$v',";<br /> $js = substr($js,0,-1)."),\n";<br /> $js .= "new Array(";<br /> foreach($this->images[2] as $v) $js .= "'$v',";<br /> $js = substr($js,0,-1).")\n);\n";<br /> $js .= "bond = '{$this->bond[1]}';";<br /> $js .= "</script>";
echo $js;
$key = 1;
}
function find($id,$key=array("id","pid")) {
foreach($this->data as $v)
if($v[$key[1]] == $id) return true;
return false;
}
/**
* filter方法
* 功能 返回$id的一级子集
*/
function filter($id,$key=array("id","pid")) {
$cmd = create_function('$var',"return(\$var[$key[1]]=='$id');");
return array_filter($this->data,$cmd);
}
/**
* node 方法
* 功能 根据模板构造指定节点数据
*/
function node($id,$key=array("id","pid","text","link")) {
$ar = $this->filter($id,$key);
if(($n = count($ar)) == 0) return "";
$块 = "";
foreach($ar as $value) {
$n--;
$p = sign($value[node_childs]);
$可见 = "none";//"block";
$标识 = $p==0 ? $p : $p+$n;
$主图标 = "迎国庆,送你一棵树_PHPimages[$p][sign($n)]}\">";
$副图标 = empty($this->images[$p][2]) ? "" : "迎国庆,送你一棵树_PHPimages[$p][2]}\">";
$文字 = empty($value[$key[3]]) ? $value[$key[2]] : "{$value[$key[2]]}";
$连线 = $this->images[3][sign($n)];

$编号 = $value[id];
$子树 = $this->all ? $this->node($value[$key[0]],$key) : $this->bond[$p];
$块 .= eval("return \"".AddSlashes($this->block)."\";");
}
return eval("return \"\n".AddSlashes($this->tpl)."\";");
}
/**
* build方法
* 功能 计算各节点的一级子节点数,用附加的node_childs表示
*/
function build($key=array("id","pid")) {
foreach($this->data as $k=>$v) {
$this->data[$k][node_childs] = count($this->filter($v[$key[0]]));
}
}
}

class exTree extends Tree {
var $images = array( // 节点图片
array("images/icon-page.gif"), // 无子节点
array("images/OpenFolder.gif","images/OpenFolder.gif"), // 子节点关闭
array("images/CloseFolder.gif","images/CloseFolder.gif") ); // 子节点打开
}

$t = new Tree;
//$t = new exTree; // 去掉注释看看效果

$ar = array(
array(id=>1,pid=>0,text=>"PHP",link=>"#"),
array(id=>2,pid=>1,text=>"函数"),
array(id=>5,pid=>1,text=>"类"),
array(id=>3,pid=>0,text=>"ASP"),
array(id=>4,pid=>2,text=>"d"),
array(id=>6,pid=>3,text=>"66"),
array(id=>7,pid=>0,text=>"JSP")
);
//$t->data = $ar;

foreach($ar as $v)
$t->insert($v);

$t->insert(array(id=>8,pid=>4,text=>"abc",link=>"aaaa"));

$t->build();

if(! isset($_GET['node'])) {
$t->display("table");
if(0) // 1 一次性加载
$t->display();
else { // 分步加载
$t->javascript();
echo $t->node(0);
}
}else { // 分步加载时返回指定节点
$s = $t->node($_GET['node']);
$s = preg_replace("/\r?\n/","\\n",$s);
echo "myload = '$s'";
}
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn