<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_DEPRECATED);
mysql_connect('localhost','root','123456') or die('链接数据库失败!'.mysql_error());
mysql_select_db('test');
mysql_set_charset('utf8');
if('POST'==$_SERVER['REQUEST_METHOD']){
header('Content-Type: application/json; charset=utf-8');
$id=$_POST['id']+0;
$id2=$_POST['id2']+0;
$pid=$_POST['pid']+0;
$node=getNodeById($id);
$pnode=getNodeById($pid);
if($_POST['isdrop']){//删除
if($node){
$num=$node->R-$node->L+1;
mysql_query('DELETE FROM `tree_lr` WHERE L>='.$node->L.' AND R<='.$node->R) or die('删除节点错误!'.mysql_error());
mysql_query('UPDATE `tree_lr` SET L=L-'.$num.' WHERE L>'.$node->R) or die('删除节点时更新L值错误!'.mysql_error());
mysql_query('UPDATE `tree_lr` SET R=R-'.$num.' WHERE R>'.$node->R) or die('删除节点时更新R值错误!'.mysql_error());
exit('true');
}else{
die('<font color="red">删除的节点不存在!</font>');
}
}elseif($_POST['isswap']){//交换两节点位置
$node2=getNodeById($id2);
if($node && $node2){
$num=$node->R-$node->L+1;
$num2=$node2->R-$node2->L+1;
mysql_query('UPDATE `tree_lr` SET dorder=IF(dorder='.$node->dorder.','.$node2->dorder.','.$node->dorder.') WHERE id IN('.$id.','.$id2.')') or die('交换节点时更新dorder值错误!'.mysql_error());
mysql_query('UPDATE `tree_lr` SET L=L+IF(L>'.$node->R.',-'.$num.','.$num2.'), R=R+IF(R>'.$node->R.',-'.$num.','.$num2.') WHERE (L>='.$node->L.' AND R<='.$node->R.') OR (L>='.$node2->L.' AND R<='.$node2->R.')') or die('交换节点时更新L值错误!'.mysql_error());
exit('true');
}else{
die('<font color="red">交换的节点至少其中之一不存在!</font>');
}
}elseif($_POST['ismove']){//移动到...
if($node && $pnode){//从后移到前...
$maxR=mysql_result(mysql_query('SELECT MAX(R) FROM `tree_lr`'),0);
$num=$node->R-$node->L+1;
mysql_query('UPDATE `tree_lr` SET pid='.$pid.' WHERE id='.$id) or die('交换节点时更新pid值错误!'.mysql_error());
//把节点放到maxR最后用来暂存LR
$lastMaxR=$maxR-$node->L+1;
mysql_query('UPDATE `tree_lr` SET L=L+'.$lastMaxR.', R=R+'.$lastMaxR.' WHERE L>='.$node->L.' AND R<='.$node->R) or die('交换节点时更新LR+lastMaxR值错误!'.mysql_error());
if($node->R<$maxR){//调整节点后的LR值
mysql_query('UPDATE `tree_lr` SET L=L-'.$num.' WHERE L<='.$maxR.' AND L>'.$node->R) or die('交换节点时更新L值错误!'.mysql_error());
mysql_query('UPDATE `tree_lr` SET R=R-'.$num.' WHERE L<='.$maxR.' AND R>='.$node->R) or die('交换节点时更新L值错误!'.mysql_error());
}
if($pnode->L>$node->L){//纠正从前移到后的pnode的LR值
$pnode->L-=$num;
$pnode->R-=$num;
}
//让父节点腾出LR值空间
mysql_query('UPDATE `tree_lr` SET L=L+'.$num.' WHERE L<='.$maxR.' AND L>'.$pnode->R) or die('交换节点时更新L值错误!'.mysql_error());
mysql_query('UPDATE `tree_lr` SET R=R+'.$num.' WHERE L<='.$maxR.' AND R>='.$pnode->R) or die('交换节点时更新L值错误!'.mysql_error());
//把暂存节点LR放到父节点腾出的LR值空间
$pnodeR=-$maxR+$pnode->R-1;
mysql_query('UPDATE `tree_lr` SET L=L+'.$pnodeR.', R=R+'.$pnodeR.' WHERE L>'.$maxR) or die('交换节点时更新LR+lastMaxR值错误!'.mysql_error());
exit('true');
}elseif($node){//从移到最后...
$maxR=mysql_result(mysql_query('SELECT MAX(R) FROM `tree_lr`'),0);
$num=$node->R-$node->L+1;
mysql_query('UPDATE `tree_lr` SET pid='.$pid.' WHERE id='.$id) or die('交换节点时更新pid值错误!'.mysql_error());
//把节点放到maxR最后用来暂存LR
$lastMaxR=$maxR-$node->L+1;
mysql_query('UPDATE `tree_lr` SET L=L+'.$lastMaxR.', R=R+'.$lastMaxR.' WHERE L>='.$node->L.' AND R<='.$node->R) or die('交换节点时更新LR+lastMaxR值错误!'.mysql_error());
mysql_query('UPDATE `tree_lr` SET L=L-'.$num.' WHERE L>'.$node->R) or die('交换节点时更新L值错误!'.mysql_error());
mysql_query('UPDATE `tree_lr` SET R=R-'.$num.' WHERE R>='.$node->R) or die('交换节点时更新L值错误!'.mysql_error());
exit('true');
}else{
die('<font color="red">交换的节点至少其中之一不存在!</font>');
}
}elseif($id){//编辑
if($node){
mysql_query('UPDATE `tree_lr` SET `name`=\''.addslashes($_POST['name']).'\' WHERE id='.$id) or die('修改节点是更新name值错误!'.mysql_error());
exit('true');
}else{
die('<font color="red">编辑的节点不存在!</font>');
}
}else{//添加
if($pid){
$pnode=getNodeById($pid);
if($pnode){
mysql_query('UPDATE `tree_lr` SET L=L+2 WHERE L>'.$pnode->R) or die('添加节点时更新L值错误!'.mysql_error());
mysql_query('UPDATE `tree_lr` SET R=R+2 WHERE R>='.$pnode->R) or die('添加节点时更新R值错误!'.mysql_error());
mysql_query('INSERT INTO `tree_lr` (pid,`name`,L,R)VALUES('.$pid.',\''.addslashes($_POST['name']).'\','.$pnode->R.','.$pnode->R.'+1)') or die('添加节点错误!'.mysql_error());
mysql_query('UPDATE `tree_lr` SET dorder=id WHERE id=last_insert_id()') or die('添加节点时更新dorder值错误!'.mysql_error());
die('"添加成功!"');
}else{
die('<font color="red">父节点不存在!</font>');
}
}else{
$maxR=mysql_result(mysql_query('SELECT MAX(R) FROM `tree_lr`'),0);
mysql_query('INSERT INTO `tree_lr` (pid,`name`,L,R)VALUES(0,\''.addslashes($_POST['name']).'\','.$maxR.'+1,'.$maxR.'+2)') or die('添加节点错误!'.mysql_error());
mysql_query('UPDATE `tree_lr` SET dorder=id WHERE id=last_insert_id()') or die('添加节点时更新dorder值错误!'.mysql_error());
die('"添加成功!"');
}
}
exit('<font color="red">未知错误!</font>');
}else{
header('Content-Type: text/html; charset=utf-8');
}
function getNodeById($id,$fields='L,R,dorder'){
if(!$id){
return false;
}
$q=mysql_query('SELECT '.$fields.' FROM `tree_lr` WHERE id='.$id.' LIMIT 1');
if(!$q){
return false;
}
return mysql_fetch_object($q);
}
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>树形结构 - 左右值</title>
<script type="text/javascript" src="jquery-1.10.0.js"></script>
<style type="text/css">
body{line-height:25px;font-size:14px;margin:0;}
#wp{margin:0 auto;width:600px;padding:0 10px;border:1px #ccc solid;}
h1{margin:0;padding:10px;font-size:20px;}
h2{border-top:1px #ccc solid;border-bottom:1px #ccc solid;background:#eee;margin:10px -10px;padding:5px 8px;font-size:14px;}
ul{list-style:none;margin:0;padding:0;}
ul.view_node,ul.manage_node{padding:10px 0;}
ul.view_node li{padding-left:1em;}
ul.manage_node{text-align:right;}
ul.manage_node li{clear:both;}
ul.manage_node p{float:left;margin:0;}
ul.manage_node ul{padding-left:1em;}
.gray{color:gray;}
a{text-decoration:none;}
li.hover{background:#efefef;}
ul{background:white;}
input,button{cursor:pointer;}
</style>
</head>
<body>
<script type="text/javascript">
$.post=function(data,callback){
return $.ajax({
url:location.href,
type:'POST',
data:data,
dataType:'json',
success:function(json){
if(typeof(json)!='string'){
callback(json);
}else{
alert(json);
location.reload();
}
},error:function(xhr){
$('#errorMsg').html(xhr.responseText+'<hr/>');
setTimeout(function(){$('#errorMsg').html('')},10000);
}
});
}
function tree_add(ipt,pid){
var val=$.trim($(ipt).val());
if(!/.+/.test(val)){
alert('不能空!');
$(ipt).focus();
return;
}
$.post({pid:pid,name:val});
};
</script>
<div id="wp">
<h1>树形结构 - 左右值</h1>
<?php
$list=array();
$q=mysql_query('SELECT * FROM `tree_lr` ORDER BY l ASC') or die(mysql_error());
while($node=mys