implementing next and back buttons for a slideshow

天涯浪子 提交于 2019-12-31 05:48:15

问题


I'm trying to make a php slideshow and I'm almost done I just need to implement the next and back buttons which I thought were going to be easy, but apparently you can't increment indexes in php?

$sql = "SELECT pic_url FROM pic_info";
$result = $conn->query($sql);
$count = 0;
$dir = "http://dev2.matrix.msu.edu/~matrix.training/Holmberg_Dane/";
$source = "gallery.php";

if ($result->num_rows > 0) {
// output data of each row
    $pic_array = array();
    while ($row = $result->fetch_assoc()) {

        $pic_array[$count] = $row['pic_url'];
        $count++;
    }

    $index = 1;
    echo "<img src= ' $dir$pic_array[$index]' />";

    echo "<a href= '$dir$pic_array[$index + 1]'>next</a>";
    echo "<a href= '$dir$pic_array[$index - 1]'>back</a>";
}



$conn->close();

?>

回答1:


Try this

<?php
echo '<img src="'.$dir.$pic_array[$index].'">
<a href="'.$dir.$pic_array[($index + 1)].'">next</a>
<a href="'.$dir.$pic_array[($index - 1)].'">back</a>';
?>



回答2:


I would suggest to place the url's in an array and loop over them.

 $urls = ['website/url/for/image/image.jpg', 'another/url/image.jpg'];

    foreach ($urls as $url) {
        echo 'a href="http://'.$url.'"> Click </a>';
    }

It is definitely more readable that way.



来源:https://stackoverflow.com/questions/37576603/implementing-next-and-back-buttons-for-a-slideshow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!