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 Adapt Software Political Yard - Talk about politics!
Insane Visions
Insane Visions
Insane Visions - Navigation

Favorites

Get Firefox!

PHP

MySQL

Affiliates

Tutorials-Expert
Software Reviews

ApocalypticTomorrow

Tutorials Garden
AllConsoleGamers
TalkPHP
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 - 8:24 pm
News Div
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: 7,774
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

AdaptBB

OneCMS

Latest Tutorials

- Basic PHP Security
- Bot Detection with PHP
- PHP and Forms


Latest Blogs

- AdaptCMS 2.0 - January 31st
- AdaptCMS 2.0 - November 20th
- AdaptCMS 2.0 - July 27th


Poll of the Month

What do you think of OneCMS 2.6?

Other
No thanks
Meh, indifferent
I will check it out
Results



Latest Posts

- Tutorial: Relative Image URL's
- New OneCMS Site is Live!
- Security: Protecting from RFI Attacks


Testimonials

I tried various other Content Management Systems before settling on OneCMS. None came close to the ease of use and the amazing customer service! This was my first time dealing with a CMS so naturally I had many, many questions. Amazingly they were all answered the same day, most of the time my problems were fixed in minutes! OneCMS has many great features and many more planned for future releases and I personally can't wait! I'm glad I went with OneCMS for my site VideoGamesHardcore.com!

- Hector Cortez, Founder/Editor-In-Chief, VideoGamesHardcore.com


Powered by AdaptCMS
Insane Visions - Footer