Saturday, August 25, 2012

Creating website content dynamically with PHP - MySQL


The Fresh website content for your visitors may be of real benefit when attempting to generate repeat traffic. Most webmasters, however, simply do not have enough spare time to frequently update or rebuild their pages manually. If your website hosting company provides free access to PHP and MySQL, this article will show you how to combine these two open source tools and replace a portion of the static content of your web sites with dynamic content changes often.

Why do you need dynamic content for your site?

Static pages on a website eventually become "old" and visitor traffic can fall significantly over time. The reduction in traffic can be attributed to these factors:

1) The reluctance of the search engines to include and display your potentially "out of date" pages in their search results,

2) The finite number of subjects other related sites that would be willing to link information about a specific topic, and

3) Visitors that learn to view your static website with a "been there, done that" attitude.

Creating and maintaining a website requires a significant investment in time and resources. Losing repeat visitors diminishes the value of your investment. Without repeat traffic it is virtually impossible for a website to be a continued success.

How can you add dynamic content without having to buy expensive software?

A proven (and easy to implement) to create dynamic content for your website is by rotating information on key web pages higher traffic using PHP with a MySQL database. Rotation content may take the form of a series of rotating articles, a rotating group of lists of products, or even a "thought for the day" simple. What is important is that your customers and search engines to find information visit new and interesting every time they visit your website.

As an example of creating dynamic content, build a system that rotates information about a product group on the main page of a hypothetical retail sales web site that sells widgets. Our goal is to present information on a different type or model of widget available for purchase whenever a consumer visits the shopping web site.

Step One: Create a table of contents to hold your widget data.

There are a couple of options for storing data to be displayed in your dynamic content rotation. The first option would be to create a new database, or perhaps they just add a table to an existing product database that will contain the information you want displayed.

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 a serial number and the second is a description field that will contain the product name and features. Note: You can add fields to your backgammon, including: an image URL field, shopping cart direct purchase URL field, product page field, etc.

1-b) Enter the sample data in your new table as follows:

INSERT INTO `content_table` VALUES (1, 'Plastic Widgets');

INSERT INTO `content_table` VALUES (2, Widget metal ');

INSERT INTO `content_table` VALUES (3, 'wooden widgets');

INSERT INTO `content_table` VALUES (4, of the rubber Widgets');

INSERT INTO `content_table` VALUES (5, 'Widget of stone');

After completing these two steps you will have a table compete with data to display on your website.

Another option would be to use your existing table of the product. If there are hundreds of different models and styles of widgets already in a database table, you can use the same structure we are now learning to connect directly to that table and display the data that already exist.

Step Two: Working with the new table:

To display the dynamic content to function there must be a mechanism in place that instructs your web page on which item should be shown to visitors. These mechanisms vary in complexity of the extremely simple controls for the more complicated use of cookies or IP tracking to determine which item should be displayed.

For this tutorial, we will use one of the most effective mechanisms and perhaps the easiest to integrate. This is the use of a generator of random numbers to decide which element 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 have five items so the maximum number of choices is 5. The reason why we need this number is to limit the random numbers being delivered. If we have five elements, we want the number generator, we only give a result between 1 and 5.

Now we must create a variable for our PHP code that will contain our new item number randomly generated as follows:

$ MyNumber = rand (1, 5);

This little snippet of code will act as a mechanism to "select" element of the product offered at random from five reported in the table of content we've created.

If we created 100 different items for dynamic display instead of five, she would simply change the "rand (1, 5)" the code to reflect the different maximum number. In this case would change in the "rand (1, 100)" so that the random number generator gives us a number somewhere between one and one hundred.

We are now ready to extract the information of the selected at random from your desktop so you can display on your site.

You can now connect to the database and query the table to find the data for the item that corresponds to the random number you created as follows:

Query_content $ = "SELECT * FROM WHERE item = $ content_table mynumber";

Step three: Displaying Data:

When you view the data is important to maintain consistency in presentation format. It 'better to create a table of specified dimensions (such as "width = 400") and display the results in this table. In this way the proportions of the page does not have to change with each new element (which can be very confusing for visitors).

Just display the results as if these where any other MySQL query using the echo command:

echo $ query_content ['products'];

Each time the page is loaded a different widget product will be randomly selected for display on that page.

What else can you do with dynamic content?

The only limits are your imagination. By adding a title and description meta tags to table of contents, you can toggle the title and description search engine for that page. You can also use this system to promote affiliate programs or sponsorship opportunities by rotating affiliate links and banners.

The proper use of dynamic content can bring your site is coming back into favor with search engines and encourage visitors to return often to see what is new....

No comments:

Post a Comment