开发者

Random banner change and not displaying the first image whenever it complete the cycle

开发者 https://www.devze.com 2023-03-04 20:23 出处:网络
Actually i have 10 banner and i want to change it random whenpage refresh. Banner change and complete its cycle of 10 bannersbefore repeating it..

Actually i have 10 banner and i want to change it random when page refresh. Banner change and complete its cycle of 10 banners before repeating it..

I am using this code

$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 show here.................

Please help me............


here is the example with a session:

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="" />

Now you will have the next image loaded everytime and if it reaches 10 it will start over.

You might need to tweak it a little to make it match your code

** UPDATE **

For random you can use rand(0,9) and check the value that is returned use it for the picture and remove it from the equation


This should do the trick:

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

精彩评论

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