search form question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

search form question

Post by ecaandrew »

I've alaways been confused on this, how do you allow users to search with multiple choices, lets say

gender, between the ages of XX and XX, country, and zip code

how would you do the SELECT sql statement??? thanks :!!
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Code: Select all

&quote;SELECT * FROM my_table where age > 15 and age < 21 and zipCode = 99223&quote;
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

ohhh i see, so just keep using AND statements?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you can also use OR if you want.. depends how strict you want your query to be


note: && and || are invalid in SQL.
Last edited by John Cartwright on Sat Mar 19, 2005 6:09 pm, edited 1 time in total.
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

basically not strict, like i want it to be optional
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

You have other options check out the mysql.com its a good reference. you can also use ()

Code: Select all

&quote;SELECT * FROM my_table where (age > 15 and age < 21)  or zipCode = 99223&quote;
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

sounds good, now what if you have 4 checkboxes, and have multiple choices, how would you tie that in? thanks !!
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Untested:

Code: Select all

<?php
$where = '';
if(isset($_POST['checkbox'])){

if($where=='')
$where = ' WHERE ';

if($where !=' WHERE ')
$where .= ' AND ';

$where .= "myField = 1"

}

$qry = "SELECT * FROM my_table $where";
?>
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

awesome ill try it out :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$count = count($_POST['checkbox']);

for ($x=0;$x >= $count; $x++)
{
     $sql = "SELECT * FROM `table` WHERE ";

     if ($_POST['checkbox'][$x] == 'checked')     
     {
          $sql .= "`value` = '".$_POST['checkbox'][$x]."'";     
     
          if ($x != $count)
          {
               $sql .= ' AND ';
          }
     }
}

echo $sql;
Hope that gives you an idea..
Post Reply