从文件夹获取图像时的 PHP 分页


php pagination when images are got from a folder

我试图在显示文件夹中的图像时进行一些分页。但是,到目前为止,我无法达到结果。我接下来有按钮的代码,以前它工作得很好。显示图像也运行良好。但是每页 8 张图像的限制不起作用。

这是图像显示的代码:

<div class="container">
                <div class="row">
                    <?php
                    $folder = "Products/";
                    if(is_dir($folder)){
                        if($handle = opendir($folder)){
                            while(($file = readdir($handle)) != False){
                                if($file=='.' || $file==='..' ) continue;
                                echo '
                                       <div class="col-xs-6 col-sm-4 col-md-3">
                                            <div class="thumbnail see" href="#">
                                            <!-- para aadicionar titulo data-title= -->
                                                <a href="Products/'.$file.'" data-lightbox="gallery"><img class="img-responsive"
                                                src="Products/'.$file.'" alt="'.$file.'" style="min-height:50px;height:100px;"></a>
                                            </div>
                                       </div>';
                            }
                            closedir($handle);
                        }
                    }
                    ?>
                </div>
            </div>

这是分页的代码:

$selectIdImage = mysqli_query($dbc,"Select idImage from images ");
$count_num_rows = mysqli_num_rows($selectIdImage);
if(isset($_GET['page'])){
    $page = preg_replace("#[^0-9]#","",$_GET['page']);
}else{
    $page = 1;
}
$ProductPerPage = 8;
$lastPage = ceil($count_num_rows/$ProductPerPage);
if($page < 1 ){
    $page = 1;
}elseif($page > $lastPage){
    $page = $lastPage;
}
$pagination = '';
$limit = "LIMIT".($page -1).$ProductPerPage.",$ProductPerPage";
if($lastPage !=1){
    if($page != $lastPage){
        $next = $page + 1;
        $pagination.= '<li class="next"><a href="seeProducts.php?page='.$next.'">Next <span aria-hidden="true">&rarr;</span></a></li>';
    }
    if($page != 1){
        $prev = $page - 1;
        $pagination.= '<li class="previous"><a href="seeProducts.php?page='.$prev.'"><span aria-hidden="true">&larr;</span> Previous</a></li>';
    }
}
?>

所以我想要每页 8 张图片,但我做不到。我知道如果从数据库中提取该怎么做(只是设置一个限制),但是从一个我不知道该怎么做的文件夹中......

我认为你在限制上有一些错误:

$limit = "LIMIT ".(($page -1)*$ProductPerPage).",$ProductPerPage";
至于文件,

例如,您可以将所有文件收集到一个数组中:

                $folder = "Products/";
                $files = array();
                if(is_dir($folder)){
                    if($handle = opendir($folder)){
                        while(($file = readdir($handle)) != False){
                            if($file=='.' || $file==='..' ) continue;
                            $files[] = $file;
                        }
                        closedir($handle);
                    }
                }

注意:我认为文件将以随机顺序出现,而不是在ABC中,并且当您执行某些目录操作(例如添加新文件)时,顺序也可能发生变化,因此$files排序可能是明智的:

                sort($files);

并且只打印那些应该在请求的页面上:

                for ($i=($page-1)*$ProductPerPage;$i<count($files)&&$i<$page*$ProductPerPage; $i++) {
                            $file = $files[$i];
                            echo '
                                   <div class="col-xs-6 col-sm-4 col-md-3">
                                        <div class="thumbnail see" href="#">
                                            <a href="Products/'.$file.'" data-lightbox="gallery"><img class="img-responsive"
                                            src="Products/'.$file.'" alt="'.$file.'" style="min-height:50px;height:100px;"></a>
                                        </div>
                                   </div>';
                        }
                    }
                }