duanmei1850 2015-05-17 00:18
浏览 44
已采纳

PHP next()不工作

I'm trying to make a gallery using PHP. The images load properly, but the next and previous buttons don't seem to work. Clicking next on picture #1 brings you to picture #3 but clicking back on picture #3 bring you to picture #2 which is correct. How should I change my code to make it so both go in order?

    <?php
function listPicturesInDir($dir) {
    $dirname = "../pictures/photos/" . $dir . "/";
    $images = glob($dirname . "*.jpg");
    $previousPic = "null";
    foreach ($images as $image) {
        $next = next($images);
        $name = str_replace(".jpg", "", $image);
        $fp = strrpos($name, '/', 5) + 1;
        $name = substr($name, $fp, strlen($name));
        $id = str_replace(" ", "", $name);

        echo '<a href="#' . $id . '"><img class="galleryPics" src="' . $image . '" alt = "' . $name . '" title="'. $name.'"/></a>';
        echo '<div id="' . $id . '" class="modalDialog">';
        echo '<div>';
        if($previousPic !== "null"){
            echo'<a href="#'.$previousPic . '"><img src="../pictures/arrowLeft2.png" alt="Previous photograph" title= "Previous photograph" class="arrow"/></a> ';
        }

        if($next !== false){
            $name_next = str_replace(".jpg", "", $next);
            $fp_next = strrpos($name_next, '/', 5) + 1;
            $name_next2 = substr($name_next, $fp_next, strlen($name_next));
            $id_next = str_replace(" ", "", $name_next2);
            echo'<a href="#'.$id_next . '"><img src="../pictures/arrowRight2.png" alt="Next photograph" title="Next photograph" class="arrow"/></a>';
        }
        echo '<a href="#close" title="Close" class="close">X</a>';
        echo '<h2>' . $name . '</h2>';
        echo '<img class="modalImg" src="' . $image . '" alt = "' . $name . '"/>';
        echo '</div>';
        echo '';
        echo '</div>';
        //echo $next;
        $previousPic = $id;
    }
}
?>
  • 写回答

2条回答 默认 最新

  • doushi3715 2015-06-21 08:35
    关注

    The problem is that you are using next($images) within a foreach ($images ...) statement, thus modifying the internal array pointer. This may lead to unexpected behavior, as pointed out in the documentation on foreach:

    As foreach relies on the internal array pointer, changing it within the loop may lead to unexpected behavior.

    This illustrates your problem, using foreach and next:

    $images = array('one', 'two', 'three', 'four');
    
    foreach ($images as $image) {
        $next = next($images);
        echo "$image => $next", PHP_EOL;
    }
    

    Output:

    one => three
    two => four
    three => 
    four =>     
    

    One may think that just replacing the next() with current() would help, but alas:

    foreach ($images as $image) {
        $next = current($images);
        echo "$image => $next", PHP_EOL;
    }
    

    Output:

    one => two
    two => two
    three => two
    four => two
    

    According to a comment on the foreach documentation page, there used to be a notice on said page stating that:

    Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.

    Don't know why that was removed, but if we use a reference for $image then it actually works (note the &):

    foreach ($images as &$image) {
        $next = current($images);
        echo "$image => $next", PHP_EOL;
    }
    

    Output:

    one => two
    two => three
    three => four
    four => 
    

    But then maybe an old school for loop just makes more sense:

    for ($i = 0; $i < count($images); $i++) {
        $nextIndex = $i + 1;
        $next = ($nextIndex < count($images)) ? $images[$nextIndex] : null;
        $image = $images[$i];
        echo "$image => $next", PHP_EOL;
    }
    

    Output:

    one => two
    two => three
    three => four
    four => 
    

    Output from PHP 5.5.20.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等