随机横幅更改,并且在完成循环时不显示第一张图像

时间:2011-05-11 06:18:03

标签: php

实际上我有10个横幅,我想在页面刷新时随机更改它。 横幅更改并完成其10个横幅的循环,然后重复它..

我正在使用此代码

$banners=$objCms->getbanners();
for($count=0;$count<count($banners);$count++)
{
    $image[$count]['path']= $banners[$count]['path'];
    $image[$count]['bid']= $banners[$count]['bid'];
    $image[$count]['bannerlink']=$banners[$count]['bannerlink'];
    $image[$count]['name']=$banners[$count]['banner_name'];
    $image[$count]['url']=$banners[$count]['bannerlink'];

}
$bannerAdTotals=count($image)-1;

if($bannerAdTotals>0)
{
   //mt_srand((double)microtime() * 1234567);
   $bannerPicked = mt_rand(0,$bannerAdTotals);

}
else
{
   $bannerPicked = 0;
}

?>

................... banner banner here .................

请帮帮我............

2 个答案:

答案 0 :(得分:1)

这是一个会话示例:

session_start();
// check which was the last image that was loaded
if (isset($_SESSION['currentImage'])) {
   $imgCounter = (int) $_SESSION['currentImage'] + 1;
}else {
   $imgCounter = $_SESSION['currentImage'] = 0;
}
if ($imgCounter > 10) {
  $imgCounter = $_SESSION['currentImage'] = 0;
}

$currentImage = "/images/image{$imgCounter}.jpg";

....

<img src="<?php echo $currentImage?>" alt="" />

现在你将每次都加载下一张图片,如果它达到10,它将重新开始。

您可能需要稍微调整一下以使其与您的代码匹配

**更新**

对于随机,您可以使用rand(0,9)并检查返回的值,将其用于图片并将其从等式中删除

答案 1 :(得分:0)

这应该可以解决问题:

session_start();
if (!isset($_SESSION['pick']) || count($_SESSION['pick']) == 0) {
   // reset available banners
   $_SESSION['pick'] = $objCms->getbanners();
}

// pick random key from available banners
$bannerPickedId = array_rand($_SESSION['pick']);
// get value
$bannerPicked = $_SESSION['pick'][$bannerPickedId];
// remove picked from available
$_SESSION['pick'] = array_slice($_SESSION['pick'], $bannerPickedId );