I'm calling this basic php code to rotate 3 image links 'randomly' on page refresh.
Is there anyone who can see how to amend my code to simply rotate these links in 'order' rather than 'randomly'.
I struggle with PHP code, and all my editing so far has failed to get it right.
Any help or advice is very much appreciated.
TIA
<?php
$Ad[0] = '<a href="link1" id="link1"><img src="1.gif" width="468" height="60" border="0" id="link_1" /></a>';
$Ad[1] = '<a href="link2" id="link2"><img src="2.gif" width="468" height="60" border="0" id="link_2" /></a>';
$Ad[2] = '<a href="link3" id="link3"><img src="3.gif" width="468" height="60" border="0" id="link_3" /></a>';
$Weight[0]=1;
$Weight[1]=2;
$Weight[2]=3;
$Weight[3]=0;
$sum =0;
for($i=0;$i<count($Weight);$i++)
$sum+=$Weight[$i];
$ShowAd = rand(0, $sum - 1);
for($i=0;$i<count($Weight);$i++)
{
if($ShowAd<=$Weight[$i])
{
$ShowAd=$i;
break;
}
else
$ShowAd-=$Weight[$i];
}
echo $Ad[$ShowAd];
?>
Help with basic code
Moderator: General Moderators
because the script runs every time it is called you need a way to find out what the last ad to be sent was - easiest way i can think of is to read the last ad from a file and then update the file's contents to reflect the new latest ad.
the file doesn't need to be anything special just a text file with the number or id of the last ad served would do it...
the file doesn't need to be anything special just a text file with the number or id of the last ad served would do it...
try the rand function (randomly returns a number between 0 and 1)
http://us3.php.net/manual/en/function.rand.php
http://us3.php.net/manual/en/function.floor.php
here's an example... you have an array with 5 image locations to switch between randomly:
if you want sequential, then you either need a counter stored somewhere and each user accesses the counter and increases it by one per load, or you need a session, or faked one, to raise a counter each user gets on their own.
in the latter, since it looks like you are talking of an ad banner, i suggest using the ip via http_vars instead of an ctual session. this way you don't rely on cookies or making everything a form.
http://us3.php.net/manual/en/function.rand.php
http://us3.php.net/manual/en/function.floor.php
here's an example... you have an array with 5 image locations to switch between randomly:
Code: Select all
<?php
$images=array(img_loc_1, img_loc_2, img_loc_3, img_loc_4, img_loc_5);
$img_disp=$images[floor((rand()*5))];
echo '<img src="'.$img_disp.'"<!-- other image options here -->>';
?>phpin the latter, since it looks like you are talking of an ad banner, i suggest using the ip via http_vars instead of an ctual session. this way you don't rely on cookies or making everything a form.