Advertisement
AlexWebDevelop

Untitled

Oct 8th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.81 KB | None | 0 0
  1. <?php
  2.  
  3. // 1. read the request string values
  4. $category = $_REQUEST['category'];
  5. $location = $_REQUEST['location'];
  6.  
  7. // 2. validate them
  8.  
  9. // 3. build the basic query
  10. $query = 'SELECT * FROM TABLE';
  11.  
  12. // 4. dynamic query parameters
  13. $categoryAdded = FALSE;
  14. $locationAdded = FALSE;
  15.  
  16. if (!empty($category))
  17. {
  18.     $query .= ' WHERE category = :category';
  19.     $categoryAdded = TRUE;
  20. }
  21.  
  22. if (!empty($location))
  23. {
  24.     if ($categoryAdded)
  25.     {
  26.         $query .= ' AND';
  27.     }
  28.     else
  29.     {
  30.         $query .= ' WHERE';
  31.     }
  32.    
  33.     $query .= ' WHERE location = :location';
  34.     $locationAdded = TRUE;
  35. }
  36.  
  37. $stmt= $db->prepare($query);
  38.  
  39. if ($categoryAdded)
  40. {
  41.     $db->bindValue(':category', $category, PDO::PARAM_STR);
  42. }
  43.  
  44. if ($locationAdded)
  45. {
  46.     $db->bindValue(':location', $location, PDO::PARAM_STR);
  47. }
  48.  
  49. // then execute the query and do the rest.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement