首页 / PHP / 使用glob的PHP分页来检索图像
使用glob的PHP分页来检索图像
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用glob的PHP分页来检索图像,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2129字,纯文字阅读大概需要4分钟。
内容图文
我正在尝试使用glob()从文件夹中检索图像,我希望将其分页,因此每页仅显示3张图像.
从在互联网上以及在S.O.我有下面的代码.问题在于它仅从目录中提取三个图像,当我单击下一页时,它会显示相同的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> ";
}
}
解决方法:
function getItemsFromPage($page, $numItems, $dir) {
$offset = ($page - 1) * $numItems;
$x = 0;
//get the first 3 items from the
foreach(glob($dir) as $image)
{
if($x < $offset)
{
$x++;
}
elseif($x < $numItems + $offset)
{
echo "<li><img src=\"$image\" alt=\"$image\" /></li>";
$x++;
}
else
{
break;
}
}
}
内容总结
以上是互联网集市为您收集整理的使用glob的PHP分页来检索图像全部内容,希望文章能够帮你解决使用glob的PHP分页来检索图像所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。