I have two tables, 'states' and 'cities' and I would like the auto-complete to search any result in the two tables from the fields state and city. I haven't been able to do it yet but I have been able to search one table but cant seem to figure out how to search both.
Tables:
States
======
id
state
Cities
======
id
city
This is how I am looping through the result...
while (开发者_C百科$row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$row_array['id'] = $row['stateid'];
$row_array['value'] = $row['state'];
array_push($return_arr,$row_array);
}
When you connect to MySQL, instead of using a query on a single table, use this
select 'state' as type, id, state
from states
union all
select 'city', id, city
from cities
The resultant column named "State" contains states as well as city names. You will probably want to use the "type" column as well to identify whether the chosen name (from the array) is a state or city.
It may be easier to do a wrapper around it, so that you can filter on the combined result:
SELECT type, id, state
FROM
(
select 'state' as type, id, state
from states
union all
select 'city', id, city
from cities
) X
WHERE ... # you can put a where clause here
精彩评论