Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.91 KB | None | 0 0
  1. <?php
  2.  
  3. class Products
  4. {
  5.     const COUNT_OF_PRODUCTS = 6;
  6.    
  7.     public static function getRecommended()
  8.     {
  9.         $db = DataBase::connectDB();
  10.        
  11.         $result = $db->query("SELECT * FROM products WHERE `is_recommended` = 1");
  12.         //$result->setFetchMode(PDO::FETCH_ASSOC);
  13.        
  14.         $res = array();
  15.         while($row = $result->fetch(PDO::FETCH_ASSOC))
  16.             $res[] = $row;
  17.        
  18.         return $res;
  19.     }
  20.    
  21.     public static function getCurrentProduct($id)
  22.     {
  23.         $db = DataBase::connectDB();
  24.        
  25.         $result = $db->query("SELECT * FROM products WHERE `id` = $id");
  26.        
  27.         $res = $result->fetch(PDO::FETCH_ASSOC);
  28.        
  29.         return $res;
  30.     }
  31.    
  32.     public static function setCurrentProduct($id)
  33.     {
  34.         $db = DataBase::connectDB();
  35.        
  36.         $name = $_POST['name'];
  37.         $price = strval($_POST['price']);
  38.         $description = $_POST['description'];
  39.         $is_recommended = $_POST['is_recommended'];
  40.         $prod_status = $_POST['prod_status'];
  41.         $is_availible = $_POST['is_availible'];
  42.         $res = $db->prepare("UPDATE `products`
  43.         SET `name` = :name,
  44.         `price` = :price,
  45.         `description` = :description,
  46.         `is_recommended` =  :is_recommended,
  47.         `status` = :prod_status,
  48.         `is_availivle` = :is_availible
  49.         WHERE `id` = :id");
  50.        
  51.         $res->bindParam(':name', $name);
  52.         $res->bindParam(':price', $price);
  53.         $res->bindParam(':description', $description);
  54.         $res->bindParam(':is_recommended', $is_recommended);
  55.         $res->bindParam(':prod_status', $prod_status);
  56.         $res->bindParam(':is_availible', $is_availible);
  57.         $res->bindParam(':id', $id);
  58.        
  59.         return $res->execute();
  60.     }
  61.    
  62.     public static function getProducts($category = 0,$page = 1, $count = self::COUNT_OF_PRODUCTS)
  63.     {
  64.         $db = DataBase::connectDB();
  65.        
  66.         $result = ($category != 0)?$db->query("SELECT * FROM products WHERE `status` = 1 AND `category` = $category ORDER BY `id` DESC LIMIT $count OFFSET ".$count*($page-1)):
  67.                     $db->query("SELECT * FROM products WHERE `status` = 1 ORDER BY `id` DESC LIMIT $count OFFSET ".$count*($page-1));
  68.        
  69.         $res = array();
  70.         if($result)
  71.             while($row = $result->fetch(PDO::FETCH_ASSOC))
  72.                 $res[] = $row;
  73.        
  74.         return $res;
  75.     }
  76.    
  77.     public static function getTotalProductsInCategory($categoryId = 0)
  78.     {
  79.         // Соединение с БД
  80.         $db = DataBase::connectDB();
  81.         // Текст запроса к БД
  82.         $sql = ($categoryId)?'SELECT count(id) AS count FROM products WHERE status="1" AND category = :category_id':'SELECT count(id) AS count FROM products WHERE status="1"';
  83.         // Используется подготовленный запрос
  84.         $result = $db->prepare($sql);
  85.         $result->bindParam(':category_id', $categoryId, PDO::PARAM_INT);
  86.         // Выполнение коменды
  87.         $result->execute();
  88.         // Возвращаем значение count - количество
  89.         $row = $result->fetch();
  90.         return $row['count'];
  91.     }
  92.    
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement