Hi, I've been using a simple method to rotate my adverts on my site, but want to upgrade it so I can display more than one advert without the risk of showing the same advert/image twice. Here's what I have been using: <?php $rnd = rand(1, 10); $filename = "ads" . $rnd; include ("basic/$filename.php"); ?> PHP: Where a file called ads(No.).php would contain the html/javascript etc. But if I repeated the code twice I would run the risk of showing the same advert again. Ideally I want to show 3 and would rather not use MySql. Cheers, Martyn
You could try this <?php $rnd = rand(1, 10); $track_array[]=$rnd; $counter=0; while ($counter <=3) { if (!in_array($rnd,$track_array) { include ("basic/$filename.php"); $counter++; $track_array[]=$rnd; } $rnd = rand(1, 10); } ?>
Use sessions to track which ads have been served to each client. Rob, i think he is talking about seperate page loads, so the counter wouldn't work.
Sorry missing a closing ) in the if make it this : if (!in_array($rnd,$track_array)) basically it will allow 3 unique files on 1 page load. but like matthew said separate page loads will require sessions.
Another simplistic approach is to use microtime() and get the modulus (a % b) with the number of ads.
Thanks Rob that solves the problem and looks good. I think I will leave separate page loads to the future. Cheers, Martyn