Advertisement
pbowers

interest query

Jan 22nd, 2017
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 KB | None | 0 0
  1. if(!empty($_POST)){
  2.     $sql = 'SELECT * FROM users WHERE 1 = 2';  // if no interests chosen, don't return anything
  3.     foreach (['small_animals', 'large_animals', 'animal_habitat', 'environment, ...] as $var) {
  4.         $inp = Input::get($var);
  5.         if ($inp == 1) {
  6.             $val = str_replace('_', ' ', $var);
  7.             // since your code has the values hard-coded there's no need to bind it - no security risk of injection or etc
  8.             $sql .= " OR ($var = '$var')";
  9.         }
  10.     }
  11.     // now $sql contains a full query like "SELECT * FROM users WHERE 1 = 2 OR (homeless = 'homeless') OR (environment = 'environment')
  12.     $sql = "SELECT * FROM users $where";
  13.     $interested_users = $db=>query($sql)=>results();
  14. }
  15.  
  16. // I skipped the rest of the HTML and etc code - this is the loop that you had to display the results:
  17.  
  18. /*
  19.                 if(!empty($_POST)){
  20.                     echo "<strong>Here's what we found...</strong><br>";
  21.                     if(!empty($matches)){
  22.                         foreach ($matches as $m){
  23.                             $foundQ = $db->query("SELECT * FROM users WHERE id = ?",($m));
  24.                             $found = $foundQ->first();
  25.                             echo $found->fname." ".$found->lname."<br>";
  26.                         }
  27.                     }else{
  28.                         echo "Sorry. No users were found";
  29.                     }
  30.                 }
  31. */
  32.  
  33. // I replaced it with this:
  34.  
  35. if (!empty($_POST)) {
  36.     echo "here's what we found";
  37.     foreach ($interested_users as $result) {
  38.         echo $result->fname.' '.$result->lname.'<br />';
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement