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条)

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像