Location: PHPKode > scripts > Category Tree > category-tree/Tree_class.php4
<?
/**	
*	@package class
*	@author abstract : Laurent Moutard, Arnaud Beauvain, Aurore Moutte, St├ęphane Bernhardt
*	@version 1.0
*/

require("Category_class.php4");

class Tree{

	/** @var integer root's id in db*/
	var $idRoot = 1;
	
	/** @var string product's table*/
	var $productTable = "product";
	
	/** @var string category's table*/
	var $categoryTable = "category";
	
	/** @var integer category's id choosen*/
	var $idCategoryChoosen = "";

	/** @var string for Listing displayable categories (all those having associated articles and their fathers)*/
	var $categoryList = "";

	/** @var int[] for Listing fathers of the clicked category*/
	var $parentsList = array();
	
	/** @var integer Size of the fathers list of the selected category*/
	var $parentsListSize = 0;
	
	/** @var mixed[] containing the tree structure with the requested branch*/
	var $tabResult = array();

	/** @var Builder which creates a character string containing all of the displayable categories,
		separated by commas
		example : "1,2,15,4,11"
	*/	
	function Tree($idCategoryChoosen=null){
		if($idCategoryChoosen == null){
			$idCategoryChoosen = $this->idRoot;
		}
		$this->idCategoryChoosen = $idCategoryChoosen;
		$categoryChoosen = new category($idCategoryChoosen);		
		$this->parentsList = explode (",",$categoryChoosen->getParentsList());
		$this->parentsListSize = sizeof($this->parentsList);
		/** We select categories associated with suitable articles*/
		$query1 = "SELECT ".$this->productTable.".id_category,".$this->categoryTable.".parents_list FROM ".$this->productTable.",".$this->categoryTable." WHERE ".$this->productTable.".id_category=".$this->categoryTable.".id GROUP BY ".$this->productTable.".id_category";
		if(($result1 = mysql_query($query1))&&mysql_num_rows($result1)) {
			$tabListCategory = array();
			while($row1=mysql_fetch_array($result1)) {
				$tabListCategory = array_merge($tabListCategory,explode(",",$row1['parents_list']));
				$tabListCategory = array_unique($tabListCategory);
			}			
			$this->categoryList = implode(", ", $tabListCategory);
		}
		$this->createTree();
	}
	/** Recursive method which creates the array which will contain the tree
		It doesn't need any parameter when it's called. Those are only used for the recursivity
		@param $index integer index used in the recursivity
		@param $deep integer contain the node's deep
	*/
	function createTree($index=0,$deep=0){						
		if($index<$this->parentsListSize){
			$idCategory = $this->parentsList[$index];
			$query2 = "SELECT id,name FROM ".$this->categoryTable." WHERE id_father='".$idCategory."' AND id In (".$this->categoryList.") ORDER BY name";
			if(($result2=mysql_query($query2))&&mysql_num_rows($result2)) {
				while($row2 = mysql_fetch_array($result2)) {
					$this->tabResult[$row2['id']] = array("name"=>$row2['name'],"deep"=>$deep);
					if(in_array($row2['id'],$this->parentsList)){
						$index++;
						$this->createTree($index,$deep+1);
					}					
				}
			}
		}
	}
	
	/** return the tree in an array
		@return mixed[] $tabResult
	*/
	function &getTree(){
		$this->createArbre();
		return($this->tabResult);
	}

	/** show the tree in html*/
	function show(){
		if(sizeof($this->tabResult)){
			?>
			<TABLE border="0" cellspacing="0" cellpadding="2" style="font-size:12px;FONT-FAMILY: Verdana;background-color:#0000BB;">
				<TR>
					<TD bgcolor="#8888BB">			
						<a href="<?=$_SERVER['SCRIPT_NAME']?>?idcat=<?=$this->idRoot?>"><font color="#FFFFFF">ROOT</font></a>
					</TD>
				</TR>			<?
			while(list($id,$values)=each($this->tabResult)){
				?>
				<TR>
					<TD>
						<?
						for($i=0;$i<$values['deep'];$i++){
							?>&nbsp;&nbsp;<?
						}
						?>
						<a href="<?=$_SERVER['SCRIPT_NAME']?>?idcat=<?=$id?>"><font color="#FFFFFF"><?=$values['name']?></font></a>
					</TD>
				</TR>
				<?
			}
			?></TABLE><?
		}
	}
}
?>
Return current item: Category Tree