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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?