douren1891 2016-08-30 12:49
浏览 66
已采纳

如何在PHP中显示文件夹中的next和prev导航?

I need to create a previous and next navigation in one folder using a structure like page 1 page 2 etc My current not-working code is the following:

<?php 
    $search_dir = "gallery";
    $images = glob("$search_dir/*.jpg");
    sort($images);
    $current = 2;
    $prev_img = $images[$current - 1];
    $next_img = $images[$current + 1];
    // Image selection and display:

    //display first image
    if (count($images) > 0) { // make sure at least one image exists
        $img = $images[0]; // first image 
        ?>
        <ul>
            <li><a href="<?php echo $prev_img; ?>">Previous</a></li>
            <li><a href="<?php echo $next_img; ?>">Next</a></li>
        </ul>
        <figure><img src="<?php echo $img; ?>" alt="<?php echo $img; ?>"></figure>
    <?php 
        } else {
      echo 'error';
    }
?>
  • 写回答

1条回答 默认 最新

  • doqdcks3948 2016-08-30 13:13
    关注

    I am bit in hurry please try this code i have made some correction please look into the code, try to understand.

    <?php 
        $search_dir = "gallery";
        $images = glob("$search_dir/*.jpg");
        sort($images);
        if(isset($_REQUEST['image_no']))
        {
            $image_no = $_REQUEST['image_no'];    
        }
        else
        {
            $image_no = 2;
        }
    
        $prev_img = ($image_no == 0)?(count($images)-1):($image_no -1);
        $next_img = ($image_no == count($images)-1)?0:($image_no + 1);
        // Image selection and display:
    
        //display first image
        if (count($images) > 0) { // make sure at least one image exists
            $img = $images[$image_no]; // first image 
            ?>
            <ul>
                <li><a href="gallery.php?image_no=<?php echo $prev_img; ?>">Previous</a></li>
                <li><a href="gallery.php?image_no=<?php echo $next_img; ?>">Next</a></li>
            </ul>
            <figure><img src="<?php echo $img; ?>" alt="<?php echo $img; ?>"></figure>
        <?php 
            } else {
          echo 'error';
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看