Thread: Query string
View Single Post
Old 04-10-2005, 09:12 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,490
sde is on a distinguished road
you are saying you want the query to support multiple names, but a drop down menu only allows you to select one name. is this from multiple select boxes? or 1, multi-select box.

if you are controlling the drop down options, then you can put exact names in there. this will eleminate the need for the wild cards " % " around the search variable.

since you mention multiple names, for now i will forget about how you are getting them into the script and i'll just use an array. below are 2 methods which you could search for multiple names.

Code:
SELECT * FROM tablename WHERE selectname IN ('Joe Smith','Mike Jones')
Code:
SELECT * FROM tablename WHERE selectname='Joe Smith' OR selectname='Mike Jones'
with PHP, we could make these queries dynamically if the selected names were in an array. Here's an example:
PHP Code:
<?
$names 
= array('Joe Smith','Mike Jones');

// start sql
$sql "SELECT * FROM tablename WHERE selectname IN ";

for(
$i=0;$i<count($names);$i++){
  
$sql .= $names[$i];

  
// create a comma if it's not the last item in the array
  
if($i<$count)
    
$sql .= ",";
}

// finish sql string
$sql .= ")";

// query db
$result mysql_query($sql);
?>
you would use the same logic if you opted to use the other sql query too. you are just building a string from an array.
__________________
Mike
sde is offline   Reply With Quote