dregvw1801 2014-06-16 06:43
浏览 70
已采纳

php:从文件夹中的图片制作数组并交替显示它们?

The following code is supposed to make an array from pictures found in a directory that end in .png using php, then allow buttons to change the pointer on the array and allow the page to display the current picture that the pointer is on. This doesnt seem to be working at all. Am I doing this correctly?

<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
img {float:left; }
</style>
</head>
<body>
<?PHP
$pages = array ();
$dirname = "assets/pictures/";
$images = glob($dirname."*.png");
foreach($images as $image) {
$pages[] = $image;
}
?>
<?PHP
echo '<img src="'.current($pages).'" class="photo"/>';
function shownext() {
$mode = next($pages);
}
function showprev() {
$mode = prev($pages);
}
function showfirst() {
$mode = reset($pages);
}
function showlast() {
$mode = end($pages);
}
?>
<a href="" onclick="showfirst()">first</a>
<a href="" onclick="showprev()">previous</a>
<a href="" onclick="shownext()">next</a>
<a href="" onclick="showlast()">last</a>
</body>
</html>
  • 写回答

4条回答 默认 最新

  • dqj29136 2014-06-16 07:12
    关注

    You cant directly put your php functions on onclick="" events. Alternatively, if you want to use jQuery, you could use $.ajax to request the values on PHP. From there, after you got the image paths, manipulate the next, prev, first, last on the client side. Consider this example:

    <?php
    
    if(isset($_POST['getimages'])) {
        $dirname = "assets/pictures/";
        $images = glob($dirname."*.png");
        // collect the images
        foreach($images as $image) {
            $pages[] = $image;
        }
        echo json_encode($pages);
        exit;
    }
    
    ?>
    
    <img src="" alt="" id="images" width="200" height="200" />
    <br/>
    <a href="#" class="navigate" id="first">First</a>
    <a href="#" class="navigate" id="previous">Previous</a>
    <a href="#" class="navigate" id="next">Next</a>
    <a href="#" class="navigate" id="last">Last</a>
    
    
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    
        var current_pointer = 0;
        var images = [];
    
        $.ajax({
            url: 'index.php', // call the php file that will process it, i just used this in the same page
            type: 'POST',
            dataType: 'JSON',
            data: {getimages: true},
            success: function(response) {
                // after a successful response from PHP
                // use that data and create your own array in javascript
                images = response;
                $('#images').attr('src', images[current_pointer]);
    
            }
        });
    
        // simple pointer to navigate the array you got from PHP
        $('a.navigate').on('click', function(){
    
            var current_val = $(this).attr('id');
            switch(current_val) {
                case 'first':
                    current_pointer = 0;
                break;
                case 'last':
                    current_pointer = images.length-1;
                break;
                case 'next':
                    current_pointer = (current_pointer >= images.length-1) ? images.length-1 : current_pointer+1;
                break;
                case 'previous':
                    current_pointer = (current_pointer < 0) ? 0 : current_pointer-1;
                break;
            }
    
            $('#images').attr('src', images[current_pointer]);
        });
    
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路