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:
-
A saying with a random-colored border.
-
Rotating ads to measure effectiveness.
-
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:
-
Maintain visitor interest when navigating pages of the website.
-
Measure effectiveness of randomly selected content.
-
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.
-
A bit of PHP code selects a number. The number represents one of embedded content sections that are available to be selected.
-
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.
Lets suppose the PHP selection code randomly selects the number 2. In that case, this is what the server sends to the browser.
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.
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