PHP水珠页的分页(pagination of php glob page)

2019-08-16 19:51发布

我有一个使用水珠显示的图像,是一个文件夹内的页面。 问题是,我想每个网页上显示只有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;'>";
}

这是我的代码。 我怎样才能让这个它每页和经销商PAGINATE显示20幅图像? TQ

Answer 1:

$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>';
   }
}

但这里的问题是,每次$文件加载的所有文件,然后将其过滤。

增加了一个简单的分页。



Answer 2:

这是代码,我用同样的目的。

/****************************************************
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>&nbsp;";

}   

$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和目录location.It完美的作品fine.Good运气点点修改后使用此代码



文章来源: pagination of php glob page