Location: PHPKode > projects > @1 File Store > libs/databasetree.php
<?
class databasetree
{
 var $datalist=array();
 var $rootlist=array();
 var $rootcnt=0;
 var $path=array();
 var $category=array();
 	
 	function databasetree($mysql, $tablename, $all=0)
	{
	 $SQL="SELECT * FROM `".$tablename."`";
	 $SQL.=" ORDER BY `parent`";
	 $mysql->query($SQL);
	 if($mysql->errno!=0 || $mysql->num<=0)
	 {
	  $this->rootcnt=0;
	  return;
	 }
	 while(!$mysql->EOF)
	 {
	  $this->additem(intval($mysql->field("parent")), 
	  		 intval($mysql->field("id")),
	  		 $mysql->field("folder_name"),
	  		 $mysql->field("folder_desc")
	  		 );
	  $mysql->movenext();
	 }
	}

	function additem($parent, $child, $text, $description)
	{
	 $this->datalist[$child]=array("name"=>$text,
	 				   "description"=>$description,
	 			       "childcnt"=>0,
	 			       "childs"=>array()
	 			      );
	 if($parent!=0 && @isset($this->datalist[$parent]))
	 {
	  $childcnt=$this->datalist[$parent]["childcnt"];
	  $this->datalist[$parent]["childs"][$childcnt]=$child;
	  $this->datalist[$parent]["childcnt"]=$childcnt+1;
	 }else
	  {
	   $this->rootlist[$this->rootcnt++]=$child;
	  }
	}
	
	function get_array($zero="Select", $fill="-")
	{
	 $result=array(0=>$zero);
	 for($i=0; $i<$this->rootcnt; $i++)
	 {
	  $num=$this->rootlist[$i];
	  $result[$num]=str_repeat($fill, 1).$this->datalist[$num]["name"];
	  if($this->datalist[$num]["childcnt"]>0)
	  {
	   $this->get_array_child($result,
	   			  $this->datalist[$num]["childs"],
	   			  $this->datalist[$num]["childcnt"],
	   			  2,
	   			  $fill
	   			 );
	  }
	 }
	 return $result;
	}

	function get_array_child(&$result, $childs, $cnt, $level, $fill)
	{
	 for($i=0; $i<$cnt; $i++)
	 {
	  $num=$childs[$i];
	  $result[$num]=str_repeat($fill, $level).$this->datalist[$num]["name"];
	  if($this->datalist[$num]["childcnt"]>0)
	  {
	   $this->get_array_child($result,
	   			  $this->datalist[$num]["childs"],
	   			  $this->datalist[$num]["childcnt"],
	   			  $level+1,
	   			  $fill
	   			 );
	  }
	 }
	}

	function get_smarty()
	{
	 $result=array();
	 for($i=0; $i<$this->rootcnt; $i++)
	 {
	  $num=$this->rootlist[$i];
	  $result[]=array("id"=>$num, "cnt"=>1, "folder_name"=>$this->datalist[$num]["name"]);
	  if($this->datalist[$num]["childcnt"]>0)
	  {
	   $this->get_smarty_child($result,
	   			  $this->datalist[$num]["childs"],
	   			  $this->datalist[$num]["childcnt"],
	   			  2
	   			 );
	  }
	 }
	 return $result;
	}
	
	function get_smarty_child(&$result, $childs, $cnt, $level)
	{
	 for($i=0; $i<$cnt; $i++)
	 {
	  $num=$childs[$i];
	  $result[]=array("id"=>$num, "cnt"=>$level, "folder_name"=>$this->datalist[$num]["name"]);
	  if($this->datalist[$num]["childcnt"]>0)
	  {
	   $this->get_smarty_child($result,
	   			  $this->datalist[$num]["childs"],
	   			  $this->datalist[$num]["childcnt"],
	   			  $level+1
	   			 );
	  }
	 }
	}
	
}
?>
Return current item: @1 File Store