Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Random Content: PHP Embedded

When a page is loaded, or reloaded, some of its content can be selected randomly with PHP.

The content might be:

  1. A saying with a random-colored border.

  2. Rotating ads to measure effectiveness.

  3. Letting each employee provide a picture to be randomly selected.

Pretty much anything that can be published as content on a web page can be randomly selected content. This includes videos, forms, and entire tables.

Reasons to provide randomly selected content can include:

  1. Maintain visitor interest when navigating pages of the website.

  2. Measure effectiveness of randomly selected content.

  3. Because the website owner wants to.

Actually, no amazing reason at all is required. If you want to test it, that is all the reason you need. It is actually quite easy to do.

There are various ways to publish randomly selected content. The method presented here may be the easiest of all.

This is how it works.

  1. A bit of PHP code selects a number. The number represents one of embedded content sections that are available to be selected.

  2. The content section matching the selected number is published and the other content sections are not.

That's it.

Overview Before Implementation

The web page source code has these sections.

PHP Selection Code
Content Section #1
Content Section #2
Content Section #3
Content Section #4
Etc.

Lets suppose the PHP selection code randomly selects the number 2. In that case, this is what the server sends to the browser.

Content Section #2

Implementation

To implement, let's first create some content to be randomly selected. For this example, the content will be bordered and each content section will have a different-colored border.

To reduce potential confusion — When the PHP code selects a random number it may select the same number as it did before. It's an entirely new random selection. No record is kept of previous selections.

Here is a live implementation of the code presented on this page.

Example content.

And here is the content source code.

<?php if($SelectedContent==1): ?>
<div style="border:3px solid blue; padding:1em;">Example content.</div>
<?php endif; ?>

<?php if($SelectedContent==2): ?>
<div style="border:3px solid red; padding:1em;">Example content.</div>
<?php endif; ?>

<?php if($SelectedContent==3): ?>
<div style="border:3px solid green; padding:1em;">Example content.</div>
<?php endif; ?>

<?php if($SelectedContent==4): ?>
<div style="border:3px solid orange; padding:1em;">Example content.</div>
<?php endif; ?>

The line of PHP code that begins the section contains a number to label the content. The numbers don't have to be sequential, but they must be inclusive. In other words, if there are 4 content sections, the numbers 1, 2, 3, and 4 must each be assigned a section. Although not required, having the sections sequential may help keep track of which numbers have been assigned.

The line of PHP code that ends a content section is the same for each.

When the page is loaded, PHP code (see below) selects a random number to determine which of the content to publish. Then that content is published and the rest is not.

Once the web page is published with the selected content, the source code for the content that was not selected will be unavailable to robots or the browser. Using the browser's "view source" functionality will not reveal the unselected text.

Here is the source code for the PHP that randomly selects a content section.

<?php
$Random_howManyContentSections = 4;
$SelectedContent = mt_rand(1,$Random_howManyContentSections);
?>

Change the number 4 in the above code to the number of content sections you have to code choose from.

Then, put the above code somewhere above the occurrence of the first content section.

Now, every time the page loads, the PHP selects a number from 1 to your specification of how many content sections there are to select from (in the example case, the number 4).

Once implemented, if you want to keep the content sections but turn off publishing any of them, in the PHP random selection code (the number 4 in the above source code) either specify the number 0 or a number larger than how many content selections are available.

This article first appeared with an issue of the Possibilities newsletter.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC