php glob页面的分页 [英] pagination of php glob page

查看:81
本文介绍了php glob页面的分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,该页面使用glob显示文件夹内的图像.事实是,我只希望每页显示20张图片.我在网上发现的有关分页的教程与数据库有关,但是我没有在代码中使用数据库.

i have a page that uses glob to display images that is inside a folder. The thing is, i want to display only 20 pics per page. The tutorials about pagination that i found online is related with database, but i did not use database inside my code.

$files = glob("uploaded_files/*.*");
usort($files, function ($a, $b) {
return filemtime($b) - filemtime($a);
});

foreach ($files as $file) {
echo "<img src='$file' style='height:180px;width:180px; border:2px solid black;  margin:20px 0px 10px  10px; *margin:10px 0px 10px 20px;'>";
}

这是我的代码.我怎样才能使它每页显示20张图像并自动分页? tq

this is my code. how can i make it so that it displays 20 images per page and auto paginate? tq

推荐答案

$files = glob("uploaded_files/*.*");
usort($files, function ($a, $b) {
return filemtime($b) - filemtime($a);
});

$record_count  = 20;
$totla_pages   = ceil(count($files)/$record_count);
$page          = $_REQUEST['page']; ///make it dyanamic :: page num
$offset        = ($page-1)*$record_count;
$files_filter  = array_slice($files, $offset,$record_count);

foreach ($files_filter as $file) {
echo "<img src='$file' style='height:180px;width:180px; border:2px solid black;  margin:20px 0px 10px  10px; *margin:10px 0px 10px 20px;'>";
}

if($totla_pages > 1){
   if($page != 1){
      echo '<a href="thispage.php?page='.($page-1).'">Prev</a>';
   }
   if($page != $totla_pages){
      echo '<a href="thispage.php?page='.($page+1).'">Next</a>';
   }
}

但是这里的问题是,每次$ files加载所有文件时,它都会过滤.

But here the problem is that every time $files load all the files then it filters.

添加了简单的分页.

这篇关于php glob页面的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆