问题
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;'>";
}
this is my code. how can i make it so that it displays 20 images per page and auto paginate? tq
回答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>';
}
}
But here the problem is that every time $files load all the files then it filters.
Added a simple pagination.
回答2:
This is code that i used for same purpose.
/****************************************************
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>
You can use this code after little bit modification according to your CSS and page url and directory location.It Works perfectly fine.Good luck
来源:https://stackoverflow.com/questions/14744194/pagination-of-php-glob-page