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

class Category{	
	
	/** @var integer contains the category's id*/
	var $id = null;

	/** @var string contains the category's name*/
	var $name = null;

	/** @var string contains the father's id*/
	var $id_father = null;

	/** @var string contains the category's table in database*/
	var $table = "category";

	/** @var integer contains the root's id in database*/
	var $id_root = 1;

	/** @var string contains the error's message*/
	var $error = "";
	
	/** @var string contains the parents' list*/
	var $parentsList = null;
	
	/** @param integer $id in order to edit an existant category. No parameter will create a new category
	*/
	function Category($id=null){
		if($id!=null){
			$query = "SELECT name,id_father,parents_list FROM ".$this->table." WHERE id=".$id;
			$result = mysql_query($query);
			if($row = mysql_fetch_array($result)){
				$this->name = $row['name'];
				$this->id_father = $row['id_father'];
				$this->parentsList = $row['parents_list'];
				$this->id = $id;
			}
		}
		else{
			$this->new = true;
		}
	}
	
	/** set or update the category's name*/
	function setName($name){
		$this->name = $name;
	}

	/** set or update the father's id*/
	function setFather($idFather){
		$this->id_father = $idFather;
	}
	
	/** return the category's name
		@return string $name
	*/
	function getName(){
		return $this->name;
	}
	
	/** return the father's id
		@return integer $id_father
	*/
	function getIdFather(){
		return $this->id_father;
	}
	
	/** return the parent's list
		@return string $parentsList
	*/
	function getparentsList(){
		return $this->parentsList;
	}
	
	/** return the error
		@return string $error
	*/
	function getError(){
		return $this->error;
	}
	
	/** save or update a category
		@return bool success or not
	*/
	function record(){
		if($this->id == null){
			//New Record
			if(($this->name != null)&&($this->id_father != null)){
				if($this->id_root != $this->id_father){
					$query = "SELECT id FROM ".$this->table." WHERE id=".$this->id_root." OR id=".$this->id_father;
					$result = mysql_query($query);
				}
				else{
					$query = "SELECT id FROM ".$this->table." WHERE id=".$this->id_root;
					$result2 = mysql_query($query);
				}
				if(((isset($result))&&(mysql_num_rows($result) == 2))||((isset($result2))&&(mysql_num_rows($result2) == 1))){
					$query = "SELECT parents_list FROM ".$this->table." WHERE id=".$this->id_father;
					$result = mysql_query($query);
					$row = mysql_fetch_array($result);
					$parents_list = $row['parents_list'];
					
					$query = "INSERT INTO ".$this->table." (name,id_father) VALUES (\"".$this->name."\",".$this->id_father.")";
					echo $query."<br>";
					if(mysql_query($query)){
						$id_new_category = mysql_insert_id();
						$query = "UPDATE ".$this->table." SET parents_list='".$parents_list.",".$id_new_category."' WHERE id=".$id_new_category;
						mysql_query($query);
						return(true);
					}
					else{
						$this->error = "Record failed : ".mysql_error();
						return(false);
					}
				}
				else{
					$this->error = "Root or Father doesn't exist";
					return(false);
				}
			}
			else{
				$this->error = "Name or/and Father are missing";
				return(false);
			}
		}
		else{
			//Update
			$query = "UPDATE ".$this->table." SET name='".$this->name."', id_father='".$this->id_father."', parents_list='".$this->parentsList."' WHERE id=".$this->id;
			mysql_query($query);
			return(true);		
		}
	}
}
?>
Return current item: Category Tree