【问题标题】:autocomplete search base on city and locality基于城市和地区的自动完成搜索
【发布时间】:2017-06-14 12:20:06
【问题描述】:

您好,我一直在尝试根据选定的城市名称在数据库中创建公司或子类别的自动完整搜索,但它似乎对我不起作用。谁能帮我?以下是我的代码:

include( "dbconnect.php" );
$q = strtolower( $_GET['q'] );
$city = intval( $_GET['city_val'] );
if ( $city != "-1" )
{
    if ( !$q )
    {
    }
    else
    {
        $query = "SELECT DISTINCT concat(listing.company_name,' (',locality.locality,')') as name,listing.id as id,listing.type as type FROM listing,locality where listing.locality=locality.id AND listing.city='{$city}' AND LCASE(company_name) like '%".{$q}."%' UNION select DISTINCT sub_cat_name as name,subcategory.id as id,subcategory.type as type FROM subcategory where LCASE(sub_cat_name) like '%".{$q}."%' order by name";
    }
}
else
{
    if ( !$q )
    {
    }
    else

        $query = "SELECT DISTINCT concat(listing.company_name,'<br>(',locality.locality,' ',city.city,' ',state.statename,')') as name,listing.id as id,listing.type as type FROM listing,locality,city,state where listing.locality=locality.id AND listing.city=city.id AND listing.state=state.id AND LCASE(company_name) LIKE '%".{$q}."%' UNION select DISTINCT sub_cat_name as name,subcategory.id as id,subcategory.type as type FROM subcategory where LCASE(sub_cat_name) LIKE '%".{$q}."%' order by name";
   }
}
$results = mysql_query( $query );

while ( $result = mysql_fetch_array( $results ) )
{

    $array[] = array('name'=>$result['name'], 'id'=>$result['id'], 'type'=> $result['type']) ;
}   
echo json_encode($array);

您还可以在下面查看我的 jquery 代码:

<script>
  $(function() {
    $( "#companies" ).autocomplete({
      source: 'companies.php'
    });
  });
  </script>

【问题讨论】:

  • 您可以在控制台中看到什么?

标签: php jquery mysql autocomplete


【解决方案1】:

自动完成方法将“term”作为默认键传递。所以如果你想得到结果,你必须这样做:

$q = strtolower($_GET['term'] );

如果您想自定义它,请使用 ajax。

看到这个:

Can the default "Term" name passed in the "jquery UI autocomplete" feature be changed?

【讨论】:

    猜你喜欢
    • 2011-05-15
    • 2013-11-22
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    相关资源
    最近更新 更多