在此代码中,当我点击上一页时,它会像54321 - 54321 -54321
一样循环播放。但是当我点击下一个时,它会永远循环。
我希望它在数组完成后返回到数字1。我希望它像(next= 12345-12345-12345) and (prev=54321-54321-54321)
有办法吗?
<?php
$ar = array(
0 => "picture1.jpg",
1 => "picture2.jpg",
2 => "picture3.jpg",
3 => "picture4.jpg",
4 => "picture5.jpg",
5=> "kalle6.jpg",
);
if($_GET['start']==0) {
echo "<a href=index.php?start=". (count($ar)-1) ."> prev </a> ";
} else {
echo "<a href=index.php?start=". ($_GET['start'] -1) ."> prev </a>";
}
if($_GET['start']==0) {
echo "<a href=index.php?start=". (count($ar)+1) ."> next </a> ";
} else {
echo "<a href=index.php?start=". ($_GET['start'] +1) ."> next </a> ";
}
?>
答案 0 :(得分:0)
如果index($_GET['start']
)小于1,那么previous应该返回到最后一个数组元素(5)。如果它高于count($array) - 1
,它应该回到第一个(0)。
$array = array(
0 => "picture1.jpg",
1 => "picture2.jpg",
2 => "picture3.jpg",
3 => "picture4.jpg",
4 => "picture5.jpg",
5 => "kalle6.jpg",
);
$index = $_GET['start'];
if ($index > 0) {
echo '<a href="index.php?start=' . ($index - 1) . '"> prev </a> ';
} else {
echo '<a href="index.php?start=' . (count($array) - 1) . '"> prev </a> ';
}
if ($index < count($array) - 1) {
echo '<a href="index.php?start=' . ($index + 1) . '"> next </a> ';
} else {
echo '<a href="index.php?start=0"> next </a>';
}
答案 1 :(得分:0)
现在它看起来像这样,但是当我点击下一步时仍然没有像51234-45123-12345这样的循环。它停在第5位。
<?php
$ar = array(
0 => "picture1.jpg",
1 => "picture2.jpg",
2 => "picture3.jpg",
3 => "picture4.jpg",
4 => "picture5.jpg",
5=> "kalle6.jpg",
);
//PREV CLICK
if($_GET['start']==0) {
echo "<a href=index.php?start=". (count($ar)-1) ."> prev </a> ";
} else {
echo "<a href=index.php?start=". ($_GET['start'] -1) ."> prev </a>";
}
//SHOW IMAGE
$show_img = 3;
$num_img = 0;
for($i=$_GET['start']; $i<count($ar) && $num_img < $show_img; $i++) {
$num_img++;
echo "<img src=".$ar[$i]."/>\n";}
for($i=$num_img; $i<$show_img;$i++) {
echo "<img src=".$ar[$i-1]."/>\n";
}
// NEXT CLICK
if($_GET['start']<5) {
echo "<a href=index.php?start=". ($_GET['start']+1) ."> next </a> ";
}
?>