1. 云栖社区>
  2. PHP教程>
  3. 正文

基于左右值的无限分类算法

作者:用户 来源:互联网 时间:2017-12-01 11:02:32

算法分类基于左右无限

基于左右值的无限分类算法 - 摘要: 本文讲的是基于左右值的无限分类算法, 基于左右值的无限分类算法,php,由于之前采用的递归排序无限分类方法感觉不是很理想,于是参考了国外同国内的左右值排序相关方法,自己写了一个基于左右值的无限分类类,欢迎大家测试,另外还有用到一个MySQL操作类,由于不是原创就不提供了,如果

基于左右值的无限分类算法,php,由于之前采用的递归排序无限分类方法感觉不是很理想,于是参考了国外同国内的左右值排序相关方法,自己写了一个基于左右值的无限分类类,欢迎大家测试,另外还有用到一个MySQL操作类,由于不是原创就不提供了,如果需要的可以PM我

[php] view plain copy


html='<embed height="18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" width="18" allowscriptaccess="never" allownetworking="internal"></embed>'>


<?php
/**
*基于左右值排序的无限分类算法
*数据库结果为
CREATETABLEom_catagory(
CatagoryIDint(10)unsignedNOTNULLauto_increment,
Namevarchar(50)default'',
Lftint(10)unsignedNOTNULLdefault'0',
Rgtint(10)unsignedNOTNULLdefault'0',
PRIMARYKEY(id),
KEYlft(lft),
KEYrgt(rgt)
)
*更多的关于左右值排序的例子
*http://www.chinaunix.net/jh/27/239532.html(http://dev.mysql.com/tech-resources/articles/hierarchical-data.html)
*@author[email][email protected][/email]
*@version1.0
*@copyrightpsdshow
*欢迎光临我的个人日志http://www.dayanmei.com
*/
classsortclass
{/**
*Description
*@var
*@since1.0
*@accessprivate
*/
var$db;/**
*Description
*@var
*@since1.0
*@accessprivate
*/
var$tablefix;/**
*Shortdescription.
*构造函数,引入数据库操作类函数
*Detaildescription
*@paramnone
*@globalnone
*@since1.0
*@accessprivate
*@returnvoid
*@updatedatetime
*/
functionsortclass()
{
global$db;
$this->db=$db;
$this->tablefix="om_";
}//endfunc/**
*Shortdescription.
*增加新的分类
*Detaildescription
*@paramnone
*@globalnone
*@since1.0
*@accessprivate
*@returnvoid
*@updatedatetime
*/
functionaddsort($CatagoryID,$SortName)
{
if($CatagoryID==0){
$Lft=0;
$Rgt=1;
}else{
$Result=$this->checkcatagory($CatagoryID);
//取得父类的左值,右值
$Lft=$Result['Lft'];
$Rgt=$Result['Rgt'];
$this->db->query("UPDATE`".$this->tablefix."catagory`SET`Lft`=`Lft`+2WHERE`Lft`>$Rgt");
$this->db->query("UPDATE`".$this->tablefix."catagory`SET`Rgt`=`Rgt`+2WHERE`Rgt`>=$Rgt");
}//插入
if($this->db->query("INSERTINTO`".$this->tablefix."catagory`SET`Lft`='$Rgt',`Rgt`='$Rgt'+1,`Name`='$SortName'")){
//$this->referto("成功增加新的类别","JAVASCRIPT:HISTORY.BACK(1)",3);
return1;
}else{
//$this->referto("增加新的类别失败了","JAVASCRIPT:HISTORY.BACK(1)",3);
return-1;
}
}//endfunc/**
*Shortdescription.
*删除类别
*Detaildescription
*@paramnone
*@globalnone
*@since1.0
*@accessprivate
*@returnvoid
*@updatedatetime
*/
functiondeletesort($CatagoryID)
{
//取得被删除类别的左右值,检测是否有子类,如果有就一起删除
$Result=$this->checkcatagory($CatagoryID);
$Lft=$Result['Lft'];
$Rgt=$Result['Rgt'];
//执行删除
if($this->db->query("DELETEFROM`".$this->tablefix."catagory`WHERE`Lft`>=$LftAND`Rgt`<=$Rgt")){
$Value=$Rgt-$Lft+1;
//更新左右值
$this->db->query("UPDATE`".$this->tablefix."catagory`SET`Lft`=`Lft`-$ValueWHERE`Lft`>$Lft");
$this->db->query("UPDATE`".$this->tablefix."catagory`SET`Rgt`=`Rgt`-$ValueWHERE`Rgt`>$Rgt");
//$this->referto("成功删除类别","javascript:history.back(1)",3);
return1;
}else{
//$this->referto("删除类别失败了","javascript:history.back(1)",3);
return-1;
}
}//endfunc
/**
*Shortdescription.
*1,所有子类,不包含自己;2包含自己的所有子类;3不包含自己所有父类4;包含自己所有父类
*Detaildescription
*@paramnone
*@globalnone
*@since1.0
*@accessprivate
*@returnvoid
*@updatedatetime
*/
functiongetcatagory($CatagoryID,$type=1)
{
$Result=$this->checkcatagory($CatagoryID);
$Lft=$Result['Lft'];
$Rgt=$Result['Rgt'];
$SeekSQL="SELECT*FROM`".$this->tablefix."catagory`WHERE";
switch($type){
case"1":
$condition="`Lft`>$LftAND`Rgt`<$Rgt";
break;
case"2":
$condition="`Lft`>=$LftAND`Rgt`<=$Rgt";
break;
case"3":
$condition="`Lft`<$LftAND`Rgt`>$Rgt";
break;
case"4":
$condition="`Lft`<=$LftAND`Rgt`>=$Rgt";
break;
default:
$condition="`Lft`>$LftAND`Rgt`<$Rgt";
;
}
$SeekSQL.=$condition."ORDERBY`Lft`ASC";
$Sorts=$this->db->getrows($SeekSQL);
return$Sorts;
}//endfunc/**
*Shortdescription.
*取得直属父类
*Detaildescription
*@paramnone
*@globalnone
*@since1.0
*@accessprivate
*@returnvoid
*@updatedatetime
*/
functiongetparent($CatagoryID)
{
$Parent=$this->getcatagory($CatagoryID,3);
return$Parent;
}//endfunc
/**
*Shortdescription.
*移动类,如果类有子类也一并移动
*Detaildescription
*@paramnone
*@globalnone
*@since1.0
*@accessprivate
*@returnvoid
*@updatedatetime
*/
functionmovecatagory($SelfCatagoryID,$ParentCatagoryID)
{
$SelfCatagory=$this->checkcatagory($SelfCatagoryID);
$NewCatagory=$this->checkcatagory($ParentCatagoryID);$SelfLft=$SelfCatagory['Lft'];
$SelfRgt=$SelfCatagory['Rgt'];
$Value=$SelfRgt-$SelfLft;
//取得所有分类的ID方便更新左右值
$CatagoryIDS=$this->getcatagory($SelfCatagoryID,2);
foreach($CatagoryIDSas$v){
$IDS[]=$v['CatagoryID'];
}
$InIDS=implode(",",$IDS);$ParentLft=$NewCatagory['Lft'];
$ParentRgt=$NewCatagory['Rgt'];
//print_r($InIDS);
//print_r($NewCatagory);
//print_r($SelfCatagory);
//exit;
if($ParentRgt>$SelfRgt){
$UpdateLeftSQL="UPDATE`".$this->tablefix."catagory`SET`Lft`=`Lft`-$Value-1WHERE`Lft`>$SelfRgtAND`Rgt`<=$ParentRgt";
$UpdateRightSQL="UPDATE`".$this->tablefix."catagory`SET`Rgt`=`Rgt`-$Value-1WHERE`Rgt`>$SelfRgtAND`Rgt`<$ParentRgt";
$TmpValue=$ParentRgt-$SelfRgt-1;
$UpdateSelfSQL="UPDATE`".$this->tablefix."catagory`SET`Lft`=`Lft`+$TmpValue,`Rgt`=`Rgt`+$TmpValueWHERE`CatagoryID`IN($InIDS)";
}else{
$UpdateLeftSQL="UPDATE`".$this->tablefix."catagory`SET`Lft`=`Lft`+$Value+1WHERE`Lft`>$ParentRgtAND`Lft`<$SelfLft";
$UpdateRightSQL="UPDATE`".$this->tablefix."catagory`SET`Rgt`=`Rgt`+$Value+1WHERE`Rgt`>=$ParentRgtAND`Rgt`<$SelfLft";
$TmpValue=$SelfLft-$ParentRgt;
$UpdateSelfSQL="UPDATE`".$this->tablefix."catagory`SET`Lft`=`Lft`-$TmpValue,`Rgt`=`Rgt`-$TmpValueWHERE`CatagoryID`IN($InIDS)";
}
$this->db->query($UpdateLeftSQL);
$this->db->query($UpdateRightSQL);
$this->db->query($UpdateSelfSQL);
//$this->referto("成功移动类别","javascript:history.back(1)",3);
return1;
}//endfunc/**
*Shortdescription.
*
*Detaildescription
*@paramnone
*@globalnone
*@since1.0
*@accessprivate
*@returnvoid
*@updatedatetime
*/
functioncheckcatagory($CatagoryID)
{
//检测父类ID是否存在
$SQL="SELECT*FROM`".$this->tablefix."catagory`WHERE`CatagoryID`='$CatagoryID'LIMIT1";
$Result=$this->db->getrow($SQL);
if(count($Result)<1){
$this->referto("父类ID不存在,请检查","javascript:history.back(1)",3);
}
return$Result;
}//endfunc/**
*Shortdescription.
*
*Detaildescription
*@paramnone
*@globalnone
*@since1.0
*@accessprivate
*@returnarray($Catagoryarray,$Deep)
*@updatedatetime
*/
functionsort2array($CatagoryID=0)
{
$Output=array();
if($CatagoryID==0){
$CatagoryID=$this->getrootid();
}
if(empty($CatagoryID)){
returnarray();
exit;
}
$Result=$this->db->query('SELECTLft,RgtFROM`'.$this->tablefix.
'catagory`WHERE`CatagoryID`='.$CatagoryID);
if($Row=$this->db->fetch_array($Result)){
$Right=array();
$Query='SELECT*FROM`'.$this->tablefix.
'catagory`WHERELftBETWEEN'.$Row['Lft'].'AND'.
$Row['Rgt'].'ORDERBYLftASC';$Result=$this->db->query($Query);
while($Row=$this->db->fetch_array($Result)){
if(count($Right)>0){
while($Right[count($Right)-1]<$Row['Rgt']){
array_pop($Right);
}
}
$Output[]=array('Sort'=>$Row,'Deep'=>count($Right));
$Right[]=$Row['Rgt'];
}
}
return$Output;
}//endfunc/**
*Shortdescription.
*
*Detaildescription
*@paramnone
*@globalnone
*@since1.0
*@accessprivate
*@returnvoid
*@updatedatetime
*/
functiongetrootid()
{
$Query="SELECT*FROM`".$this->tablefix."catagory`ORDERBY`Lft`ASCLIMIT1";
$RootID=$this->db->getrow($Query);
if(count($RootID)>0){
return$RootID['CatagoryID'];
}else{
return0;
}
}//endfunc/**
*Shortdescription.
*
*Detaildescription
*@paramnone
*@globalnone
*@since1.0
*@accessprivate
*@returnvoid
*@updatedatetime
*/
functionreferto($msg,$url,$sec)
{
echo"";
echo"";
if(is_array($msg)){
foreach($msgas$key=>$value){
echo$key."=>".$value."
";
}
}else{
echo$msg;
}
exit;
}//endfunc
}//endclass?>

以上是基于左右值的无限分类算法的全部内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有其他相关内容,欢迎继续使用右上角搜索按钮进行搜索算法 , 分类 , 基于 , 左右 无限 ,以便于您获取更多的相关知识。