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.
PHP Search
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($ r = 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($ r = 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!
|