尝试实施类似于here的搜索。这会根据城市,地区,属性名称和构建器名称搜索属性。现在只有较小的版本可以搜索地点我追加城市名称的名称。
$(document).ready(function(){
//alert("Entered");
$("#searchId").keyup(function()
{
var searchbox = $(this).val();
var dataString = 'searchword='+ searchbox;
if((searchbox=='') || (searchbox=='e.g') )
{
//$('#display').css('display') == 'none';
$('#display').hide();
}
else
{
if("#searchId".length>3)
{
$.ajax({
type: "POST",
url: "search.php",
data: dataString,
cache: false,
success: function(html)
{
$("#display").html(html).show();
$(".name").click(function(){
// alert($(this).text());
$("input#searchId").val($(this).text());
$('#display').hide();
});
}
});
}
}return false;
});
});
在search.php中,我有这样的查询,
$sql_res=mysql_query("select * from locality as loc inner join cities as cit on loc.city_id=cit.city_id where locality_name like '%$q%' or city_name like '%$q%' order by locality_id LIMIT 5");
但我希望它的功能类似于给定的链接。下拉列表应显示所选条件的名称及其前面的类型(例如:Pune(条件)城市(类型)) 。如何编写 sql语句和ajax / jquery 函数来实现这个?
修改 添加输入框的HTML
<input type="text" autocomplete="off" class="homeinput ac_input" title="Search" id="searchId" name="SearchValue" value="e.g lodha new cuffe parade, thane mumbai" onFocus="if(this.value=='e.g lodha new cuffe parade, thane mumbai') this.value='';" onBlur="if(this.value=='') this.value='e.g lodha new cuffe parade, thane mumbai';">
感谢您的时间。
答案 0 :(得分:1)
我认为你最好有两种不同的MySQL搜索。像...
$sql_res=mysql_query("select * from locality as loc inner join cities as cit on loc.city_id=cit.city_id where locality_name like '%$q%' order by locality_id LIMIT 5");
然后第二个......
$sql_res=mysql_query("select * from locality as loc inner join cities as cit on loc.city_id=cit.city_id where city_name like '%$q%' order by locality_id LIMIT 5");
然后使用你的php将每个查询的结果与每行末尾的[type]相连接。你的jQuery实际上不需要改变。
答案 1 :(得分:1)
在您查看下面的代码之前,请注意以下几点:
然而,这是正确的方法:
(
SELECT
'Locality' AS `type`,
`locality_name` AS `name`
FROM
`locality`
WHERE
`locality_name` LIKE '%$q%'
)
UNION ALL
(
SELECT
'City' AS `type`,
`city_name` AS `name`
FROM
`cities`
WHERE
`city_name` LIKE '%$q%'
)