search results with PHP

Wednesday 27th June 2007 04:52 PM

TAGS: PHP, Search

Searching can be simple in PHP, but it exponentially gets more difficult when you want more features and more options. Personally, I think using GET is vital when searching, as it doesn't break the back button, and you can easily link to a search result.

Lets look at simple page navigation, i.e. splitting 100 results into 5 pages of 20 results. Each page navigation link would look something like:

search.php?string=example&page=1

That's fine for basic queries, but what if the query was something like, search for all pages with the word example, with the tag "examples", or "demo" , posted by the user "John", between the dates of 01/01/07 and 01/06/07, with more than 3 comments. There could be far more options, but you get the idea.

search.php?sting=examples&tag=examples,demo&user=John&fromDate=01-01-07&toDate=01-06-07&comments>3&page=2

Quite a mouthfull! You're query string will be looking huge with all that data. Your "page" links will have to contain every piece of that data, and in general it's going to be very messy to work with, even for something as simple as copy-pasting your search to an associate or friend.

Some software gets around this issue quite cleverly, by generating a "search id", so your above "conditions" are stored temporarily in a database row, and your search page links would look something like:

search.php?searchID=19384&p=1

The added advantage of this, is that you can also cache the results of the search. Perhaps a certain keyword is searched thousands of times a day on your site - you could cache these results in the search row (which is a very efficient query), and save your server from more intensive fulltext searches.

Just remember to set a time limit on these searches, as you would need them to expire after a certain period of non-use so that they don't become stale.

Comments on this article:


I love feedback and comments, be the first!

Add Comment:


Make a Comment

*Nb, all comments are moderated to prevent spam or inappropriate content.








netforge logo
netforge provides high quality and friendly website design services to business. We're Australian based and reliable... (find out more).