Help with basic code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danc
Forum Newbie
Posts: 2
Joined: Mon Jul 07, 2003 7:51 am

Help with basic code

Post by danc »

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];
?>
User avatar
Unifex
Forum Newbie
Posts: 14
Joined: Mon Jul 07, 2003 12:29 am

Post by Unifex »

If you're using sessions or cookies you could just set a counter. Whenever it reaches the end of your list reset it.
ayron
Forum Newbie
Posts: 14
Joined: Tue Jun 03, 2003 11:18 pm
Location: Perth, Australia

Post by ayron »

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...
danc
Forum Newbie
Posts: 2
Joined: Mon Jul 07, 2003 7:51 am

Post by danc »

ayron wrote:because the script runs every time it is called you need a way to find out what the last ad to be sent was ...
You're right, i hadnt thought this through. Now im really stuck :?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

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:

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 -->>';

?>php
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.
Post Reply