相思资源网 Design By www.200059.com
以前一直使用父子无限分类,这种分类结构清晰,使用也简单。但若分类数量很大的话,在查询上性能不佳。比如在做导航菜单中,我要根据某一分类查询出整个分类树的话(祖辈)。
性能消耗是非常大的,要么做递归,要么做多次查询。故,对于分类的数据量很大的情况,我推荐使用左右值,以减少查询上的麻烦。
复制代码 代码如下:
_id
/**
+----------------------------------------------------------
* 构造函数
* @access public
* @return void
+----------------------------------------------------------
*/
public function __construct($left,$right,$id){
parent::__construct();
$this->_left = $left;
$this->_right = $right;
$this->_id = $id;
}
/**
+----------------------------------------------------------
* 根据node$this->_id得到该node的所有值
* @access public
* @param $nodeId
* @return array
+----------------------------------------------------------
*/
public function getNodeById($nodeId)
{
if($nodeId>0)
{
return $this->getById($nodeId);
}
else
{
throw_exception('未知$this->_id');
return false;
}
}
/**
+----------------------------------------------------------
* 获取父节点,含直属父类(type=1),所有父类:type=0
* @access public
* @param $nodeId int 节点$this->_id
* @return $parentNode array()
+----------------------------------------------------------
*/
public function getParentNode($nodeId,$type = 0)
{
if($nodeId == 0) throw_exception('未知$this->_id');;
$currentNode = $this->getNodeById($nodeId);
if($currentNode)
{
$condition = " ".$this->_left.'<'.$currentNode[$this->_left].' and '.$this->_right.' >'.$currentNode[$this->_right]." ";
if($type ==1) //直属父类
{
return $this->where($condition)->order($this->_left." DESC")->limit(1)->find();
// $sql = "SELECT * FROM ".TABLE_NAME." WHERE {$condition} ORDER BY ".$this->_left." DESC LIMIT 1";
// return mysql_query($sql) or die(mysql_error());
}
else if($type ==0)
{
return $this->where($condition)->findAll();
// $sql = "SELECT * FROM ".TABLE_NAME." WHERE {$condition} ";
// return mysql_query($sql) or die(mysql_error());
}
}
else
{
return false;
}
}
/**
+----------------------------------------------------------
* 当前节点下子孙节点总数.子孙总数=(当前节点的右值 - 当前节点的左值-1)/2
* @access public
* @param $node_id int 节点$this->_id
* @return $amount int 该节点下的子孙总数 *
+----------------------------------------------------------
*/
public function getChildCount($nodeId)
{
$currentNode = $this->getNodeById($nodeId);
if(!empty($currentNode))
{
return (int)($currentNode[$this->_right]-$currentNode[$this->_left] -1)/2;
}
}
/**
+----------------------------------------------------------
* 获取当前节点下所有子节点。 当 A子类的右节点=B子类左节点-1 则 A、B属于同一级别
* @access public
* @param $curentId
* @param $type int 0:当前节点下所有子类,1为当前节点下一级子类
* @return bool
+----------------------------------------------------------
*/
public function getChild($nodeId,$type=0)
{
$currentNode = $this->getNodeById($nodeId);
if($currentNode[$this->_left]-$currentNode[$this->_right] ==1)
{
return false; //当 该节点左值 - 右值=1 时,其下没有子节点。
}
else
{
$condition = $this->_left.'>'.$currentNode[$this->_left].' and '.$this->_right .'<'.$currentNode[$this->_right];
$child = $this->where($condition)->findAll();
if($type == 0)//所有子类
{
return $child;
}
else if($type ==1) //获取当前节点下一级分类
{
$subArr = array(); //一级子类
foreach ($child as $k=>$sub) {
//子类的左节点=父类左节点+1,则子类为第一个子类
if($sub[$this->_left]==$currentNode[$this->_left]+1)
{
//$right = $sub[$k][$this->_right]; //当前节点的右节点
$firstSub = $sub; //当前节点下第一个子类
array_push($subArr,$firstSub); //子类入栈
unset($child[$k]);
}
}
$rightVal = $firstSub[$this->_right]; //第一个子节点为比较标志
$childCount = count($child);//剩余子节点数
for($i=0;$i<$childCount;$i++) //循环检索出 同级子节点
{
foreach ($child as $key => $sub2) {
if($rightVal == $sub2[$this->_left]-1)
{
$rightVal = $sub2[$this->_right]; //把循环当前的node的右节点当做比较值
array_push($subArr,$sub2);
unset($child[$key]);
}
}
}
return $subArr;
}
}
}
/**
+----------------------------------------------------------
* 返回当前节点的完整路径
* @access public
* @param $nodeId
* @return array
+----------------------------------------------------------
*/
public function getSinglePath($nodeId)
{
$sql = "select parent.* from __TABLE__ as node,__TABLE__ as parent where node.{$this->_left} between parent.{$this->_left}
AND parent.{$this->_right} AND node.{$this->_id} = {$nodeId} order by parent.{$this->_left}";
// echo $sql;
return $this->query($sql);
}
/**
+----------------------------------------------------------
* 添加子节点,分3种:0:在当前节点下最后追加一个子节点;1:在当前节点下追加第一个子节点;
2:在当前节点下的某个子节点后追加
复制代码 代码如下:
* @access public
* @param $currentId int
* @param $nodeName string 新节点名称
* @param $targetId int 追加到当前节点下子节点的指定节点后
* @return bool
+----------------------------------------------------------
*/
public function addNode($nodeId,$newData,$type=0,$targetId=0)
{
if(empty($newData))
{
throw_exception('新分类不能为空');
}
$currentNode = $this->getNodeById($nodeId);
switch ($type) {
case 0:
$leftNode = $currentNode[$this->_right]; //新节点的左值为父节点的右值
$rightNode = $leftNode+1;
break;
case 1:
$leftNode = $currentNode[$this->_left]+1; //新节点的左值为父节点的左值+1
$rightNode = $leftNode+1;
break;
case 2:
$otherNode = $this->getNodeById($targetId);
$leftNode = $otherNode[$this->_right]+1;
$rightNode = $leftNode+1;
default:
break;
}
// $sql = "UPDATE ".TABLE_NAME." SET ".$this->_right."=".$this->_right."+2 WHERE ".$this->_right." >= ".$leftNode;
// $sql2 = "UPDATE ".TABLE_NAME." SET ".$this->_left."=".$this->_left."+2 WHERE ".$this->_left.">".$leftNode;
$this->setInc($this->_right,$this->_right.">=".$leftNode,2); //把所有右值大于新节点左值的节点的右值+2,注意效率
$this->setInc($this->_left,$this->_left.">".$leftNode,2); //把所有大于新节点的左值+2
$newData[$this->_left] = (int)$leftNode;
$newData[$this->_right] =(int) $rightNode;
return $this->add($newData);
}
/**
+----------------------------------------------------------
* 删除节点
* @access public
* @param type 操作类型,默认为0删除当前节点下的所有子节点,1为删除包括自身的节点
* @param $nodeId int 要删除的$this->_id
* @return bool
+----------------------------------------------------------
*/
public function rmNode($nodeId,$type =1)
{
$currentNode = $this->getNodeById($nodeId);
if($type == 1) //删除包含自身的节点
{
$sql = "DELETE FROM __TABLE__ WHERE ".$this->_left.">= {$currentNode[$this->_left]} AND ".$this->_right."<= {$currentNode[$this->_right]}";
$childCount = ($this->getChildCount($nodeId)+1)*2; //要更新的值
$sql2 = "UPDATE __TABLE__ SET ".$this->_right."=".$this->_right."-".$childCount." WHERE ".$this->_right.">".$currentNode[$this->_right];
$sql3 = "UPDATE __TABLE__ SET ".$this->_left."=".$this->_left."-".$childCount." WHERE ".$this->_left.">".$currentNode[$this->_left];
}
else //删除当前节点下的所有节点
{
$sql ="DELETE FROM __TABLE__ WHERE ".$this->_left."> {$currentNode[$this->_left]} AND ".$this->_right."< {$currentNode[$this->_right]}";
$childCount = $this->getChildCount($nodeId)*2; //要更新的值
$sql2 = "UPDATE __TABLE__ SET ".$this->_right."=".$this->_right ."-".$childCount." WHERE ".$this->_right.">=".$currentNode[$this->_right];
$sql3 = "UPDATE __TABLE__ SET ".$this->_left."=".$this->_left."-".$childCount." WHERE ".$this->_left.">".$currentNode[$this->_left];
}
$this->execute($sql);
$this->execute($sql2);
$this->execute($sql3);
return true;
}
/**
+----------------------------------------------------------
* 修改节点,名称等
* @access public
* @param $newData array()必须含有 要修改的$this->_id,k-v必须对齐,如arr['node_name'] = '商品'
* @return bool
+----------------------------------------------------------
*/
public function modiNode($newData)
{
if(!empty($newData))
{
$id = $newData[$this->_id];
unset($newData[$this->_id]);
return $this->save($newData,$this->_id.'='.$id);
}
}
}
?>
性能消耗是非常大的,要么做递归,要么做多次查询。故,对于分类的数据量很大的情况,我推荐使用左右值,以减少查询上的麻烦。
复制代码 代码如下:
_id
/**
+----------------------------------------------------------
* 构造函数
* @access public
* @return void
+----------------------------------------------------------
*/
public function __construct($left,$right,$id){
parent::__construct();
$this->_left = $left;
$this->_right = $right;
$this->_id = $id;
}
/**
+----------------------------------------------------------
* 根据node$this->_id得到该node的所有值
* @access public
* @param $nodeId
* @return array
+----------------------------------------------------------
*/
public function getNodeById($nodeId)
{
if($nodeId>0)
{
return $this->getById($nodeId);
}
else
{
throw_exception('未知$this->_id');
return false;
}
}
/**
+----------------------------------------------------------
* 获取父节点,含直属父类(type=1),所有父类:type=0
* @access public
* @param $nodeId int 节点$this->_id
* @return $parentNode array()
+----------------------------------------------------------
*/
public function getParentNode($nodeId,$type = 0)
{
if($nodeId == 0) throw_exception('未知$this->_id');;
$currentNode = $this->getNodeById($nodeId);
if($currentNode)
{
$condition = " ".$this->_left.'<'.$currentNode[$this->_left].' and '.$this->_right.' >'.$currentNode[$this->_right]." ";
if($type ==1) //直属父类
{
return $this->where($condition)->order($this->_left." DESC")->limit(1)->find();
// $sql = "SELECT * FROM ".TABLE_NAME." WHERE {$condition} ORDER BY ".$this->_left." DESC LIMIT 1";
// return mysql_query($sql) or die(mysql_error());
}
else if($type ==0)
{
return $this->where($condition)->findAll();
// $sql = "SELECT * FROM ".TABLE_NAME." WHERE {$condition} ";
// return mysql_query($sql) or die(mysql_error());
}
}
else
{
return false;
}
}
/**
+----------------------------------------------------------
* 当前节点下子孙节点总数.子孙总数=(当前节点的右值 - 当前节点的左值-1)/2
* @access public
* @param $node_id int 节点$this->_id
* @return $amount int 该节点下的子孙总数 *
+----------------------------------------------------------
*/
public function getChildCount($nodeId)
{
$currentNode = $this->getNodeById($nodeId);
if(!empty($currentNode))
{
return (int)($currentNode[$this->_right]-$currentNode[$this->_left] -1)/2;
}
}
/**
+----------------------------------------------------------
* 获取当前节点下所有子节点。 当 A子类的右节点=B子类左节点-1 则 A、B属于同一级别
* @access public
* @param $curentId
* @param $type int 0:当前节点下所有子类,1为当前节点下一级子类
* @return bool
+----------------------------------------------------------
*/
public function getChild($nodeId,$type=0)
{
$currentNode = $this->getNodeById($nodeId);
if($currentNode[$this->_left]-$currentNode[$this->_right] ==1)
{
return false; //当 该节点左值 - 右值=1 时,其下没有子节点。
}
else
{
$condition = $this->_left.'>'.$currentNode[$this->_left].' and '.$this->_right .'<'.$currentNode[$this->_right];
$child = $this->where($condition)->findAll();
if($type == 0)//所有子类
{
return $child;
}
else if($type ==1) //获取当前节点下一级分类
{
$subArr = array(); //一级子类
foreach ($child as $k=>$sub) {
//子类的左节点=父类左节点+1,则子类为第一个子类
if($sub[$this->_left]==$currentNode[$this->_left]+1)
{
//$right = $sub[$k][$this->_right]; //当前节点的右节点
$firstSub = $sub; //当前节点下第一个子类
array_push($subArr,$firstSub); //子类入栈
unset($child[$k]);
}
}
$rightVal = $firstSub[$this->_right]; //第一个子节点为比较标志
$childCount = count($child);//剩余子节点数
for($i=0;$i<$childCount;$i++) //循环检索出 同级子节点
{
foreach ($child as $key => $sub2) {
if($rightVal == $sub2[$this->_left]-1)
{
$rightVal = $sub2[$this->_right]; //把循环当前的node的右节点当做比较值
array_push($subArr,$sub2);
unset($child[$key]);
}
}
}
return $subArr;
}
}
}
/**
+----------------------------------------------------------
* 返回当前节点的完整路径
* @access public
* @param $nodeId
* @return array
+----------------------------------------------------------
*/
public function getSinglePath($nodeId)
{
$sql = "select parent.* from __TABLE__ as node,__TABLE__ as parent where node.{$this->_left} between parent.{$this->_left}
AND parent.{$this->_right} AND node.{$this->_id} = {$nodeId} order by parent.{$this->_left}";
// echo $sql;
return $this->query($sql);
}
/**
+----------------------------------------------------------
* 添加子节点,分3种:0:在当前节点下最后追加一个子节点;1:在当前节点下追加第一个子节点;
2:在当前节点下的某个子节点后追加
复制代码 代码如下:
* @access public
* @param $currentId int
* @param $nodeName string 新节点名称
* @param $targetId int 追加到当前节点下子节点的指定节点后
* @return bool
+----------------------------------------------------------
*/
public function addNode($nodeId,$newData,$type=0,$targetId=0)
{
if(empty($newData))
{
throw_exception('新分类不能为空');
}
$currentNode = $this->getNodeById($nodeId);
switch ($type) {
case 0:
$leftNode = $currentNode[$this->_right]; //新节点的左值为父节点的右值
$rightNode = $leftNode+1;
break;
case 1:
$leftNode = $currentNode[$this->_left]+1; //新节点的左值为父节点的左值+1
$rightNode = $leftNode+1;
break;
case 2:
$otherNode = $this->getNodeById($targetId);
$leftNode = $otherNode[$this->_right]+1;
$rightNode = $leftNode+1;
default:
break;
}
// $sql = "UPDATE ".TABLE_NAME." SET ".$this->_right."=".$this->_right."+2 WHERE ".$this->_right." >= ".$leftNode;
// $sql2 = "UPDATE ".TABLE_NAME." SET ".$this->_left."=".$this->_left."+2 WHERE ".$this->_left.">".$leftNode;
$this->setInc($this->_right,$this->_right.">=".$leftNode,2); //把所有右值大于新节点左值的节点的右值+2,注意效率
$this->setInc($this->_left,$this->_left.">".$leftNode,2); //把所有大于新节点的左值+2
$newData[$this->_left] = (int)$leftNode;
$newData[$this->_right] =(int) $rightNode;
return $this->add($newData);
}
/**
+----------------------------------------------------------
* 删除节点
* @access public
* @param type 操作类型,默认为0删除当前节点下的所有子节点,1为删除包括自身的节点
* @param $nodeId int 要删除的$this->_id
* @return bool
+----------------------------------------------------------
*/
public function rmNode($nodeId,$type =1)
{
$currentNode = $this->getNodeById($nodeId);
if($type == 1) //删除包含自身的节点
{
$sql = "DELETE FROM __TABLE__ WHERE ".$this->_left.">= {$currentNode[$this->_left]} AND ".$this->_right."<= {$currentNode[$this->_right]}";
$childCount = ($this->getChildCount($nodeId)+1)*2; //要更新的值
$sql2 = "UPDATE __TABLE__ SET ".$this->_right."=".$this->_right."-".$childCount." WHERE ".$this->_right.">".$currentNode[$this->_right];
$sql3 = "UPDATE __TABLE__ SET ".$this->_left."=".$this->_left."-".$childCount." WHERE ".$this->_left.">".$currentNode[$this->_left];
}
else //删除当前节点下的所有节点
{
$sql ="DELETE FROM __TABLE__ WHERE ".$this->_left."> {$currentNode[$this->_left]} AND ".$this->_right."< {$currentNode[$this->_right]}";
$childCount = $this->getChildCount($nodeId)*2; //要更新的值
$sql2 = "UPDATE __TABLE__ SET ".$this->_right."=".$this->_right ."-".$childCount." WHERE ".$this->_right.">=".$currentNode[$this->_right];
$sql3 = "UPDATE __TABLE__ SET ".$this->_left."=".$this->_left."-".$childCount." WHERE ".$this->_left.">".$currentNode[$this->_left];
}
$this->execute($sql);
$this->execute($sql2);
$this->execute($sql3);
return true;
}
/**
+----------------------------------------------------------
* 修改节点,名称等
* @access public
* @param $newData array()必须含有 要修改的$this->_id,k-v必须对齐,如arr['node_name'] = '商品'
* @return bool
+----------------------------------------------------------
*/
public function modiNode($newData)
{
if(!empty($newData))
{
$id = $newData[$this->_id];
unset($newData[$this->_id]);
return $this->save($newData,$this->_id.'='.$id);
}
}
}
?>
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无解析thinkphp的左右值无限分类的评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。