Creating Dynamic Website Content with PHP - MySQL
Article source: http://www.devnewz.com/. Used with author's permission.
Fresh website content for your visitors can be of real benefit when attempting to generate repeat traffic. Most webmasters, however, just don't have enough spare time to frequently update or rebuild their pages manually. If your web site hosting company provides free access to PHP and MySQL, this article will show you how to combine those two open source tools and replace a portion of your websites' static content with frequently changing dynamic content.
Why do you need dynamic content for your website?
Static pages on a website eventually become "stale" and visitor traffic can fall significantly over time. The drop in traffic can be attributed to these primary factors:
1) The reluctance of search engines to include and display your potentially "out of date" pages in their search results,
2) The finite number of other subject related websites that would be willing to link to your information on a specific topic, and
3) Visitors that learn to view your static website with a "been there, done that" attitude.
Creating and maintaining a web site requires a significant investment in time and resources. Loosing repeat visitors diminishes the value of your investment. Without repeat traffic it is virtually impossible for a website to be a continuing success.
How can you add dynamic content without having to purchase expensive software?
One proven (and easy to implement) method of creating dynamic content for your website is by rotating information on key, higher traffic web pages using PHP with a MySQL database. Rotating content can take the form of a series of rotating articles, a rotating group of product listings, or even a simple "thought for the day". What is important is that your clients and visiting search engines find new and interesting information each time they visit your website.
As an example of dynamic content creation, we will build a system that rotates information about a group of products on the main page of a hypothetical retail sales web site that markets widgets. Our goal is to present information about a different type or model of widget available for purchase whenever a consumer visits the shopping web site.
Step One: Create a content table to hold your widget data.
There are a couple of options for storing the data to be displayed in your dynamic content rotation. The first option would be to create a new database, or perhaps simply add a table in an existing product database that will hold the information that you wish to display.
Let's take five theoretical widget products and design a table as follows:
| item | product |
| 1 | Plastic Widgets |
| 2 | Metal Widgets |
| 3 | Wooden Widgets |
| 4 | Rubber Widgets |
| 5 | Stone Widgets |
1-a) Create your table with the following SQL statement:
CREATE TABLE `content_table` (
`item` int(4) NOT NULL auto_increment,
`product` varchar(10) NOT NULL default '',
KEY `item` (`item`)
) TYPE=MyISAM AUTO_INCREMENT=6 ;
This table contains two fields. The first is an item number and the second is a description field that will hold the product name and features. Note: You can add fields to your actual table including: an image URL field, shopping cart direct purchase URL field, product page field, etc.
1-b) Insert the example data into your new table as follows:
INSERT INTO `content_table ` VALUES (1, ' Plastic Widgets');
INSERT INTO `content_table ` VALUES (2, ' Metal Widgets');
INSERT INTO `content_table ` VALUES (3, ' Wooden Widgets');
INSERT INTO `content_table ` VALUES (4, ' Rubber Widgets');
INSERT INTO `content_table ` VALUES (5, ' Stone Widgets');
Once you have completed these two steps you will have a table compete with data to be shown on your website.
Another option would be to utilize your existing product table. If there are hundreds of different models and styles of widgets already in one of the tables in your database, you could utilize the same structure we are learning now to connect directly to that table and display the already existing data.
Step two: Working with your new table:
For this tutorial, we will utilize one of the most effective mechanisms and perhaps the easiest to incorporate. This is the use of a random number generator for deciding which item will be shown.
To create a random number generator using PHP you must first calculate the total number of possible items that you want the system to choose from. In this example we had five items so the maximum number of choices will be 5. The reason we need this number is to limit the random numbers being delivered. If we have five items, we want the number generator to only give us a result of between 1 and 5.
We must now create a variable for our PHP code that will hold our new randomly generated item number as follows:
$mynumber = rand(1, 5);
This little snippet of code will act as the mechanism to "select" a widget product item at random from the five provided in the content table that we created.
If we created 100 different items for your dynamic display instead of just five, you would simply change the "rand (1, 5)" part of the code to reflect the different maximum number. In this case we would change it to "rand (1, 100)" so that the random number generator gives us back a number somewhere between one and one hundred.
We are now ready to extract the randomly selected item's information from your table so that it can be displayed on your webpage.
You can now connect to your database and query your table to find the data for the item that matches the random number you created, as follows:
$query_content = "SELECT * FROM content_table WHERE item = $mynumber ";
Step three: Displaying your data:
When displaying your data it is important to maintain consistency in presentation size. It is preferable to create a table of specified dimensions (such as "width=400") and display your results within this table. In this way the page proportions do not have to change with each new item (which can be very confusing for visitors).
Simply display the results just as if these where any other MySQL query using the echo command:
echo $query_content [' product '];
Every time your page is loaded a different widget product will be selected at random for display on that page.
What else can you do with your dynamic content?
The only limits are within your imagination. By adding a title and meta description tags to your content table, you can alternate the title and search engine description for that page. You can also utilize this system to promote affiliate programs or sponsorship opportunities by rotating affiliate links and banners.
The proper use of dynamic content can bring your website back into favor with search engines and encourage your visitors to return frequently to see what is new. Don Beavers lives in Bryan/College Station, Texas and is an enterprise level PHP-MySQL programmer at both the Shopping Elf Shopping Guide, and the Datavor Web Directory.