donglin1192 2017-01-29 03:29
浏览 113

基于城市和地区的自动完成搜索

Hi I've been trying to create auto complete search of companies or subcategories in database base on selected city name but it doesnt seem to work for me. Can anyone help me? below is my code:

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);

you can also see my jquery code below:

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

1条回答 默认 最新

  • douxing8939 2017-01-29 04:59
    关注

    autocomplete method pass "term" as a default key. So if you want to get result you have to do this:

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

    If you want to customize it then use ajax.

    see this:

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

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序