Jump to content

Splitting a image gallery onto several "pages"


newbreed65

Recommended Posts

Hi everyone

 

I’m currently working on a Image gallery that in the future I can add to sites that need one but I’m currently having problem find a simple way of splitting the images up into group. For example if there were 50 images in my gallery it would be split up into pages of 24 with the remainders on the last page.

 

Can anyone help me?

 

This site is a good example of what I’m trying to do  http://www.comicartcommunity.com/gallery/search.php?show_result=1

 

and here the gallery at the moment and its relevant code http://www.andyhughes.co.uk/development/imagegallery/

<?php 

$images = "image_gallery/"; // Location of small versions 
$big    = "big/"; # Location of big versions (assumed to be a subdir of above) 
$cols   = 6; # Number of columns to display 

if ($handle = opendir($images)) { 
   while (false !== ($file = readdir($handle))) { 
       if ($file != "." && $file != ".." && $file != rtrim($big,"/")) { 
           $files[] = $file; 
       } 
   } 
   closedir($handle); 
} 

$filecount = count(glob("" . $images . "*.jpg"));

echo "<p>Images (" . $filecount . ")</p>";

if ($filecount == 0) {

echo "<h1>No Images Currently in the Gallery</h1>";

} else {
rsort($files);

$colCtr = 0; 

echo '<table width="100%" cellspacing="3"><tr>'; 




foreach($files as $file) 
{ 
  if($colCtr %$cols == 0) 
	echo '</tr><tr><td colspan="' . $cols . '"><hr /></td></tr><tr>'; 
  echo '<td align="center"><a href="index.php?image=' . $file . '"><img src="' . $images . $file . '" /></a></td>'; 
  $colCtr++; 
} 

echo '</table>' . "\r\n"; 
}


?> 

 

 

 

 

 

Link to comment
Share on other sites

Hi Andy,

 

The problem that we have with your script here is that it's not using MySQL. Most image galleries use MySQL because of it's ability to select only certain images based on a range. You'd usually in this case get MySQL to retrieve a page number, lets say 2 for example, now on page 2 we'd want images 25 - 28.

 

The code would look a bit like this (the maths is a slightly long version I can't remember the faster route at this moment in time):

<?php
$page = $_GET['page'];

// We get the number of the last image on the page -1 to take array indexes into consideration
$lastimg = ($page * 24) - 1;

// Then create a range (25 - 48)
$firstimg = $lastimg - 23;

// Once we have these two numbers, we'd normally do an sql query but this is a bit different

?>

 

Your case is a bit different, but it shouldn't be too hard, what we're going to do is change your foreach array slightly. The array you're building would look a bit like:

Array(
[0]=>"imgurl"
[1]=>"imgurl"
...
)

 

So what we can do is change the foreach using the code above:

foreach($files as $key => $file){
if($key >= $firstimg && $key <= $lastimg){
if($colCtr %$cols == 0) 
	echo '</tr><tr><td colspan="' . $cols . '"><hr /></td></tr><tr>'; 
  echo '<td align="center"><a href="index.php?image=' . $file . '"><img src="' . $images . $file . '" /></a></td>'; 
  $colCtr++; 
}
}

 

That should work though the numbers may need a few bits of tweaking. I haven't made a gallery in ages and never with a file array > SQL! Any problems just ask and I'll work through it with you.

Link to comment
Share on other sites

I actually build my gallery using a similar concept without storing the image paths in the database.

 

The idea is just read the files in the directory, the only difference is I added pagination so that it only shows x amount of images per page.

 

I did this by using a FOR LOOP where $thispage is the amount of images per page

 

for ($count=$start;$count<$thispage;$count++) {

 

I then have a page number listing with an HREF such as

 

<a href='display.php?name=$album&page=$count'><font size=+1><b>$count</b></font></a>

 

So you see the link states what $count to start on which could be image 1, image 10, image 20 etc etc and $thispage shows how many per page.  So if you have an array with 20 files and you loop through the array starting at any item you want specified by the variable in the path.

 

Hope that makes sense..

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.