簡體   English   中英

php glob頁面的分頁

[英]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>&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和目錄位置進行一些修改后使用此代碼,它可以很好地運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM