开发者

Criteria Search based on TextField and a List Menu

I am trying to make a search where a user can be able to search for certain establishments (night-clubs) based on certain criteria.

As of now, I have made a search box whereby a user can enter the name of a club and a list menu to select a location.

The drop down list menu (dynamically generated from the database)is as follows:

Location:

Select
Westlands
Eastleigh
Langata
Kamukunji
Dagoretti
Starehe 
Makadara

I want to make it such that if a user leaves the textfield blank and selects westlands and hits the search button, all the clubs in westlands show up. This is working fine.

Also, a user can enter something into the textbox and leave the list menu as default select and the matching results will be displayed. This is working fine too.

I also want it that when a user say types "P" into the textbox, and selects westlands, all the clubs in westlands that start with the letter "P" to show up. This is not working.

My code:

//get textf开发者_如何转开发ield search value
$search_value = "-1";
if (isset($_POST['event_search'])) 
{
  $search_value = $_POST['event_search'];
  $search_value = mysql_real_escape_string($search_value);
}

//get location search value
$location_search_value = "-1";
if (isset($_POST['location'])) 
{
  $location_search_value = $_POST['location'];
  $location_search_value = mysql_real_escape_string($location_search_value);
}

//query establishments table
mysql_select_db($database_connections, $connections);
$query_establishment = "SELECT establishment_id, establishment_thumb_url, establishment_name, establishment_pricing, location_name
FROM establishment JOIN location ON establishment.location_id = location.location_id WHERE (establishment_name LIKE '".$search_value."%' 
AND establishment.location_id = '$location_search_value') OR (establishment_name LIKE '".$search_value."%' 
OR establishment.location_id = '$location_search_value')";
$establishment = mysql_query($query_establishment, $connections) or die(mysql_error());
$totalRows_establishment = mysql_num_rows($establishment);

As of right now, when I enter "P" into the textbox, and select westlands, all the clubs starting with the letter P show up regardless of location. How do I fix this?

Mucho appreciation for any help.


There is something strange in your where clause.

If you test : (A AND B) OR (A OR B)

A => a.establishment_name LIKE '".$search_value."'
B => a.location_id = '".$location_search_value."'

IF A is true, then there is no need that b is true. And this explain your third example is not working. I think you should test your value and create the correct WHERE clause based on your explanation.

if($search_value != "" && $location_search_value == "") {
    $where = "a.establishment_name LIKE '".$search_value."'";
} else if ($search_value == "" && $location_search_value != "") {
    $where = "a.location_id = '".$location_search_value."'";
} else {
    $where = "(a.establishment_name LIKE '".$search_value."' AND a.location_id = '".$location_search_value."')";
}
$query = "SELECT a.*, b.location_name ".
         "FROM establishment a ".
         "JOIN location b ON a.location_id = b.location_id ".
         "WHERE ".$where; 


There is an error in the query. You inserted $search_value correctly, but You used single quotes without .'s on the second variable. Try this:

$query = "SELECT a.*, b.location_name
  FROM establishment a JOIN location b ON a.location_id = b.location_id
  WHERE (a.establishment_name LIKE '".$search_value."' AND a.location_id = '".$location_search_value."')
  OR (a.establishment_name LIKE '".$search_value."' OR a.location_id = '".$location_search_value."')";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜