Advertisement
Guest User

filter-sort

a guest
Jun 2nd, 2015
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.08 KB | None | 0 0
  1.     /**
  2.     * Sort & filter query
  3.     *
  4.     * @sorts (array) $sorts - Array containing sort and filter options
  5.     */
  6.     public function sortFilter($sorts, $user_id) {
  7.  
  8.             $tab = "";
  9.             $letter = "";
  10.             $userIDS = array();
  11.             $sort = "";
  12.             $active = "";
  13.             $setLetter = "";
  14.             $setYear = "";
  15.             $lett = "";
  16.             $follow = "";
  17.             $ref = "";
  18.             $prep = "";
  19.             $year = false;
  20.  
  21.             if(array_key_exists('tab', $sorts) && $sorts['tab'] === "follow")
  22.             {
  23.                 $tab = "follow";
  24.                 $params = array();
  25.                 $prep = array();
  26.                 $ref = array();
  27.  
  28.                 $userIDS = $this->userTools->userFollowing($user_id);
  29.  
  30.                 foreach($userIDS as $id)
  31.                 {
  32.                     $ref[] = $id['followed'];
  33.                 }
  34.  
  35.                 $params = implode(",", array_fill(0, count($userIDS), "?"));
  36.                 $prep = implode("", array_fill(0, count($userIDS), "i"));
  37.  
  38.                 $follow = "i.user_id IN (".$params.") AND";
  39.  
  40.                 $active = 1;
  41.                 $order = "i.id DESC";
  42.  
  43.             }
  44.             else if(array_key_exists('tab', $sorts) && $sorts['tab'] === "expired")
  45.             {
  46.                 $tab = "expire";
  47.                 $active = 0;
  48.                 $order = "i.id DESC";
  49.                 //echo "Expire tab";
  50.             }else{
  51.                 $tab = "latest";
  52.                 $active = 1;
  53.                 $order = "i.id DESC";
  54.                 //echo "Latest tab";
  55.             }
  56.  
  57.             if(array_key_exists('letter', $sorts))
  58.             {
  59.                 $letter = $sorts['letter'];
  60.  
  61.                 $let = array();
  62.                 $ref[] = $letter;
  63.                
  64.                 $params = $letter;
  65.                 $prep = "s";
  66.  
  67.                 $setLetter = "AND i.item_title LIKE CONCAT(?, '%')";
  68.             }
  69.  
  70.             if(array_key_exists('publisher', $sorts))
  71.             {
  72.                 $issue = $sorts['publisher'];
  73.  
  74.                 $setIssue = "AND i.item_number = ?";
  75.             }
  76.  
  77.             if(array_key_exists('year', $sorts))
  78.             {
  79.                 $year = $sorts['year'];
  80.                
  81.                 $yearArr = array();
  82.                 $yearArr[] = $sorts['year'];
  83.                 $ref = $this->helperClass->valRef($yearArr);
  84.  
  85.                 $params = $year;
  86.                 $prep = "i";
  87.  
  88.                 $setYear = "AND i.year = ?";
  89.             }
  90.  
  91.             if(array_key_exists('number', $sorts))
  92.             {
  93.                 $issue = $sorts['number'];
  94.  
  95.                 $setIssue = "AND i.item_number = ?";
  96.             }
  97.  
  98.             if(array_key_exists('cond', $sorts))
  99.             {
  100.                 $cond = $sorts['cond'];
  101.                
  102.                 $setCond = "AND i.item_condition = ?";
  103.             }
  104.  
  105.             if(array_key_exists('option', $sorts) && $sorts['option'] === "low-shipping")
  106.             {
  107.                 //echo "LOW SHIP";
  108.                 $sort = "low_shipping";
  109.                 $order = "c.ship_cost ASC";
  110.             }
  111.  
  112.             if(array_key_exists('option', $sorts) && $sorts['option'] === "low-price")
  113.             {
  114.                 //echo "LOW PRICE";
  115.                 $sort = "low_price";
  116.                 $order = "maxbid ASC, i.item_start ASC";
  117.             }
  118.  
  119.             if(array_key_exists('option', $sorts) && $sorts['option'] === "high-price")
  120.             {
  121.                 //echo "HIGH PRICE";
  122.                 $sort = "high-price";
  123.                 $order = "maxbid, i.item_start DESC";
  124.             }
  125.  
  126.             if( !empty($userIDS) && $letter )
  127.             {
  128.                 $ref = array();
  129.  
  130.                 foreach($userIDS as $id)
  131.                 {
  132.                     $ref[] = $id['followed'];
  133.                 }
  134.  
  135.                 $ref[] = $letter;
  136.  
  137.                 $params = implode(",", array_fill(0, count($userIDS), "?"))."?";
  138.                 $prep = implode("", array_fill(0, count($userIDS), "i"))."s";
  139.  
  140.             }
  141.  
  142.             $q = $this->db->mysqli->prepare("SELECT c.ship_cost, c.item_id, i.id, i.user_id, i.item_title, i.item_number, i.item_year, i.item_condition, i.item_start, i.item_description, i.active, u.first_name, u.last_name, CAST(u.fb_id AS CHAR(50)) AS fb_id, u.user_pic, MAX(b.bid) AS maxbid, COUNT(b.bid) AS bids, p.publisher_name
  143.                                                                     FROM countries_ship c
  144.                                                                     JOIN items i
  145.                                                                         ON c.item_id = i.id
  146.                                                                     JOIN users u
  147.                                                                         ON i.user_id = u.id
  148.                                                                     LEFT JOIN bids b
  149.                                                                         ON i.id = b.item_id
  150.                                                                     LEFT JOIN publishers p
  151.                                                                         ON i.item_publisher = p.id
  152.                                                                     WHERE ".$follow." i.active = ".$active." ".$setLetter." ".$setYear."
  153.                                                                     GROUP BY i.id
  154.                                                                     ORDER BY ".$order." LIMIT 18");
  155.  
  156.                 if( $tab === "follow" || $letter || $year)
  157.                 {
  158.                     call_user_func_array('mysqli_stmt_bind_param', array_merge ( array($q, $prep), $this->helperClass->valRef($ref) ) );
  159.                 }
  160.  
  161.                 $q->execute();
  162.  
  163.                 $rows = $this->helperClass->bindResults($q);
  164.  
  165.                 $q->close();
  166.  
  167.                 return $rows;
  168.  
  169.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement