Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. <?php
  2. require_once($_SERVER['DOCUMENT_ROOT'].'/config/configuration.php');
  3. require_once($_SERVER['DOCUMENT_ROOT'].'/config/functions.php');
  4.  
  5. $page = (int) $_GET['page'];
  6. $section = ( isset( $_GET['section'] ) ) ? $_GET['section'] : 'man';
  7.  
  8. $filter_price = ( isset( $_GET['filter_price'] ) ) ? explode('-', $_GET['filter_price']) : [];
  9.  
  10. $sql_count_rows = "
  11. SELECT COUNT(id) as len FROM products
  12. WHERE id IN
  13. (
  14. SELECT pc.product_id FROM catalogs as p
  15. JOIN product_catalog as pc ON p.id=pc.catalog_id
  16. WHERE code = '{$section}'
  17. )
  18. ";
  19.  
  20. if( !empty( $filter_price ) ){
  21. $sql_count_rows .= "AND price >={$filter_price[0]} AND price <={$filter_price[0]}";
  22. }
  23.  
  24. $result_count_rows = mysqli_query($db, $sql_count_rows);
  25. $result_count_rows_arr = mysqli_fetch_assoc($result_count_rows);
  26. $count_rows = $result_count_rows_arr['len'];
  27.  
  28. $count_products_on_page = 5;
  29.  
  30. // ceil() - округление в большую сторону
  31. $count_page = ceil($count_rows / $count_products_on_page);
  32.  
  33. $response = [
  34. 'products'=>[],
  35. 'pagination'=>[
  36. 'countPage'=> $count_page,
  37. 'nowPage'=>$page
  38. ]
  39. ];
  40.  
  41. $from_row = $count_products_on_page * ($page - 1);
  42. /*
  43. 1 - 0
  44. 2 - 5
  45. 3 - 10
  46. */
  47. $sql_products = "
  48. SELECT * FROM products
  49. WHERE id IN
  50. (
  51. SELECT pc.product_id FROM catalogs as p
  52. JOIN product_catalog as pc ON p.id=pc.catalog_id
  53. WHERE code = '{$section}'
  54. )";
  55.  
  56. if( !empty( $filter_price ) ){
  57. $sql_products .= " AND price >={$filter_price[0]} AND price <={$filter_price[0]}";
  58. }
  59.  
  60. $sql_products .= " LIMIT {$from_row}, {$count_products_on_page}";
  61. $result_products = mysqli_query($db, $sql_products);
  62.  
  63. while( $row = mysqli_fetch_assoc($result_products) ){
  64. // d($row);
  65. $response['products'][] = $row;
  66. }
  67.  
  68.  
  69. echo json_encode( $response );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement