Developing A Site Search Engine With PHP And MySQL
The key to developing a successful web site is content. Sure, your site can be packed with 10,000 50-page articles, but what good are these articles if your visitors can't find what they're after? In this article Mitchell shows us how to create a site search engine with PHP and MySQL. He develops the MySQL database and PHP script that will allow us to enter several keywords to search dynamic content.
Developing A Site Search Engine With PHP And MySQL - The searchdocs.php script (Page 4 of 7 )
Our PHP search script will be fairly simple, and will be used to demonstrate one way of retrieving records from the articles table based on selective keywords. Create a new script named searchdocs.php and save it in a directory that Apache can process. Enter the framework code for our search routine into searchdocs.php:
$submit = $_POST["submit"];
$keywords = $_POST["keywords"];
if(isset($submit) || isset($keywords))
Our search script starts out by assigning the values of two form variables collected from the $_POST associative array to local variables, $submit and $keywords. In PHP v4.1 and later, the $HTTP_POST_VARS associative array has been deprecated and it's recommended that you use the $_POST associative array instead (see this link for more info).
By default, the $submit variable will contain no data, and will thus be unset, so the getKeywords function will be called. Enter the following code into the getKeyWords function:
The getKeyWords function allows the user to enter keywords into a text box and looks like this:
When the search button is clicked, the name and value of the text box and submit button is passed back to our script. Recall earlier that the getKeyWords function is called when isset($keywords) is false.
This time round, $keywords (which is the value of the text box from PHP's $_POST associative array) contains some data, and so the doSearch function is called instead, passing $keywords in as its parameter.