基于 TextField 和列表菜单的标准搜索 [英] Criteria Search based on TextField and a List Menu

查看:40
本文介绍了基于 TextField 和列表菜单的标准搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行搜索,让用户能够根据特定条件搜索特定场所(夜总会).

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:

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.

我还希望当用户在文本框中输入P"并选择 westlands 时,westlands 中以字母P"开头的所有俱乐部都会显示出来.这不起作用.

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.

我的代码:

//get textfield 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);

截至目前,当我在文本框中输入P"并选择 westlands 时,无论位置如何,所有以字母 P 开头的俱乐部都会显示出来.我该如何解决这个问题?

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?

非常感谢任何帮助.

推荐答案

你的 where 子句中有一些奇怪的东西.

There is something strange in your where clause.

如果你测试:(A AND B) OR (A OR B)

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

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

如果 A 为真,则不需要 b 为真.这解释了你的第三个例子不起作用.我认为您应该测试您的价值并根据您的解释创建正确的 WHERE 子句.

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; 

这篇关于基于 TextField 和列表菜单的标准搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆