Continued from page 1
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 tables in your database, you could utilize same structure we are learning now to connect directly to that table and display already existing data.
Step two: Working with your new table:
For this tutorial, we will utilize one of most effective mechanisms and perhaps easiest to incorporate. This is 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 total number of possible items that you want system to choose from. In this example we had five items so maximum number of choices will be 5. The reason we need this number is to limit random numbers being delivered. If we have five items, we want 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 mechanism to “select” a widget product item at random from five provided in content table that we created.
If we created 100 different items for your dynamic display instead of just five, you would simply change “rand (1, 5)” part of code to reflect different maximum number. In this case we would change it to “rand (1, 100)” so that random number generator gives us back a number somewhere between one and one hundred.
We are now ready to extract 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 data for item that matches 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 page proportions do not have to change with each new item (which can be very confusing for visitors).
Simply display results just as if these where any other MySQL query using echo command:
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 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, Texas and is an enterprise level PHP-MySQL programmer at the Datavor Web Directory and at the Shopping Elf directory Shopping Guide