search form question
Moderator: General Moderators
search form question
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 :!!
gender, between the ages of XX and XX, country, and zip code
how would you do the SELECT sql statement??? thanks :!!
Code: Select all
"e;SELECT * FROM my_table where age > 15 and age < 21 and zipCode = 99223"e;- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
you can also use OR if you want.. depends how strict you want your query to be
note: && and || are invalid in SQL.
note: && and || are invalid in SQL.
Last edited by John Cartwright on Sat Mar 19, 2005 6:09 pm, edited 1 time in total.
You have other options check out the mysql.com its a good reference. you can also use ()
Code: Select all
"e;SELECT * FROM my_table where (age > 15 and age < 21) or zipCode = 99223"e;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";
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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;