Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\main_class\Category;
- use App\main_class\Category\Message;
- use App\main_class\Category\Utility;
- use PDO;
- class category
- {
- public $id="";
- public $con="";
- public $username="root";
- public $password="";
- public $parent_id="";
- public $name="";
- public $discription="";
- public $status="";
- public $position="";
- public $creation_date="";
- public function __construct()
- {
- try
- {
- $this->con= new PDO('mysql:host=localhost;dbname=online_shoping',$this->username,$this->password);
- $this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
- }catch (PDOException $ex) {
- echo 'ERROR';
- }
- }
- public function prepare($data=array())
- {
- if(is_array($data) && array_key_exists('parent_id',$data))
- {
- $this->parent_id=$data['parent_id'];
- }
- if(is_array($data) && array_key_exists('name',$data))
- {
- $this->name=$data['name'];
- }
- if(is_array($data) && array_key_exists('discription',$data))
- {
- $this->discription=$data['discription'];
- }
- if(is_array($data) && array_key_exists('status',$data))
- {
- $this->status=$data['status'];
- }
- if(is_array($data) && array_key_exists('position',$data))
- {
- $this->position=$data['position'];
- }
- if(is_array($data) && array_key_exists('creation_date',$data))
- {
- $this->creation_date=$data['creation_date'];
- }
- if (array_key_exists('id',$data) && !empty($data['id']))
- {
- $this->id=$data['id'];
- }
- return $this;
- }
- public function store()
- {
- $query=$this->con->prepare("INSERT INTO category (`parent_id`,`name`,`location`,`discription`,`status`,`position`,`creation_date`) values (:parent_id,:name,:location,:discription,:status,:position,:creation_date)");
- $query->execute(array(':parent_id'=>$this->parent_id,':name'=>$this->name,':location'=>$this->location,':discription'=>$this->discription,':status'=>$this->status,':position'=>$this->position,':creation_date'=>$this->creation_date));
- if($query)
- {
- Message::message('Category Data is Added SuccessFully.');
- Utility::redirect();
- }
- else
- {
- Message::message('There is an Error While Storing Category Information,Please Try Again');
- Utility::redirect();
- }
- }
- public function selectcategory()
- {
- $res=$this->con->query("select * From category where parent_id='0'");
- $res->execute();
- $query=$res->fetchAll(PDO::FETCH_ASSOC);
- return $query;
- }
- public function fetchCategoryTree($parent = 0, $spacing = '', $user_tree_array = '') {
- if (!is_array($user_tree_array))
- $user_tree_array = array();
- $sql = "select id,parent_id,name from category WHERE 1 AND parent_id= :parent_id";
- $q = $this->con->prepare($sql);
- $q->bindParam(':parent_id',$parent);
- $q->execute();
- $q1 = $q->rowCount();
- //var_dump($q1);
- // die(); int(2)
- //$q2 = $q->fetch(PDO::FETCH_ASSOC);
- //var_dump($q2);
- //var_dump($user_tree_array);
- //die();
- //$query = mysql_query($sql);
- //return $q1;
- if ($q1 > 0) {
- while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
- // var_dump($row);
- // die();
- // echo $row["id"];
- // die();
- $user_tree_array[] = array("id" => $row["id"], "name" => $spacing . $row["name"]);
- $user_tree_array = $this->fetchCategoryTree($row["id"], $spacing . '¦ ', $user_tree_array);
- }
- }
- return $user_tree_array;
- }
- public function productcount($parent = 0, $spacing = '', $user_tree_array = '')
- {
- if (!is_array($user_tree_array))
- $user_tree_array = array();
- $res=$this->con->query("SELECT category.name as category_name,count(product.id) as total_product FROM category
- left join product on
- product.categories_id=category.id
- group by category.id");
- $res->execute();
- $query=$res->fetchAll(PDO::FETCH_ASSOC);
- return $query;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement