点击刷新时切换(更改)两个图像之间的图像

时间:2011-05-30 05:29:57

标签: php

我有一个网页,我希望在刷新时更改两个图像。我不想使用随机函数,因为它只会产生一个随机输出,我可能会正确获得第一个图像,但第二个图像可能会在8次或9次刷新尝试后显示。所以使用随机功能是不可能的。

2 个答案:

答案 0 :(得分:4)

您可以将当前图像存储在会话变量中:

session_start();

$total_images = 10; // or whatever is the total number of images u have

if (!isset($_SESSION['current'])){
   $_SESSION['current'] = 1;
   $current = 1;
}else {
   $current = $_SESSION['current'];
   $_SESSION['current']++;
}

if ($_SESSION['current'] > $total_images) {
   $_SESSION['current'] = 1; // this way it will start over it reaches the end
}

$image = "image/path/name{$current}.jpg"; // name1.jpg, name2.jpg ... and so on;

// now echo the current image
echo "<img src={$image} alt='' />";

答案 1 :(得分:0)

甜美,简单,轻松。

session_start();
$swap = 8; // or 9, depends on you
if (!isset($_SESSION['count'])) $_SESSION['count'] = 1;
else {
     $_SESSION['count']++;
     if ($_SESSION['count'] < $swap) echo '<img src="image1.jpg" />';
     else {
        echo '<img src="image2.jpg" />';
        unset($_SESSION['count']);
     }
}
相关问题