Looking to see how to search by a string with part of string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
briwal222
Forum Newbie
Posts: 10
Joined: Fri Oct 29, 2004 9:47 am

Looking to see how to search by a string with part of string

Post 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"
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
briwal222
Forum Newbie
Posts: 10
Joined: Fri Oct 29, 2004 9:47 am

set a minimum amount lf letters to search

Post 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?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

you could use [php_man]strlen[/php_man] to determine the length[/php_man]
briwal222
Forum Newbie
Posts: 10
Joined: Fri Oct 29, 2004 9:47 am

Post 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) &#123;
print"<table width="400"><tr><td>Please search with at least 3 letters or more.</td></tr></table>";

&#125;?>
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?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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
Post Reply