开发者

print each item of array in order - one per page refresh

开发者 https://www.devze.com 2023-01-10 14:00 出处:网络
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

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开发者_StackOverflow社区 for a while, and i'm not finding anything.

any help would be great, thanks much!


<?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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号