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>