我有以下代码,但遗憾的是它不起作用。我注意到未定义的索引:imagerotate。我无法解决这个问题。有谁可以帮助我吗?感谢。
<?php
$imgArray = array("header_1000x150_basnenaprani_3_o.png", "header_1000x150_basnenaprani_1_m.png", "header_1000x150_basnenaprani_1-zlatá.png");
// Path to where your images are stored
$hostDir = "http://myweb.com/wp-content/uploads/2015/10/"; // Don't forget that ending '/'
$imgArraySize = count($imgArray); // Store the size of the array (starts at 1)
$imgToShow = 0; // We'll default to always show the first image.
// Check to see if the cookie has already been created.
if ($_COOKIE['imagerotate'] != "") {
// The cookie existed
$imgToShow = $_COOKIE['imagerotate'];
if($imgToShow + 1 >= $imgArraySize) {
// The image we were supposed to show next would have been out of bounds in the array.
$imgToShow = 0; // We'll show the first image
}
else // The image to be shown is not out of bounds.
$imgToShow++; // Increment the image counter
}
// Write the new cookie with the new image we are showing
setcookie ("imagerotate", $imgToShow, time()+5);
//Print the image from the directory.
echo "<img src=\"".$hostDir.$imgArray[$imgToShow]."\" alt=\"An Image\" />";
?>
答案 0 :(得分:0)
试试这个:在if语句中使用“isset”(在phptester.net上测试):
<?php
$imgArray = array("header_1000x150_basnenaprani_3_o.png","header_1000x150_basnenaprani_1_m.png", "header_1000x150_basnenaprani_1-zlatá.png");
// Path to where your images are stored
$hostDir = "http://myweb.com/wp-content/uploads/2015/10/"; // Don't forget that ending '/'
$imgArraySize = count($imgArray); // Store the size of the array (starts at 1)
$imgToShow = 0; // We'll default to always show the first image.
// Check to see if the cookie has already been created.
if (isset($_COOKIE['imagerotate']) != "") {
// The cookie existed
$imgToShow = $_COOKIE['imagerotate'];
if($imgToShow + 1 >= $imgArraySize) {
// The image we were supposed to show next would have been out of bounds in the array.
$imgToShow = 0; // We'll show the first image
}
else // The image to be shown is not out of bounds.
$imgToShow++; // Increment the image counter
}
// Write the new cookie with the new image we are showing
setcookie ("imagerotate", $imgToShow, time()+5);
//Print the image from the directory.
echo '<img src="'.$hostDir.$imgArray[$imgToShow].'" alt="An Image" />';
另外,请更改此行:
echo "<img src=\"".$hostDir.$imgArray[$imgToShow]."\" alt=\"An Image\" />";
到此:
echo '<img src="'.$hostDir.$imgArray[$imgToShow].'" alt="An Image" />';