问题描述
|
我有一个网页,我想在刷新时在两个图像之间切换。我不想使用随机函数,因为它只会产生一个随机输出,例如,我可能会正确获得第一张图像,但是第二张图像可能会在尝试8或9次刷新后显示。因此,不可能使用随机函数。
解决方法
您可以将当前显示的图像存储在会话变量中:
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=\'\' />\";
, 甜美,简单,轻松。
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\']);
}
}