Maybe I'm not thinking this through correctly but I have a company table with:
name
address
city
state
zip
I want to search two fields, name and city.
My query is:
SELECT DISTINCT city, name
FROM companies
WHERE city
LIKE '%$search%'
OR name
LIKE '%$search%'
and the results code is:
echo '<a href="city.php?city=' . $results['city'] . '">'
. $results['city'] . '</a>';
Now this only takes into account if the person searches for a city. How do I get it to show the page if someone searches for a company name? Something like:
echo '<a href="company.php?co=' . $results['name'] . '">'
. $results['name'] . '</a>';
Is there any way to show the proper result depending on what they searched for?
If you have only one search box, how can you know what the user is looking for?
You could add checkboxes/radio buttons to offer the choice between city
or name
.
If you don't want to do that then you do not know what the user is searching for. You can however run two queries, one for city
then another one for name
. If you get results you will at least know where they are coming from.
Why don't you compare the two fields of the results to the search text, to know what matched it: city or name. Something like:
if($search === $results['city']) echo '<a href="city.php?city=' . $results['city'] . '">'. $results['city'] . '</a>'; elseif($search === $results['name'])
echo '<a href="company.php?co=' . $results['name'] . '">'. $results['name'] . '</a>';
Not sure if I understood right...