问题
Like a random image generator (ex: http://www.dustindiaz.com/a-simple-php-image-rotator/), only in a sequential order. Im running banner ads, and the client would like them run 1,2,3,4,5 in order per page load. So they just go round robin. 5 page loads, then the cycle start again.
Any ideas? I've googled for a while, and i'm not finding anything.
any help would be great, thanks much!
回答1:
<?php
// start session
session_start();
$ads = array ('Ad1', 'Ad2', 'Ad3', 'Ad4', 'Ad5');
// rotate
$id = ++$_SESSION['ad_id'] % count($ads);
$_SESSION['ad_id'] = $id;
// display ad
echo $ads[$id];
Of course you'd need the actual HTML code instead of Ad1
, Ad2
, etc.
If you don't want notices the following piece of code can be added after session_start()
if (!isset($_SESSION['ad_id'])) $_SESSION['ad_id'] = 0;
来源:https://stackoverflow.com/questions/3436854/print-each-item-of-array-in-order-one-per-page-refresh