View Full Version : Selecting and using only a section of results from an array

12-08-2010, 04:32 PM
Hi there, I am currently pulling my hair out, as I have been trying to get my code working for the past 5 days and am getting nowhere fast. I have tried virtually every permutation of possibilities that I can think of, or have been advised of, and it's still not working.

Anyway, I was wondering if anyone here would have any insight into what I have done wrong. I am a PHP noob, so if you could actually explain to me any suggestions in the simplest way possible, it'll help me learn from my mistakes! :D

What I am trying to do is make a gallery.

I have several folders of pics in one main folder called 'gallery'.

I want the page to look inside the 'gallery' folder, see how many folders are in it, then use a pic called 'thumb.jpg' that is in each folder to display a thumb on the main gallery page in reverse order, so that the latest gallery is at the top.

I am numbering my galleries 001,002,003,004... etc in order to make the sorting easy.

So far so good... I can get it to do all this, however then comes the issue of pagination. I am aware that this is a minefield, and it's one I really don't think I can get my head around right now, so rather than confusing myself more with even more functions and code, all I want to do is write a second page that is exactly the same as the first page, apart from it will display the galleries from where the last page finished.

For example... I currently have 16 test folders in my main gallery folder (001,002, etc to....016).
I want the first 6 to display on the main page, then the next 6 to display on page 2, which I will hardlink to at the bottom of the main page.

I don't envisage there being more than the 2 pages just now, but if there are in the future, I can always just make a new page and hardlink it in the same way. I will look into pagination for more pages once it becomes necessary and I've got my head around this first!!! :D

Anyway... here's the code so far :

$images = "gallery/"; # Location of galleries
$cols = 2; # Number of columns to display
$start = 6; # First gallery to show on this page
$max = 6; # Maximum number of galleries to show

// the folder '$images' is opened, and each folder within it is counted. I am
// not 100% sure what is actually going on here, but I have copied this
// snippet from another page I have that works in the same way.
// If someone has a brief layman's explanation, I'd be grateful!

if ($handle = opendir($images)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$files[] = $file;

$colCtr = 0;

// the table is started and the files are sorted. for some reason,
// natcasesort() and natsort() both gave weird sort orders
// and rsort() puts them in normal order, so I'm using sort()

echo "<div><table><tr>";


$c = count($files) - (1 + $start);
// $c = counts the files in the array minus 1, in order to get the real value...
// then $start is subtracted to carry on where the last page left off.
// It doesn't though :(

for($i = $c; $i > ($c - $max); $i--)
// $i = $c, $i is greater than the array count minus $max,
// $i de-increments on each loop. This will give us a countdown for the
// number of galleries required and no more on a page than the value
// specified in $max.

if($colCtr %$cols == 0)
echo "</tr><tr>";
echo "<td><a href\"gallerylist.php?folder=" . $files[$i] . "\"><img src=\"" . $images . $files[$i] . "/thumb.jpg\" width=\"240px\" height=\"180px\" alt=\"" . $alt . "\" /></a></td>";
// echo'ing out the $file[$i] on the loop, will give us the last required number
// of files in the file array. there will be no more than $max on a page, but
// how do I get it starting from gallery number $start???


// close the table once the galleries have been displayed.
echo "</table></div>" . "\r\n";

I have tried using array_slice(), but I don't think I am doing it correctly, as it's displaying 3 galleries and 3 spaces rather than all 6 galleries...

I'm using sort() rather than rsort() as for some bloody reason it seems to be getting the files in reverse order to start with... not sure why :confused:

I have looked into range() but again, am not sure how to use it.

I have also tried ever permutation of having $start - or + in various parts of the code.

I have tried using sprintf() to make sure I'm not losing my leading '0's on the folder numbers...

I'm basically pulling my hair out now. Do you have a smiley for that? ;)

Anyway... any help GREATLY appreciated.

12-08-2010, 06:36 PM
The only real pagination issue here is that you still need to work linearly since you are scanning a directory. This isn't really a huge problem though since the collection is so small.
Lets start with your sort. The reason is a pain is simply because the numbers are considered strings. You may want to consider using a simple usort, this will probably work:

function sortRevNums($a, $b)
return strcmp($a, $b);

usort($files, 'sortRevNums');

Breaking the array up itself makes the most sense, this is what you will want to do. Since we now have a sorted $files array, try this:

$sliced = array_slice($files, $start, $max);

To actually pagination, $start would be a comparison between a provided page number and $max:

$start = isset($_GET['page']) ? (int)$_GET['page'] : 0;
$startAt *= $max;
if ($startAt > 0)
$startAt += 1;

So lets see, if $max is 3, and $start is 0, then $startAt = 0. If $max is 3 and $start is 2, $startAt = 6 + 1 since the first 6 went with the $start 0 and $start 1.
Yeah that looks about right there.

Oh yeah, then you iterated the $sliced array instead:

$iItems = count($sliced);
for ($i = 0; $i < $iItems; ++$i)

Or you can use a while or foreach or whatever you like for your loops.