<?php
class Category{

	private $conn;
	private $table_name = "category";

	public $id;	
	public $category;	

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

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

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

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

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

	function getAllRows(){
		$condition = "";
		if($this->category){
			$condition .= " WHERE category=:category ";
		}
		$query = "SELECT * FROM " . $this->table_name . $condition . " ORDER BY category ";
		$stmt = $this->conn->prepare($query);
		if($this->category) $stmt->bindParam(":category", $this->category);
		$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;
    }
    

}
?>
