This is a little PHP script I wrote to rotate ads, text snippets, images, web pages, links and anything else rotatable. In fact, I have two instances of it running in JournalXtra – one to display random ads in the sidebar and another to display random links in the sidebar. I’ve found it so incredibly useful that I thought I’d better write about it and share it.
Affectionately called Randy, the PHP script performs a simple task: it reads an external file, picks a few random lines from it and serves them to a web browser. It is intended to be used to randomly display text ads within web pages but can easily display any HTML content.
I’ve used it to display
- random hyperlinks
- random images in a gallery
- random tube clips
- random user reviews
- random ads
- random text snippets
- and more…
It is easy to use, just
- download the script (randy.php)
- upload it to your server
- unzip it
- create a text file called ads.txt
- put the content to be displayed by Randy into ads.txt
- call the script within your web page
The randy.php file and the ads.txt file should be stored in the same directory as the page that calls randy.php. For example, if the random ads, images or text snippets are to display in index.php then the randy.php and ads.txt files should be stored in the same directory as index.php.
Each item to be displayed must be on its own line within ads.txt. There must not be any empty lines and you can include any HTML formatting you like. Any HTML formatting must be within the same line as the item it affects. For example, if you have data for three ads in ads.txt and you want each item to be contained by a <div> box and for any text to display in bold then that must be stated in each line, viz
<div style="margin-left: auto; margin-right: auto; text-align: center;"><img src="http://some-place-one.com" /><br /><a href="http://some-place-one.com"><strong>Click Here</strong></a></div> <div style="margin-left: auto; margin-right: auto; text-align: center;"><img src="http://some-place-two.com" /><br /><a href="http://some-place-two.com"><strong>Click Here</strong></a></div> <div style="margin-left: auto; margin-right: auto; text-align: center;"><img src="http://some-place-three.com" /><br /><a href="http://some-place-three.com"><strong>Click Here</strong></a></div>
Additional CSS styling through element IDs and Classes are observed when the random items are rendered within a browser.
Call the script with either a PHP include:
or a Server Side Include:
The web page that calls the script should be an .shtml or .php file type (it will not work for a .html page).
The script comes with a few options, namely
- $title — the title to display with ads (optional)
- $file — the file that contains the ads or data to display randomly
- $pre — anything that should display before any random data is displayed (optional)
- $post — anything that should display after any random data is displayed (optional)
- $infix — anything that should display between each randomly displayed item (optional)
- $show — to maximum number of ads to display
- $per_row — ads, images, data may be displayed in columns. Use this to specify the number of items to display per row
I usually leave $title blank. Use a <br /> for the spacer ($infix) if you want each item displayed on a line of its own or use if you want each item displayed on the same line (in a row) but separated with a single space. Any symbol or HTML may be used as a spacer e.g <hr /> or |. Alternatively, specify the number of items to display per row and have Randy autogenerate a table to display the items in.
Randy also has two advanced settings:
- $companion — this is for specifying a file containing companion data to display alongside the main file data
- $infill — specifies the data to use when the companion file has fewer datalines than the primary file
- $dpath — this is for specifying a directory path to the datafiles when they are located in a different directory to the page that calls randy.php
The companion file is ideal for specifying textual information to display next to graphical ads. Think along the lines of the way Facebook ads display. To achieve this effect, place your graphical ads in the primary datafile ($file) and each graphical ad’s companion text in the secondary datafile ($companion); ensure both parts (graphical and textual) occupy an equal line number (as would be apparent in a non word-wrapped document). When called to display, the ad images will appear to the left of their text. To display the text first, put the text in $file and the graphics in $companion.
When the companion file contains fewer datalines than the primary file, the companion file is appended with enough lines of data to ensure both files have an equal number of datalines. The appended data will be either empty newlines else whatever is stated in $infill.
Randy can be used in WordPress by placing randy.php and ads.txt in the root directory of your WordPress installation (same place as index.php) and calling it with a PHP widget or PHP short tag. The ads served here at JournalXtra are served by Randy.