Saturday, 17 August 2013

PHP Conditional Processing Required to Display Rotating Banners Continuously

PHP Conditional Processing Required to Display Rotating Banners Continuously

My name is Kevin and I'm trying to do something a little different with a
banner rotator. Note that I am a PHP novice, all I know I've sourced via
the Internet and I have no formal training. So what might seem obvious to
you, might take me a little longer to comprehend.
Below is the script I am using to read 2 text files (stored on my root
directory with .db extensions) to rotate banners on a website. One file
holds a counter (FileDB), the other holds the HTML banner code (URLDB).
The URLDB file currently holds 6 lines of HTML code to display hyperlinked
banners. The following script builds an array and rotates these banners
sequentially on the refresh of the page counting from 0 - 5, and it does
this perfectly.
<?php
define('FILEDB', '/WORKING DIRECTORY/count.db');
define('URLDB', '/WORKING DIRECTORY/url.db');
function readURLS()
{
$fo = fopen(URLDB, 'r');
if( null == $fo )
return false;
$retval = array();
while (($line = fgets($fo)) !== false)
{
$retval[] = $line;
}
return $retval;
}
$list = readURLS();
if( false === $list )
{
echo "No URLs available";
}
else
{
$fo = fopen(FILEDB, 'a+');
$count = (fread($fo, filesize(FILEDB)) + 1) % count($list);
ftruncate($fo, 0);
fwrite($fo, "{$count}");
fclose($fo);
echo $list[$count];
}
?>
On the webpage that I want to display the banners I have 8 placeholders.
However I only have 6 banners. Here is the PHP code in each of the
placeholders...
Placeholder 1: <?php echo $list[$count];?>
Placeholder 2: <?php echo $list[$count +1];?>
Placeholder 3: <?php echo $list[$count +2];?>
Placeholder 4: <?php echo $list[$count +3];?>
Placeholder 5: <?php echo $list[$count +4];?>
Placeholder 6: <?php echo $list[$count +5];?>
Placeholder 7: <?php echo $list[$count +6];?>
Placeholder 8: <?php echo $list[$count +7];?>
With the count at 0, the 6 banners are displayed in placeholders 1 - 6 and
placeholders 7 & 8 are blank. With every refresh the counter increase by
1, showing each banner in the first placed holder and pulling the other
banners through each place holder from 5 through to 0, but leaving
previously populated placeholders blank until the 6th banner is in
placeholder one. Then on the next refresh banners 1 - 6 are once again
shown.
This occurs because I've hardcoded the values in each placeholder and am
obviously attempting to reference an entry in the file that is out of the
bounds of the array built by the above script.
You can see a working example here...
http://125ads.net/bannertest.php
What I am trying to achieve is display all banners in the URLDB such that
when the last entry is shown, the first entry is displayed in the next
placeholder (which in this case is placeholder 7) and the 2nd entry is
showin in placeholder 8.
The idea is that the banners move continuously through each of the
placeholders like the carriages of a train with each page refresh and
increment of the counter - one following the other.
So, now you have the background, on to my question...
Is there a way I can amend the script to store in a PHP variable the
maximum number of entries in the URLDB file/array and subsequently add
conditional processing in the placeholders to check when the counter
reaches this maximum value, and reference the next valid value in the
array (i.e. 0) such that the banners restart again in the surplus
placeholders - so here are no blank or empty placeholders shown?
I imagine this might seem like a strange request. But of course would
appreciate any advice on how I can achieve my goal based on where things
are currently.
Thanks,
Kevin

No comments:

Post a Comment