An infinite classification of processing classes

Source: Internet
Author: User
Tags define array definition function prototype html form php code table name valid
PHP Code:--------------------------------------------------------------------------------





<?php


/* Name: Business logic Encapsulation for classification operations


*


* Author: Handsome like scum qq:1191391 e-mail:netcat2@21cn.com


*


* Completion Date: 2003-12-18 13:33


*


* Description: The other classes referenced in this class (DB, Table, Item) are not provided, so this class can only do a reference, can not directly apply the


* is not my stingy do not provide other classes, it is because those are a year or two ago written class, very rotten. I'm afraid everyone will look after the big


* leads to misleading. This class is published here, only hope that you can learn some programming methods.


* teach people to fish than to teach people to fishing ~


*


* Features:


* Using recursive method, only one database query can be used to classify data to generate a tree-like structure. Infinite recursive hierarchy (depending on the machine stack)


*


* Database definition:


* ID smallint unsigned primary #如果数据量很大可用int


* parentid smallint unsigned index #如果数据量很大可用int, click this field


* #如果为根分类, then ParentID = 0


*


* rootid smallint unsigned index #如果数据量很大可用int, click this field


* #如果是根分类则RootID = 0, otherwise rootid = top-level parent category ID


* CategoryName varchar (n) #此大小自定


* If there is any other field definition attached to the back





* Precautions:


* Do not attempt to call this class directly unless you have an interface that corresponds to the other classes that I define, otherwise it will not succeed


* Define dbtable_category This constant in the appropriate place to point to your categorical data table name


*


* Program framework:


*├─ Basic Class <!--complete operation of underlying database operations, data abstraction, language, templates, exceptions, miscellaneous, etc.-->


*│


*│


*└─ business Logic Layer (hierarchy of this class) <!--use classes such as data manipulation, data abstraction, and so on, to complete data processing according to the parameters passed by the presentation layer and return data or operational results-->


*│


*│


*└─── Presentation layer (user interface) <!--Use the business logic layer to display the results of data or operational data obtained through classes such as interfaces in the underlying class-->


*/





define (' dbtable_category ', ' xxx ');





class Category_logic


{


var $KernelRef = NULL; Reference to the core of the system


var $tblObj = NULL; Contains an instance of the current categorical data Table class





var $_currentitem = NULL; Contains an instance of the current classification data TItem class





var $CategoryID = 0; Current Category ID, if there is no current category this item is 0





//---------------------------------------------------------------------------


//private array getnodedata (array $Data, int $ParentNode)


///The ID of the tree and the current category based on a given root and the sibling parent method, which returns the position of the current category in the entire taxonomy


//


//@param: $Data 2-D array of arrays (


//Array (


//' ID ' => category ID,


//' ParentID ' => parent category ID,


//' Rootid ' => root category ID,


//' CategoryName ' => category name,


// ),


// ......


// );


//represented by a tree


//


//@param: $ParentNode The parent classification ID, which is given by the caller each time, and is passed by the program at recursion


//


//Return value: Returns the tree
of all categories as expressed in sibling parental law

//Note: Make sure the current classification is set, otherwise this function does not return


//


//---------------------------------------------------------------------------


function Getnodedata ($Data, $ParentNode)


{


$arr = Array ();





$ArrayCount = 0;





for ($i = 0, $cnt = Count ($Data); $i < $cnt; $i + +)


{


if ($Data [$i] [' parentid '] = = $ParentNode)


{


$arr [$ArrayCount] = $Data [$i];


$arr [$ArrayCount ++][' child '] = $this->getnodedata ($Data, $Data [$i] [' ID ']);


}


}





return $arr;


}





//---------------------------------------------------------------------------


//private string _currentlevel (array $Data, int $Current, String $ProcessFunc = ')


///The ID of the tree and the current category based on a given root and the sibling parent method, which returns the position of the current category in the entire taxonomy


//


//@param: A tree that is represented by the $Data sibling parent law, passed by the caller


//


//@param: $Current The current classification ID, given by the caller on the first call, and calculated recursively by the program


//


@param: $ProcessFunc Specify the processing function for the categorical data, the definition of the function prototype
the annotation in the->printcurrentlevel $this

//


//Return value: Returns the position of the current category in the classification tree


//Note: Make sure the current classification is set, otherwise this function does not return


//


//---------------------------------------------------------------------------


function _currentlevel ($Data, $Current, $ProcessFunc = ")


{


for ($i = 0; $i < Count ($Data); $i + +)


{


if ($Data [$i] [' ID '] = = $Current)


{


if ($Data [$i] [' parentid ']!= 0)


{


$str = $this->_currentlevel ($Data, $Data [$i] [' ParentID '], $ProcessFunc). '-> ';





if ($ProcessFunc) $str. = $ProcessFunc ($Data [$i]);


else $str. = $Data [$i] [' CategoryName '];


}


Else


{


if ($ProcessFunc) $str = $ProcessFunc ($Data [$i]);


else $str = $Data [$i] [' CategoryName '];


}


break;


}


}





return $str;


}





//---------------------------------------------------------------------------


//public category_logic (Object & $Kernel, int $CategoryID =-1)


//This class constructor


//


//@param: $Kernel This parameter is a reference to the core class of the current system, including
in the core class

//Database class, input and output class, System configuration class, etc.


//


//@param: $CategoryID current category ID.


//When you want to call Printcurrentlevel, Getrootid, Getparentid, Generatetypetreelist and


//Call the _currentitem member's method, set this value first.


//


//When Generatetypetreelist is set, no ID is selected for this category, no default is not set


//


//Return Value:none


//


//---------------------------------------------------------------------------


Function &category_logic (& $Kernel, $CategoryID =-1)


{


$this->kernelref = & $Kernel;





$this->tblobj = new Table ($Kernel->dbobj, dbtable_category);





if ($CategoryID!=-1)


{


$this->setcategoryid ($CategoryID);


}


}





//---------------------------------------------------------------------------


//public void Setcategoryid (int $CategoryID)


//Set current category ID


//


//Return Value:none


//


//---------------------------------------------------------------------------


function Setcategoryid ($CategoryID)


{


if (! $CategoryID) return;





$Item = new TItem ($this->kernelref->dbobj, dbtable_category, ' * ', $CategoryID, ' ID ');





$this->_selfdata = & $Item;





$this->categoryid = $CategoryID;


}





//---------------------------------------------------------------------------


//public int Getrootid ()


//Returns the root category ID of the current category


//Note: This function is valid only if the current classification is set


//


//Return value: Returns the root category ID of the current category


//


//---------------------------------------------------------------------------


function Getrootid ()


{


return $this->_selfdata->get (' Rootid ');


}





//---------------------------------------------------------------------------


//public int Getparentid ()


//Returns the parent category ID of the current category


//Note: This function is valid only if the current classification is set


//


//Return value: Returns the parent category ID of the current category


//


//---------------------------------------------------------------------------


function Getparentid ()


{


if ($this->categoryid) return $this->_selfdata->get (' ParentID ');


}








//---------------------------------------------------------------------------


//public string generatetypetreelist (array $Data, string $ProcessFunc, int $floor = 0)


//Returns a list of the entire taxonomy's tree structure placed in optionlist


//


//@param: $Data This parameter is returned by $this->dumptypedatatotree ()


//


//@param: $ProcessFunc handles the callback function that displays the classification information, the function prototype please refer to: $this->printcurrentlevel ()


//


//@param: $floor This parameter can not be artificially given, is the program automatic calculation of the median


//


//return value:


//structure a tree
of the representation of a sibling's parents

//Set such as classified data as follows:


//├──1 Level Classification


//│


//│


//│


//├─2 Level Classification


//││


//│└─3 Level Classification


//│


//└─2 Level Classification


//


//The return value is Array (


//0 => Array (


//' ID ' => ',


//' ParentID ' => ',


//' Rootid ' => ',


//' CategoryName ' => ',


//' child ' => ...


// )


// .....


// )


//


//---------------------------------------------------------------------------


function dumptypedatatotree ($RootID = 0, $Fields = ' * ')


{


$this->tblobj->setfields ($Fields);


$this->tblobj->setcondition (");





$List = $this->tblobj->mapresult ($this->tblobj->select ());





return $this->getnodedata ($List, $RootID);


}





//---------------------------------------------------------------------------


//public string generatetypetreelist (array $Data, string $ProcessFunc = ', int $floor = 0)


//Returns a list of the entire taxonomy's tree structure placed in optionlist


//


//@param: $Data This parameter is returned by $this->dumptypedatatotree ()


//


//@param: $ProcessFunc handles the callback function that displays the classification information, the function prototype please refer to: $this->printcurrentlevel ()


//


//@param: $floor This parameter can not be artificially given, is the program automatic calculation of the median


//


//Return value: Returns a <option> category name 1</option> ... <option> category name N</option>


//


//PS: Call when echo "<select name= ' xxxx ' >". $_c->generatetypetreelist ($Data, ' Processfunc '). "</select>";


//


//---------------------------------------------------------------------------


function Generatetypetreelist ($Data, $ProcessFunc, $floor = 0)


{


$Str = ';


for ($i = 0, $cnt = Count ($Data); $i < $cnt; $i + +)


{


if ($this->categoryid = = $Data [$i] [' ID '])


{


$Str. = "<option value= ' {$Data [$i] [' ID ']} ' selected> '


. Str_repeat ("", $floor * 3)


. ' ├ '


. ($ProcessFunc $ProcessFunc ($Data [$i]): $Data [$i] [' CategoryName '])


. "</option>\n";


}


Else


{


$Str. = "<option value= ' {$Data [$i] [' ID ']} ' > '


. Str_repeat ("", $floor * 3)


. ' ├ '


. ($ProcessFunc $ProcessFunc ($Data [$i]): $Data [$i] [' CategoryName '])


. "</option>\n";


}





if ($Data [$i]) $Str. = $this->generatetypetreelist ($Data [$i] [' child '], $ProcessFunc, $floor + 1);


}





return $Str;


}





//---------------------------------------------------------------------------


//public string Generatetypetreeview (array $Data, string $ProcessFunc = ')


//Returns the tree-structured view of the entire category


//


//@param: $Data This parameter is returned by $this->dumptypedatatotree ()


//


//@param: $ProcessFunc handles the callback function that displays the classification information, the function prototype please refer to: $this->printcurrentlevel ()


//


//Return value: Returns the generated HTML form of the tree


//


//---------------------------------------------------------------------------


function Generatetypetreeview ($Data, $ProcessFunc)


{


$Str = ' <ul style= ' line-height:200% ' > ';





for ($i = 0, $cnt = Count ($Data); $i < $cnt; $i + +)


{


if ($ProcessFunc) $Str. = ' <li> '. $ProcessFunc ($Data [$i]). ' </li> '. "\ n";


else $Str. = ' <li> '. $Data [$i] [' CategoryName ']. ' </li> '. "\ n";





if ($Data [$i] [' child ']) $Str. = ' <li> '. $this->generatetypetreeview ($Data [$i] [' child '], $ProcessFunc). ' </li> ';


}





$Str. = ' </ul> ';





return $Str;


}





//---------------------------------------------------------------------------


//public string Printcurrentlevel (String $ProcessFunc = ')


//Generate current position string for multilevel classification


//Set such as the classification data below, the current classification is 3 level classification, then call returns 1 level classification-> 2 class-> 3 classification


//├──1 Level Classification


//│


//│


//│


//├─2 Level Classification


//││


//│└─3 Level Classification


//│


//└─2 Level Classification


//


//


//


//


@param: $ProcessFunc This is the callback function for how the sorted data is displayed and the category name is displayed directly without setting


//function defines the prototype as a function (& $arr);


//Where the $arr parameter is a one-dimensional array of each of the classification information is as follows:


//Array (ID => 1, parentid => 0, rootid => 0, CategoryName => ' 1-level classification ')

The
//return value is the result of the above data processing, such as returning the categorized name with the link, changing the display color, and so on


//


//Return value: Returns the position of the current category in the entire classification tree


//


//---------------------------------------------------------------------------


function printcurrentlevel ($ProcessFunc = ')


{


if (! $this->categoryid) return ";





if ($this->_selfdata->get ("rootid") = = 0)


{


if ($ProcessFunc) return $ProcessFunc ($this->_selfdata->fetchdatatoarray ());


else return $this->_selfdata->get ("CategoryName");


}





$Current = $this->categoryid;





$this->tblobj->setcondition (' Rootid = '. $this->_selfdata->get (' Rootid '). "Or ID =". $this->_selfdata->get (' Rootid '));





$Data = $this->tblobj->mapresult ($this->tblobj->select ());





return $this->_currentlevel ($Data, $Current, $ProcessFunc);


}





//---------------------------------------------------------------------------


//public boolean Add (array $arr)


//Add new categories to the classification table


//


//@param: $arr include a definition of the newly added taxonomy in this array, defined as follows:


//


//$arr [' Rootid '] the root category ID to which the new category belongs


//$arr [' ParentID '] the parent category ID of the new category


//$arr [' CategoryName '] The name of the new category


//


//Return value: Returns the result of adding a classification operation


//


//---------------------------------------------------------------------------


function Add ($arr)


{


$this->tblobj->setfields (


Array (


' Rootid ',


' ParentID ',


' CategoryName ',


)


);





return $this->tblobj->insert (


Array (


$arr [' Rootid '],


$arr [' ParentID '],


$arr [' CategoryName '],


)


);


}





//---------------------------------------------------------------------------


//public boolean Delete (int $ID)


//Delete existing categories


//


//@param: $ID the category ID to be deleted


//


//Return value: Returns the result of the delete category operation


//


//---------------------------------------------------------------------------


function Delete ($ID)


{


$sysOption = & $this->kernelref->config;





$this->tblobj->setfields (' * ');


$this->tblobj->setcondition (' ID = '. (int) $ID);





return $this->tblobj->delete ();


}





//---------------------------------------------------------------------------


//public boolean Modify (int $ID, array $arr)


//modification of existing classifications


//


//@param: $ID the category ID to be modified


//@param: $arr include the modified taxonomy definition in this array, as defined below:


//


//$arr [' Rootid '] the root category ID to which the new category belongs


//$arr [' ParentID '] the parent category ID of the new category


//$arr [' CategoryName '] The name of the new category


//


//Return value: Returns the modified classification operation result


//


//---------------------------------------------------------------------------


function Modify ($ID, $arr)


{


$this->tblobj->setcondition (' ID = '. (int) $ID);





$prev = $this->tblobj->maponerow ($this->tblobj->select ());





$this->tblobj->setfields (


Array (


' Rootid ',


' ParentID ',


' CategoryName ',


)


);





return $this->tblobj->update ($arr);


}





//---------------------------------------------------------------------------


//public array Modify (int $ID)


//modification of existing classifications


//


//@param: $ID The specified category ID


//


//Return value: Returns the information for the specified ID category


//array includes:


//Array (


//' ID ' => category ID,


//' ParentID ' => parent category ID,


//' Rootid ' => root category ID,


//' CategoryName ' => category name,


// );


//


//---------------------------------------------------------------------------


function GetCategory ($ID)


{


$this->tblobj->setcondition (' ID = '. (int) $ID);





return $this->tblobj->maponerow ($this->tblobj->select ());


}


}


?>








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.