HomePHP Page 4 - Creating a Searchable Inventory System: Setting Up Your Database and User Interface
How Do We Begin? - PHP
Businesses that hope to sell items through their website need a searchable inventory system for the web. This article, the first in a four-part series, will start you on your way to building one.
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.
Our first focus is on the search form itself. For our example application, that form will comprise three SELECT menus and a text input field. Creating the text input field is simple, but our PHP will need to retrieve, sort, and display a proper set of options for each of the SELECT menus. To make things even more interesting, two of those menus (“Sub Categories” and “Manufacturers”) will be dependent upon values found within the first (“Categories”), a feature that will require additional JavaScript and PHP code to function.
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:
helper.php // holds PHP & Javascript to dynamically update menus inc.conf.php // holds application configuration variables inc.core.php // includes all necessary files index.php // main layout page (HTML & CSS) search.php // search helper class w/ various search functions
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.