How to Make a Product Catalog in PHP

By Max Power

The PHP scripting language allows a Web developer to create a website that shows site visitors an array of dynamic data sets. For example, PHP can be used to retrieve product data from a MySQL database and present that data to the Web in the form of an online product catalog. The developer may style the online catalog using Cascading Style Sheet rules. MySQL, PHP and CSS are all open source technologies and can be used by the developer without cost.

Step 1

Launch your phpAdmin utility and create a new MySQL table to host the product data. Name the table, "products", and place a field into the table for each item that you wish to display in the product catalog. For instance, carefully study the following example SQL command. You may enter this command into your phpAdmin utility's SQL command line. The command starts a new products table and creates an ID, description, upcNumber, name, price, picture and timestamp field. CREATE TABLE `yourDatabase`.`products (`id` INT( 100 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,`description` VARCHAR( 3000 ) NOT NULL ,`upcNumber VARCHAR( 100 ) NOT NULL ,`name` VARCHAR( 100 ) NOT NULL ,`price` VARCHAR( 100 ) NOT NULL ,`picture` VARCHAR( 100 ) NOT NULL ,`timestamp` TIMESTAMP( 200 ) NOT NULL DEFAULT CURRENT_TIMESTAMP) ENGINE = MYISAM

Step 2

Populate the new data fields with your product information. Place the information for each product in its corresponding field. You can do this my clicking on the "Insert" button from the phpAdmin utility. For example, you should place the product name into the product field, the product's UPC number into the upcNumber field and the filename of each product's picture into the picture field.

Step 3

Retrieve the data from the catalog using PHP. PHP retrieves each product's description from the database and displays the data where it may be viewed by your site's visitors. In this example code, PHP queries the database using the mysql_query function, places each product's data into an array, loops through the array and assigns each product data item into a corresponding variable.

Step 4

Print and style the PHP output with CSS rules. This example styles each product's image presentation to the site visitor using an inline CSS rule. The image has been floated to the left, the product name has been placed inside an H1 tag, the UPC number displays within an H4 tag, the price of the product will appear as an H5 heading and the product description will appear in a paragraph tag.

Step 5

Place the styled output within each product's array loop. This will display one occurrence for each product retrieved from the database. The completed PHP code now appears as follows.