Switch(Change) between two images on hitting refresh
I have a webpage and in that I want to change between two images on refreshing. I don't want to use random function because it will only produce a random output like, I may get the first image correctly, but the second image might be show after 8 or 9开发者_如何学Go refresh attempts. So using random function is not possible.
You can store the current image been displayed in a session variable:
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='' />";
Sweet, simple and easy.
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']);
}
}
精彩评论