All pastes #123698 Raw Edit

||cw

public php v1 · immutable
·published 2006-08-09 14:44 UTC
rendered paste body
<?phpclass Product{	var $db;	var $id;	var $name;	var $code;	var $category;			// __construct()	// takes an integer ID to lookup in the database or an array/object to load directly	function Product($id)	{		if (is_int($id))		{			global $db;			$this->db = $db; // database handler						$this->id = $id;						// get product data from db			$query = sprintf("				SELECT 					product.id, product.name, product.code, product.category_id, 					product_category.name AS category				FROM product				LEFT JOIN product_category				ON product_category.id = product.category_id				WHERE product.id = %d", $this->id);			$result = $this->db->get_row($query);						$this->name 	= $result->name;			$this->code	= $result->code;			$this->category	= $result->category;		}		else		{			$this->id = $id->id;			$this->name 	= $id->name;			$this->code	= $id->code;			$this->category	= $id->category;					}	}			/**	*	these functions return characteristics of the object	*	@access PUBLIC	*	@return STRING	*/	function getId()	{ return $this->id; }	function getName()	{ return $this->name; }	function getCode() 	{ return $this->code; }	function getCategory() 	{ return $this->category; }			/**	*	return list of products	*	@access STATIC	*	@return ARRAY	*/	function getListByCategory($categoryId)	{		global $db;				// get all products into array		$query = sprintf("			SELECT 				product.id, product.name, product.code, product.category_id, 				product_category.name AS category			FROM product			LEFT JOIN product_category			ON product_category.id = product.category_id			WHERE category_id = %d ORDER BY id ASC", $categoryId);		$result = $db->get_results($query);				// init		$productList = array();				foreach ($result as $key => $obj)		{			$productList[] = new Product($obj);		}				return $productList;	}}?>