<?php
class Brand{

	private $conn;
	private $table_name = "brand";

	public $id;	
	public $category;	
	public $brand;	

	public function __construct($db){
		$this->conn = $db;
	}

	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET category=:category, brand=:brand";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":category", $this->category);
		$stmt->bindParam(":brand", $this->brand);

		if($stmt->execute()){
			return true;
		}	 
		return false;
	}

	function exist(){
		$query = "SELECT id FROM " . $this->table_name . " WHERE category = ? and brand= ? LIMIT 0,1";
		$stmt = $this->conn->prepare( $query );	
	 
		$stmt->bindParam(1, $this->category);
		$stmt->bindParam(2, $this->brand);
		
		$stmt->execute();
		if($stmt->rowCount()>0){
			return true;
		}
		return false;
	}

	function getAllRows(){
		$condition = "";

		if($this->category){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " category=:category ";
		}
		if($this->brand){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " brand=:brand ";
		}
		if($condition!=""){
			$condition = " WHERE " . $condition;
		}

		$query = "SELECT * FROM " . $this->table_name . $condition . " ORDER BY category,brand ";
		$stmt = $this->conn->prepare($query);
		if($this->category) $stmt->bindParam(":category", $this->category);
		if($this->brand) $stmt->bindParam(":brand", $this->brand);
		$stmt->execute();
		return $stmt;
	}

    function delete(){
		$query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
		$stmt = $this->conn->prepare($query);
		$stmt->bindParam(1, $this->id);
		if($stmt->execute()){
			return true;
		}
		return false;
    }
    

}
?>
