<?php
/**
* Infinite classification algorithm based on left and right value ordering
* Database results are
CREATE TABLE Om_catagory (
Catagoryid Int (ten) unsigned not NULL auto_increment,
Name varchar ("Default"),
Lft Int (ten) unsigned not NULL default ' 0 ',
RGT Int (ten) unsigned not NULL default ' 0 ',
PRIMARY KEY (ID),
KEY LfT (LFT),
KEY RGT (RGT)
)
* More examples of sorting left and right values
* Http://www.chinaunix.net/jh/27/239532.html (http://dev.mysql.com/tech-resources/articles/hierarchical-data.html)
* @author [Email][email Protected][/email]
* @version 1.0
* @copyright Psdshow
* Welcome to my personal log http://www.dayanmei.com
*/
Class Sortclass
{
/**
* Description
* @var
* @since 1.0
* @access Private
*/
var $db;
/**
* Description
* @var
* @since 1.0
* @access Private
*/
var $tablefix;
/**
* Short description.
* constructor, introduce database operation class function
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function Sortclass ()
{
Global $db;
$this->db= $db;
$this->tablefix= "Om_";
}//End Func
/**
* Short description.
* Add a new category
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function AddSort ($CatagoryID, $SortName)
{
if ($CatagoryID ==0) {
$Lft = 0;
$RGT = 1;
}else{
$Result = $this->checkcatagory ($CatagoryID);
Gets the left value of the parent class, the right value
$Lft = $Result [' Lft '];
$RGT = $Result [' Rgt '];
$this->db->query ("UPDATE". $this->tablefix. " Catagory ' SET ' Lft ' = ' Lft ' +2 WHERE ' Lft ' > $Rgt ');
$this->db->query ("UPDATE". $this->tablefix. " Catagory ' SET ' Rgt ' = ' Rgt ' +2 WHERE ' Rgt ' >= $Rgt ");
}
Insert
if ($this->db->query ("INSERT into". $this->tablefix. " Catagory ' SET ' Lft ' = ' $Rgt ', ' Rgt ' = ' $Rgt ' +1, ' Name ' = ' $SortName ' ")} {
$this->referto ("Add new category successfully", "javascript:history. Back (1) ", 3);
return 1;
}else{
$this->referto ("added new category failed", "javascript:history." Back (1) ", 3);
return-1;
}
}//End Func
/**
* Short description.
* Delete Category
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function Deletesort ($CatagoryID)
{
Gets the left and right values of the deleted category, detects if there are subclasses, and deletes them together if any
$Result = $this->checkcatagory ($CatagoryID);
$Lft = $Result [' Lft '];
$RGT = $Result [' Rgt '];
Perform the Delete
if ($this->db->query ("DELETE from". $this->tablefix. " Catagory ' WHERE ' Lft ' >= $Lft and ' Rgt ' <= $Rgt ")) {
$Value = $Rgt-$Lft +1;
Update left and right values
$this->db->query ("UPDATE". $this->tablefix. " Catagory ' SET ' Lft ' = ' Lft '-$Value WHERE ' Lft ' > $Lft ');
$this->db->query ("UPDATE". $this->tablefix. " Catagory ' SET ' Rgt ' = ' Rgt '-$Value WHERE ' Rgt ' > $Rgt ');
$this->referto ("Successfully deleted category", "Javascript:history.back (1)", 3);
return 1;
}else{
$this->referto ("Delete category failed", "Javascript:history.back (1)", 3);
return-1;
}
}//End Func
/**
* Short description.
* 1, all subclasses, not including themselves; 2 contains all of its own subclasses; 3 does not contain all of its own parent class 4; Contains all of its own parent classes
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function Getcatagory ($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 ' > $Lft and ' Rgt ' < $RGT";
Break
Case "2":
$condition = "' Lft ' >= $Lft and ' Rgt ' <= $Rgt";
Break
Case "3":
$condition = "' Lft ' < $Lft and ' Rgt ' > $Rgt";
Break
Case "4":
$condition = "' Lft ' <= $Lft and ' Rgt ' >= $Rgt";
Break
Default:
$condition = "' Lft ' > $Lft and ' Rgt ' < $RGT";
;
}
$SeekSQL. = $condition. " ORDER by ' Lft ' ASC;
$Sorts = $this->db->getrows ($SeekSQL);
return $Sorts;
}//End Func
/**
* Short description.
* Get immediate parent class
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function GetParent ($CatagoryID)
{
$Parent = $this->getcatagory ($CatagoryID, 3);
return $Parent;
}//End Func
/**
* Short description.
* Move class, if class has subclasses also move
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function Movecatagory ($SelfCatagoryID, $ParentCatagoryID)
{
$SelfCatagory = $this->checkcatagory ($SelfCatagoryID);
$NewCatagory = $this->checkcatagory ($ParentCatagoryID);
$SelfLft = $SelfCatagory [' Lft '];
$SELFRGT = $SelfCatagory [' Rgt '];
$Value = $SelfRgt-$SelfLft;
Get the ID of all categories to easily update the left and right values
$CatagoryIDS = $this->getcatagory ($SelfCatagoryID, 2);
foreach ($CatagoryIDS as $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-1 WHERE ' Lft ' > $SelfRgt and ' Rgt ' <= $ParentRgt ";
$UpdateRightSQL = "UPDATE". $this->tablefix. " Catagory ' SET ' Rgt ' = ' Rgt '-$Value-1 WHERE ' Rgt ' > $SelfRgt and ' Rgt ' < $PARENTRGT ";
$TmpValue = $ParentRgt-$SelfRgt-1;
$UpdateSelfSQL = "UPDATE". $this->tablefix. " Catagory ' SET ' Lft ' = ' Lft ' + $TmpValue, ' Rgt ' = ' Rgt ' + $TmpValue WHERE ' Catagoryid ' in ($InIDS) ";
}else{
$UpdateLeftSQL = "UPDATE". $this->tablefix. " Catagory ' SET ' Lft ' = ' Lft ' + $Value +1 WHERE ' Lft ' > $ParentRgt and ' Lft ' < $SelfLft ";
$UpdateRightSQL = "UPDATE". $this->tablefix. " Catagory ' SET ' Rgt ' = ' Rgt ' + $Value +1 WHERE ' Rgt ' >= $ParentRgt and ' Rgt ' < $SelfLft ";
$TmpValue = $SelfLft-$ParentRgt;
$UpdateSelfSQL = "UPDATE". $this->tablefix. " Catagory ' SET ' Lft ' = ' Lft '-$TmpValue, ' Rgt ' = ' Rgt '-$TmpValue WHERE ' Catagoryid ' in ($InIDS) ";
}
$this->db->query ($UpdateLeftSQL);
$this->db->query ($UpdateRightSQL);
$this->db->query ($UpdateSelfSQL);
$this->referto ("Successful move category", "Javascript:history.back (1)", 3);
return 1;
}//End Func
/**
* Short description.
*
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function Checkcatagory ($CatagoryID)
{
Detects if the parent class ID exists
$SQL = "SELECT * from". $this->tablefix. " Catagory ' WHERE ' catagoryid ' = ' $CatagoryID ' LIMIT 1 ';
$Result = $this->db->getrow ($SQL);
if (count ($Result) <1) {
$this->referto ("Parent class ID does not exist, please check", "Javascript:history.back (1)", 3);
}
return $Result;
}//End Func
/**
* Short description.
*
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return Array ($Catagoryarray, $Deep)
* @update Date Time
*/
function Sort2array ($CatagoryID =0)
{
$Output = Array ();
if ($CatagoryID ==0) {
$CatagoryID = $this->getrootid ();
}
if (empty ($CatagoryID)) {
return Array ();
Exit
}
$Result = $this->db->query (' SELECT Lft, Rgt from '. $this->tablefix.
' catagory ' WHERE ' catagoryid ' = '. $CatagoryID);
if ($Row = $this->db->fetch_array ($Result)) {
$Right = Array ();
$Query = ' SELECT * from '. $this->tablefix.
' catagory ' WHERE Lft between '. $Row [' Lft ']. ' and '.
$Row [' Rgt ']. ' ORDER by Lft ASC ';
$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;
}//End Func
/**
* Short description.
*
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function Getrootid ()
{
$Query = "SELECT * from". $this->tablefix. " Catagory ' ORDER by ' Lft ' ASC LIMIT 1 ';
$RootID = $this->db->getrow ($Query);
if (count ($RootID) >0) {
return $RootID [' Catagoryid '];
}else{288104151
return 0;
}
}//End Func
/**
* Short description.
*
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function Referto ($msg, $url, $sec)
{
echo "<meta http-equiv=\" content-type\ "content=\" text/html; Charset=utf-8\ ">";
echo "<meta Http-equiv=refresh content= $sec; Url= $url > ";
if (Is_array ($msg)) {
foreach ($msg as $key = = $value) {
echo $key. " = ". $value." <br> ";
}
}else{
Echo $msg;
}
Exit
}//End Func
}//End Class
?>
function description and examples
PHP Infinite Classification [left-right value] algorithm
The result of database is an infinite classification algorithm based on left and right value sort