Jump to content

simple image gallery help


murratw

Recommended Posts

The code below works well and shows all the images in a folder in reverse chronological order. But I want to limit it to a certain number of images .  This would enable me to have several pages of images.

 

So the first page would display the latest 50 images, the second page would show 50-100, and the third, 100-150.

 

Any helps on how to modify? I can create the pages manually. I just want to know how to limit the current page to a certain range.

<?php

//Your folder
$files = glob("images/*.*");

function sortnewestfilesfirst($a, $b) {
    return filemtime($b) - filemtime($a);
}
usort($files, "sortnewestfilesfirst");

$colCnt=0;
echo '<table border="0" style="width:1000px;">';

for ($i=0; $i<count($files); $i++)
  {
  $colCnt++;
  if ($colCnt==1)
  echo '<tr>';
  echo '<td width="20%" style="font-size:8.5px; font-family:arial">';

  $num = $files[$i];
   
echo ' 
  <div class="ImgBorder">
   <div class="clipout">
    <div class="clipin"> <a href="' . $num . '" rel="lightbox[all]"><img class="thumb ImgBorder" src="'.$num.'">  
</a> </div>
   </div></div>'."  ";


  echo '</td>';

  if ($colCnt==6)
    {
    echo '</tr>';
    $colCnt=0;
    }
  }

echo '</table>';
?>

Link to comment
Share on other sites

You could use array and $_GET from PHP to accomplish your goal

 

<?php
//how many image per page?
$img_per_page = 20;

//default page
$page = 1;
//sanitize GET var
if ((isset($_GET["page"])) && (is_int($_GET["page"]))) $page = $_GET["page"];

//Your folder
$files = glob("images/*.*");

//max page possible
$max_page = ceil(count($files) / $img_per_page);

if ($page > $max_page) {
echo "there is no page"; //or other warning
} else {

function sortnewestfilesfirst($a, $b) {
//the rest of your code.....

echo '</table>';

//add pagination
for ($x = 1; $x <= $max_page; $x++) {
echo "<a href='yourpage.php?page=$x'>[ $x ] </a>";
}

}

 

reference:

http://www.w3schools.com/php/php_get.asp

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.