Store Front Contact Us Projects Blog PHP Tutorials Community Forums Support Downloads Client Area Homepage Charlie Page - Home to the blog of myself! Dynasty Wizard - Free game dynasties, period. php cms reviews - find the right cms for you. 24 Quotes - Jack Bauer, Tony Almeida - Season 6 Bengals Chatter - The unofficial blog of the Cincinnati Bengals, recaping game by game along with opinion articles and community for Jungle fans to chat away. Political Yard - Talk about politics!
Insane Visions
Insane Visions
Insane Visions - Navigation

Favorites

Get Firefox!

PHP

MySQL

Affiliates


AllConsoleGamers
TalkPHP
Script Dungeon
XBOX 360 News
Hilarious Videos
Free Myspace Stuff

[ Apply for Affiliation ]



Scripts.com

MaxTutorial.com - Best photoshop, flash and php tutorials


Your Link Here
Your Link Here
Your Link Here

Acceptance Mark
Insane Visions - Top Navigation


Insane Visions - PHP Search Tutorial :: Premium PHP Scripts - AdaptCMS, AdaptBB, OneCMS PHP Search at Feb 04, 08 - 7:24 pm

Post to Digg Post to Facebook Post to Furl Post to Netscape Post to Newsvine Post to Reddit Post to Simpy Post to Spurl Post to StumbleUpon

Views: 2862
Type: PHP
Experience Level: Beginner

In a series of tutorials here at Insane Visions, we have shown some of the basics of PHP and continue it with how to enable visitors to search your site, using PHP.

We'll take you step by step on a fairly simple search feature. From the actual form, to the results and several things you can do to make your search better.

Search Form

<?php
if ($ _GET['search'] == "") {
?>
<form action="phpsearch.php" method="get">
<input type="text" name="search"><br>
<input type="submit" value="Search">
</form>
<?php
}
?>

For anyone familiar with HTML (I would hope you are!), this is just a simple form. We're setting the form as "get", meaning the form values will show up in the URL. We then have a textfield 'search', carrying the search term and submit button.

Results of Search

<?php
if ($ _GET['search']) {
searchentry htmlentities($ _GET['search']);

echo 
"<table width='500' cellpadding='5'>";

search mysql_query("
SELECT * FROM content_table WHERE title LIKE
'%"
.$ searchentry."%' ORDER BY `id` DESC LIMIT 25");
while($ 
mysql_fetch_array($ search)) {
echo 
"<tr><td>".$ r[title]."</td></tr>";
}

echo 
"</table>";
}
?>

Ok so the person has submitted the form with the keyword they want to search your website for. Now this is a basic example, but we do numerous things. First we make sure that the search field has data, if so we then show the results page. Next we clean out the search keyword variable to make sure there is no nasty code or anything that could result in an attempted hacking.

The first big part (after the table) is the mysql query. Now you can search specifically for a result that matches the keyword exactly, but it\'s not a good idea. We then have a mysql_fetch_array to get the data and then with the echo, show whatever field(s) you want.

Searching on Steroids

<?php
if ($ _GET['search']) {
searchentry htmlentities($ _GET['search']);

echo 
"<table width='500' cellpadding='5'>";

search mysql_query("
SELECT * FROM content_table WHERE title LIKE
'%"
.$ searchentry."%' ORDER BY `id` DESC LIMIT 25");
amount mysql_num_rows(mysql_query($ search));
if (
$amount == "1") {
header("location: http://www.website.com");
die;
}
while($ 
mysql_fetch_array($ search)) {
n1 = $ searchentry;
n2 "<font style='background-color: #FFFF00'>
"
.$ searchentry."</font>";
name str_replace($ n1, $ n2, $ r[title]);

echo 
"<tr><td>".$ name."</td>
<td>"
.date("D m Y"$r [date])."</td></tr>";
}

echo 
"</table>";
}
?>

Yeah, hopefully you aren\'t lost. If you compare this code to the other code, you will see a lot of the same, so I will focus on what\'s different. First, after the query, we check to see how many rows (another words, how many results) there are. If there is only one row, some may want to simply direct them to that result directly.

The other change was highlighting. If you search, this will highlight what you searched for. WIth simply the name, it\'s not 100% helpful, however if used with a field with more data (like a blog), it can help greatly. We simply are setting what we want highlighted and how to do it, the str_replace does it for us.

Conclusion

Another basic tutorial, I hope it can help people. The search is an underestimated part of a website. Simply if someone goes to your website, a big reason (or THE reason) is looking for something specific. A search makes it painless and fast for them to find the information they want. As with almost everything with PHP, there are endless addons to even a measly search. Happy coding!

Download:

Rating:

Vote for Article:



 email

 website







Username:
Password:
Insane Visions - Login Register

AdaptCMS

OneCMS

Latest Tutorials

- PHP and Forms
- Basic PHP File Handling
- PHP Search


Latest Blogs

- AdaptCMS - v1.2 Features
- Creating your first Website
- AdaptCMS - RSS Importer


Poll of the Month

How do you code in PHP?
Standard, all the time
I don't know PHP
It depends on the script
OOP, the one and only!
Results


Latest Posts

- Launch Upcoming
- Is this adapt BB?
- AdaptCMS Pro v1.1 -
Released


Testimonials

'I tried five different content management systems before I settled on OneCMS. OneCMS has proved itself to be very powerful, capable and easily customizable. The support has been fantastic; any little problems or questions I had were answered in one day. This software comes with my highest reccomendations.'

- George Lester, Founder/Webmaster, NintendoFocus.com



Powered by AdaptCMS Pro
Insane Visions - Footer
Loans - Guitar Lessons - Arizona Pools - Credit Counseling