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.
Video of the Day
CREATE TABLE yourDatabase
.products (
idINT( 100 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
descriptionVARCHAR( 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.
<?php mysql_connect("localhost", "yourUsername", "yourPassword") or die(mysql_error()); mysql_select_db("yourDatabaseName") or die(mysql_error());
$data = mysql_query("SELECT * FROM products ORDER BY id desc") or die(mysql_error('No Records Found'));
while($info = mysql_fetch_array( $data ))
{ $name = $info['name']; $description = $info['description']; $upcNumber = $info['upcNumber']; $imageFile = $info['picture']; $price = $info['price'];
} ?>
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.
<?php mysql_connect("localhost", "yourUsername", "yourPassword") or die(mysql_error()); mysql_select_db("yourDatabaseName") or die(mysql_error());
$data = mysql_query("SELECT * FROM products ORDER BY id desc") or die(mysql_error('No Records Found'));
while($info = mysql_fetch_array( $data ))
{ $name = $info['name']; $description = $info['description']; $upcNumber = $info['upcNumber']; $imageFile = $info['picture']; $price = $info['price']; ?>
Video of the Day