0

Hi

This is probably a fairly easily solved issue but I am struggling at the moment. I basically just want to be able to search a database but with several different options, any of which can be used and any combination can be used. The options will be name, date and location.

What is the best way to do the search, I only want to search if something is entered into the form for that part so if no name is entered I do not want it to search for that etc.

Another issue is the date and location are drop down menus, how do I use if(isset) for drop down menus, do I leave a black <select> option?

Thanks in advance

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by diafol
0

isset should be fine.

if(isset($_POST['name']))$fields[] = "`name` = '". mysql_real_escape_string($_POST['name'])  ."'";

if(isset($_POST['date']))$fields[] = "`date` = '". mysql_real_escape_string($_POST['date'])  ."'";

if(isset($_POST['location']))$fields[] = "`location` = '". mysql_real_escape_string($_POST['location'])  ."'";

if(isset($fields)){
  $pairs = " WHERE " . explode(',',$fields) : '';
}else{
  $pairs = "";
}
$query = "SELECT field1 FROM table1" . $pairs; 

That will return all data if no parameters entered. If you don't want that, then you can modify to this:

if(isset($fields)){
  $query = "SELECT field1 FROM table1 WHERE " . explode(',',$fields);
  ...
}else{
  echo "Apply a search item";
}

I should note that there are better ways (and shorter ways) of doing this. But I thought this would be the easiest.

Edited by diafol: expand ternery conditional

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.