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

柔情痞子 提交于 2019-12-25 04:45:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!