Page 1 of 1
Looking to see how to search by a string with part of string
Posted: Sun Oct 31, 2004 1:41 pm
by briwal222
I have a restaurant search site. One of the methods to search is by name. What I have done is to create a response to the user if they mispell or don't exactly match the search method where they can search by the first letter of the restaurant. I would like to make the search more robust by allowing them to still get a result if they only type in part of the restaurant's name. I am having trouble when users search for a restaurant that may be called "The Blue Pony", where I would like them to find a result if they only typed in "Blue Pony"
Posted: Sun Oct 31, 2004 1:45 pm
by John Cartwright
if your information is stored in a mysql database they have a very efficient search engine built int
for example
Code: Select all
$sql = "SELECT `restaurants` FROM `listings` WHERE `name` LIKE %$name%";
of course, it all depends on how your database structure is.
set a minimum amount lf letters to search
Posted: Sun Oct 31, 2004 2:00 pm
by briwal222
Ok, thanks for the help. Got that part done, however, now if you type in one letter, like "a", all of the names that have the letter "a" in them show up. Is there a way to set a minimum amount of letters user must type in to the search?
Posted: Sun Oct 31, 2004 2:10 pm
by timvw
you could use [php_man]strlen[/php_man] to determine the length[/php_man]
Posted: Sun Oct 31, 2004 3:14 pm
by briwal222
Thanks, that helped. Now you have opened up another question. If I add this code:
Code: Select all
<?php
if (strlen($str)<=2) {
print"<table width="400"><tr><td>Please search with at least 3 letters or more.</td></tr></table>";
}?>
And I follow it with "exit;"
The text shows up on the bottom of the page, and blows away my formatting. Is there a way to follow that statement with something that would end the page without blowing away the formatting, or better yet, a way to redirect the user right back to the search page if they do not use at least 3 letters?
Posted: Sun Oct 31, 2004 5:02 pm
by rehfeld
you dont need to exit
just do
Code: Select all
if (strlen($string) > 2) {
// do you search code here
} else {
echo 'not long enough';
}
you could also redirect them by using the header() funciton,
or just echo a link back to the search page