[英]pagination of php glob page
我有一个页面使用glob显示文件夹内的图像。 事情是,我只希望每页显示20张图片。 我在网上发现的有关分页的教程与数据库有关,但是我没有在代码中使用数据库。
$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
$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加载所有文件,然后它就会过滤。
添加了简单的分页。
这是我用于相同目的的代码。
/****************************************************
To fetch total number of images for pagination
*****************************************************/
$folder_name=$row[page_addlink] ;
$dirname = "gallery/".$folder_name ;
$files = glob($dirname."/*.{jpg,gif,png,tiff,jpeg,bmp}", GLOB_BRACE);
$no_of_files=count($files);
/****************************************************
This is to get total number of records for pagination
*****************************************************/
$rows = $no_of_files;
$pagenum=$_GET[pagenum];
/*******************************************
By default page number is 1
*******************************************/
if (!(isset($pagenum)))
{
$pagenum = 1;
}
/****************************************************
No of rows to be visibles.Value is set in config file
*****************************************************/
$page_rows = $no_of_images_pagination ; /// defined in connection.php file
$last = ceil($rows/$page_rows);
if ($pagenum <= 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
?>
<div class="right_panel">
<div class="inner">
<div class="inner_bg">
</br>
<div align='center'>
<?php
if($rows <=$page_rows)
{
}
else
{
if($pagenum!=1)
{
echo "<a href='[URL of your page]?id=$folder_id&pagenum=1' style='text-decoration:none'> <<-First</a> ";
}
$previous = $pagenum-1;
if($pagenum!=1)
{
echo " <a href='[URL of your page]?id=$folder_id&pagenum=$previous' style='text-decoration:none'><-Previous</a> ";
}
//just a spacer
for($k=1;$k<=$last;$k++)
{
echo " <a href='[URL of your page]?id=$folder_id&pagenum=$k' style='text-decoration:none'>";
if($k==$pagenum)
{
echo "<b><u>".$k."</u></b>" ;
}
else
{
echo $k ;
}
echo "</a> ";
}
$next = $pagenum+1;
if($pagenum!=$last)
{
echo "<a href='[URL of your page]?id=$folder_id&pagenum=$next' style='text-decoration:none'>Next -></a> ";
}
echo " ";
if($pagenum!=$last)
{
echo " <a href='[URL of your page]?id=$folder_id&pagenum=$last' style='text-decoration:none'>Last ->></a> ";
}
}
?>
</div>
您可以根据您的CSS以及页面url和目录位置进行一些修改后使用此代码,它可以很好地运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.