Advertisement
Guest User

Untitled

a guest
Jun 9th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. <?php
  2. namespace App\main_class\Category;
  3. use App\main_class\Category\Message;
  4. use App\main_class\Category\Utility;
  5. use PDO;
  6.  
  7. class category
  8. {
  9.  
  10. public $id="";
  11. public $con="";
  12. public $username="root";
  13. public $password="";
  14. public $parent_id="";
  15. public $name="";
  16. public $discription="";
  17. public $status="";
  18. public $position="";
  19. public $creation_date="";
  20.  
  21. public function __construct()
  22. {
  23. try
  24. {
  25. $this->con= new PDO('mysql:host=localhost;dbname=online_shoping',$this->username,$this->password);
  26. $this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  27.  
  28. }catch (PDOException $ex) {
  29. echo 'ERROR';
  30. }
  31. }
  32.  
  33. public function prepare($data=array())
  34. {
  35.  
  36. if(is_array($data) && array_key_exists('parent_id',$data))
  37. {
  38. $this->parent_id=$data['parent_id'];
  39. }
  40. if(is_array($data) && array_key_exists('name',$data))
  41. {
  42. $this->name=$data['name'];
  43. }
  44. if(is_array($data) && array_key_exists('discription',$data))
  45. {
  46. $this->discription=$data['discription'];
  47. }
  48. if(is_array($data) && array_key_exists('status',$data))
  49. {
  50. $this->status=$data['status'];
  51. }
  52. if(is_array($data) && array_key_exists('position',$data))
  53. {
  54. $this->position=$data['position'];
  55. }
  56. if(is_array($data) && array_key_exists('creation_date',$data))
  57. {
  58. $this->creation_date=$data['creation_date'];
  59. }
  60. if (array_key_exists('id',$data) && !empty($data['id']))
  61. {
  62. $this->id=$data['id'];
  63. }
  64. return $this;
  65. }
  66.  
  67. public function store()
  68. {
  69.  
  70. $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)");
  71. $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));
  72.  
  73. if($query)
  74. {
  75. Message::message('Category Data is Added SuccessFully.');
  76. Utility::redirect();
  77. }
  78. else
  79. {
  80. Message::message('There is an Error While Storing Category Information,Please Try Again');
  81. Utility::redirect();
  82. }
  83. }
  84.  
  85. public function selectcategory()
  86. {
  87. $res=$this->con->query("select * From category where parent_id='0'");
  88. $res->execute();
  89. $query=$res->fetchAll(PDO::FETCH_ASSOC);
  90. return $query;
  91. }
  92.  
  93. public function fetchCategoryTree($parent = 0, $spacing = '', $user_tree_array = '') {
  94. if (!is_array($user_tree_array))
  95. $user_tree_array = array();
  96. $sql = "select id,parent_id,name from category WHERE 1 AND parent_id= :parent_id";
  97. $q = $this->con->prepare($sql);
  98. $q->bindParam(':parent_id',$parent);
  99. $q->execute();
  100. $q1 = $q->rowCount();
  101.  
  102. //var_dump($q1);
  103.  
  104. // die(); int(2)
  105. //$q2 = $q->fetch(PDO::FETCH_ASSOC);
  106. //var_dump($q2);
  107. //var_dump($user_tree_array);
  108. //die();
  109. //$query = mysql_query($sql);
  110. //return $q1;
  111. if ($q1 > 0) {
  112. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  113. // var_dump($row);
  114. // die();
  115. // echo $row["id"];
  116. // die();
  117.  
  118. $user_tree_array[] = array("id" => $row["id"], "name" => $spacing . $row["name"]);
  119. $user_tree_array = $this->fetchCategoryTree($row["id"], $spacing . '¦&nbsp;&nbsp;&nbsp;&nbsp;', $user_tree_array);
  120. }
  121. }
  122. return $user_tree_array;
  123. }
  124.  
  125. public function productcount($parent = 0, $spacing = '', $user_tree_array = '')
  126. {
  127. if (!is_array($user_tree_array))
  128. $user_tree_array = array();
  129.  
  130. $res=$this->con->query("SELECT category.name as category_name,count(product.id) as total_product FROM category
  131. left join product on
  132. product.categories_id=category.id
  133. group by category.id");
  134. $res->execute();
  135.  
  136. $query=$res->fetchAll(PDO::FETCH_ASSOC);
  137. return $query;
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement