There are a number of ways we could go about setting up our PHP code to handle the task at hand. However, since the ideas we are discussing are not limited to the particular application we are developing (the computer hardware storefront), it would be helpful to develop code that is as re-usable as possible. In order to do that, we will first need to take a moment to think about what exactly it is that we need PHP to do.
As previously mentioned, our goal is to create code that is reusable, and a good first step for doing that is defining the proper code flow. Let’s go ahead and create the following files:
As you can see, each file has a specific task/function. The “index.php” file we have already discussed, so let’s now take a look at the “inc.conf.php” page. At this point all we will need to do within our conf file is open a database connection:
<?php # open MySQL connection mysql_connect( 'localhost', 'root', '' ); mysql_select_db( 'search_engine' ); ?>
This database connection will be used by PHP code found within other files (“helper.php” and “search.php” mainly), but instantiating it here helps keep things a little cleaner and easier to update, should our connection information change. Our “inc.core.php” file should also look pretty barebones at this point:
<?php # start session before any header information has been sent @ session_start();
# turn off error reporting on undefined indexes # NOTE: this was done to clean-up code and reduce 'isset' checks error_reporting( E_ALL ^ E_NOTICE );
# include the core configuration page include_once 'inc.conf.php';
# include the Search helper class include_once 'search.php'; ?>
That’s it for our configuration and includes files for the time being, although we will come back to them with some enhancements as the article series continues.
Summing things Up
We’ve now put all of the pieces into place, and we’re ready to start assembling our search interface using the helper Search class we’ll be creating (“search.php”). So far our page looks pretty empty, but we’ll fill it up quickly once things get started. Your “index.php” file should currently display the following: http://portfolio.boynamedbri.com/devshed/search/part_1/
You’re now ready to move on to part two, and begin working on the Search class.