PHP 分页使用 glob 检索图像
我正在尝试使用 glob() 从文件夹中检索图像,并且我希望对其进行分页,以便每页仅显示 3 个图像。
通过在互联网和此处进行挖掘,我得到了下面的代码。问题是它只从目录中提取三个图像,当我单击下一页时,它会显示相同的 3 个图像。
使用 glob 为每个页面获取不同图像的最佳方法是什么?
$imagesDir = 'uploadedImages/thumbs/*';
$itemsPerPage = 3;
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
$totalItems = getTotalItems($imagesDir);
$totalPages = ceil($totalItems / $itemsPerPage);
getItemsFromPage($currentPage, $itemsPerPage, $imagesDir);
getPager($totalPages, $currentPage);
//counts the number of files in the dir
function getTotalItems($imagesDir) {
$numImages = count(glob($imagesDir));
return $numImages;
}
//gets the files from the dir
function getItemsFromPage($page, $numItems, $dir) {
$x = 0;
//get the first 3 items from the
foreach(glob($dir) as $image)
{
if($x < $numItems)
{
echo "<li><img src=\"$image\" alt=\"$image\" /></li>";
$x++;
}
else
{
break;
}
}
}
//creates the pages and the links
function getPager($totalPages, $currentPage) {
// if we're not on the first page, show the previous page and first page links
if($currentPage > 1)
{
$prevPage = $currentPage - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevPage'><</a> ";
}
$range = 1;
// show what page we're on and the page numbers surround this page
for($x = ($currentPage - $range); $x < ($currentPage + $range) +1; $x++)
{
if(($x > 0) && ($x <= $totalPages))
{
if($x == $currentPage)
{
echo " [<b>$x</b>] ";
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?currentPage=$x'>$x</a> ";
}
}
}
// if we're not on the last page show the next page and last page links
if($currentPage != $totalPages)
{
$nextPage = $currentPage + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextPage'>></a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalPages'>>></a> ";
}
}
I'm trying to retrieve images from a folder using glob() and i want it to be paginated so it only displays 3 images per page.
From digging around on the internet and here on S.O. i've got the code below. The problem is that it only pulls three images from the directory and when i click on the next page it shows the same 3 images.
What would be the best way to fetch different images for each page using glob?
$imagesDir = 'uploadedImages/thumbs/*';
$itemsPerPage = 3;
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
$totalItems = getTotalItems($imagesDir);
$totalPages = ceil($totalItems / $itemsPerPage);
getItemsFromPage($currentPage, $itemsPerPage, $imagesDir);
getPager($totalPages, $currentPage);
//counts the number of files in the dir
function getTotalItems($imagesDir) {
$numImages = count(glob($imagesDir));
return $numImages;
}
//gets the files from the dir
function getItemsFromPage($page, $numItems, $dir) {
$x = 0;
//get the first 3 items from the
foreach(glob($dir) as $image)
{
if($x < $numItems)
{
echo "<li><img src=\"$image\" alt=\"$image\" /></li>";
$x++;
}
else
{
break;
}
}
}
//creates the pages and the links
function getPager($totalPages, $currentPage) {
// if we're not on the first page, show the previous page and first page links
if($currentPage > 1)
{
$prevPage = $currentPage - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevPage'><</a> ";
}
$range = 1;
// show what page we're on and the page numbers surround this page
for($x = ($currentPage - $range); $x < ($currentPage + $range) +1; $x++)
{
if(($x > 0) && ($x <= $totalPages))
{
if($x == $currentPage)
{
echo " [<b>$x</b>] ";
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?currentPage=$x'>$x</a> ";
}
}
}
// if we're not on the last page show the next page and last page links
if($currentPage != $totalPages)
{
$nextPage = $currentPage + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextPage'>></a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalPages'>>></a> ";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题出在 getItemsFromPage 函数中。
它始终从
$x = 0;
开始,而 $x 应该有所不同,具体取决于页码the problem is in the getItemsFromPage function.
It always starts from
$x = 0;
while $x should be vary, depends on the page number如果有人想要检索不带文件扩展名的图像文件名
use
pathinfo
- 返回有关文件路径的信息。if someone wants to retrieve the image filename without the file extension
use
pathinfo
- Returns information about a file path.