djkirstyjay Posted December 6, 2010 Share Posted December 6, 2010 Hi folks... Trying to get a bit of gallery code to work the way I want it to and would appreciate some feedback from more experienced folks... What it's doing, or is supposed to be doing, is taking a thumb called 'thumb.jpg' from each folder within my gallery folder and displaying it in reverse order on a page. I only want a maximum number of images to display on the page at any one time, so that if I end up uploading loads of folders in the future, it will only display a set amount of thumbs on one page. I want the thumbs displayed in reverse order, so that the newest appears first. Here's the code... and as far as I can see, it should work... and I'm sure I have had it working in the past (I've just come back to working on it after a while) however now, it's putting the thumbs in a random order. My folders are all in the gallery folder, and are named 001, 002, 003, 004, 005, 006, etc. I want them to display with 006 at the top and to de-increment, but only for the most recent 16 folders. <?php $images = "gallery/"; # Location of galleries $cols = 4; # Number of columns to display $max = 16; # Maximum number of galleries to show if ($handle = opendir($images)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $files[] = $file; } } closedir($handle); } $colCtr = 0; echo "<div id=\"updates\"><table><tr>"; $c = count($files) - 1; //$c = the number of files in the array, minus 1 = highest index in the array. 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. { if($colCtr %$cols == 0) echo "</tr><tr>"; echo "<td><img src=\"" . $images . $files[$i] . "/thumb.jpg\" width=\"240px\" height=\"180px\" alt=\"" . $alt . "\" /></td>"; //echo'ing out the $file[$i] on the loop, will give us the last required number of files in the file array. $colCtr++; } echo "</table></div>" . "\r\n"; ?> I also want to work out how to set a start number for the galleries, so that I can paginate them... so, once I have 36 galleries (for example) I can have the latest 16 on the first page, and the next 16 on the 2nd page... and the 4 that run over would drift off into obscurity until I get round to deleting them... maybe with a variable named $min... How would I do that...? Like this, or differently? : <?php $images = "gallery/"; # Location of galleries $cols = 4; # Number of columns to display $min = 16; # Minimum number of galleries to show $max = 32; # Maximum number of galleries to show if ($handle = opendir($images)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $files[] = $file; } } closedir($handle); } $colCtr = 0; echo "<div id=\"updates\"><table><tr>"; $c = count($files) - 1; //$c = the number of files in the array, minus 1 = highest index in the array. for($i = $c; ($i + $min) > ($c - $max); $i--) //$i = $c, $i plus $min 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. { if($colCtr %$cols == 0) echo "</tr><tr>"; echo "<td><img src=\"" . $images . $files[$i] . "/thumb.jpg\" width=\"240px\" height=\"180px\" alt=\"" . $alt . "\" /></td>"; //echo'ing out the $file[$i] on the loop, will give us the last required number of files in the file array. $colCtr++; } echo "</table></div>" . "\r\n"; ?> Any help greatly appreciated! Quote Link to comment Share on other sites More sharing options...
btherl Posted December 7, 2010 Share Posted December 7, 2010 readdir() doesn't guarantee to return the files in order. It probably worked in the past by luck only. You could read all the files in and sort them, or you could keep a list somewhere of the latest ones. As for pagination, it is always a bit messy, and there are many tutorials available. Quote Link to comment Share on other sites More sharing options...
djkirstyjay Posted December 7, 2010 Author Share Posted December 7, 2010 Thanks... I have managed to get it working using a sort($files); after $colCtr = 0; Dunno if that's the best way to go about it... just that it's working!!! With regards to pagination, I have no intention of looking into the messy bits right now. It's hard enough for me to get my head around this stuff! All that I was planning on doing was having maybe 2 or three pages, that i can manually write and link one to the next. The trouble I'm having is where to fit in my $min variable, so that the 2nd page displays a range of thumbs rather than the whole lot. So... at the moment I have : $min = 16; # First gallery to show $max = 32; # Maximum number of galleries to show echo "<div id=\"updates\"><table><tr>"; sort($files); $c = count($files) - 1; //$c = the number of files in the array, minus 1 = highest index in the array. 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. { if($colCtr %$cols == 0) echo "</tr><tr>"; echo "<td><img src=\"" . $images . $files[$i] . "/thumb.jpg\" width=\"240px\" height=\"180px\" alt=\"" . $alt . "\" /></td>"; //echo'ing out the $file[$i] on the loop, will give us the last required number of files in the file array. $colCtr++; } echo "</table></div>" . "\r\n"; I thought it would be $c = count($files) - ($min + 1); //$c = the number of files in the array, minus $min + 1 = the array ceiling. But all it returns is nothing... any hints? Quote Link to comment Share on other sites More sharing options...
btherl Posted December 7, 2010 Share Posted December 7, 2010 Well, that IS pagination Even if it's only two pages. Another way to do it is: $pageno = 2; # Show page #2 $files_per_page = 16; # Show 16 files on each page $page_files = array_slice($files, ($pageno - 1) * $files_per_page, $files_per_page); Then $page_files is an array containing just the files for the current page. The trickiest bit of pagination is generating the next/prev links, so if you are doing those manually then this should be all you need. Edit: Maybe you need to subtract one from the second argument to array_slice(). I don't remember. Quote Link to comment Share on other sites More sharing options...
hennzo Posted December 7, 2010 Share Posted December 7, 2010 try this: <html> <title>test</title> <body> <?php $images = "images/"; # Location of galleries $cols = 4; # Number of columns to display $max = 5; # Maximum number of galleries to show if ($handle = opendir($images)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $files[] = $file; } } closedir($handle); } natcasesort($files); $colCtr = 0; echo "<div id=\"updates\"><table><tr>"; $c = count($files); //$c = the number of files in the array, minus 1 = highest index in the array. $start = ($_GET['page'] > 1)? ($_GET['page'] - 1)*$max : 1; $end = (($c - $max) > 0)? ($c - $max)$max - ($c)); for($i = $c - $start; $i >= $end; $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. { if($colCtr %$cols == 0) echo "</tr><tr>"; echo "<td><img src=\"" . $images . $files[$i] . "/head_bg_form.gif\" width=\"240px\" height=\"180px\" alt=\"" . $alt . "\" /></td>"; //echo'ing out the $file[$i] on the loop, will give us the last required number of files in the file array. $colCtr++; } echo "</table></div>" . "\r\n"; //pagination echo '<table><tr>'; $nbpages = ceil(($c+1)/$max); $current_page = isset($_GET['page'])? $_GET['page'] : 1; for($page=1; $page <= $nbpages; $page++){ if($page == $current_page){ echo '<td><span>'.$page.'</span></td>'; }else{ echo '<td><a href="?page='.$page.'"><span>'.$page.'</span></a></td>'; } } echo '</tr></table>'; ?> </body> </html> It's better to use natcasesort() than sort(). Otherwise 002 will be greater than 012. Quote Link to comment Share on other sites More sharing options...
btherl Posted December 7, 2010 Share Posted December 7, 2010 It's better to use natcasesort() than sort(). Otherwise 002 will be greater than 012. That problem doesn't apply here. The problem is that "2" sorts above "12" using sort(), but the OP has added leading zeroes. Edit: Oops, the problem is that "a2" sorts above "a12". There must be some non-numeric stuff in the string to encounter the problem. Quote Link to comment Share on other sites More sharing options...
djkirstyjay Posted December 7, 2010 Author Share Posted December 7, 2010 Thanks for the input and hints... am trying to give it a go, but my server has decided to go offline, so am going to go and do some Xmas shopping and have a go later when it's back on. Sods law eh?! Quote Link to comment Share on other sites More sharing options...
djkirstyjay Posted December 7, 2010 Author Share Posted December 7, 2010 Right... managed to have a look now. It paginates, but page 2 has nothing on it, whereas it should as there are currently about 16 galleries there to test with and I have set 6 to a page. I am trying to get my head round what should be happening on page 2 in order to understand it. I have written the comments in the code below. Is what I think it correct actually so? $images = "gallery/"; # Location of galleries $cols = 2; # Number of columns to display $min = 6; # First gallery to show $max = 6; # Maximum number of galleries to show // the folder '$images is opened, and each folder within it is counted if ($handle = opendir($images)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $files[] = $file; } } closedir($handle); } $colCtr = 0; // the table is started and the files are sorted. for some reason, natcasesort() and natsort() both gave weird sort orders, so I've gone back to sort() echo "<div><table><tr>"; sort($files); $c = count($files) - 1; // $c = counts the files in the array minus 1, in order to get the real value 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. // I would have thought that it was here that I have to minus the $max value somewhere with this code for page 2, as it should reduce the number of files in the array by the $max value... or am I not understanding this correctly? { if($colCtr %$cols == 0) echo "</tr><tr>"; echo "<td><img src=\"" . $images . $files[$i] . "/thumb.jpg\" width=\"240px\" height=\"180px\" alt=\"" . $alt . "\" /></td>"; // echo'ing out the $file[$i] on the loop, will give us the last required number of files in the file array. $colCtr++; } echo "</table></div>" . "\r\n"; I can't for the life of me figure out how to start the array looping down $min files lower... so if I have 30 gallery files and page 1 shows galleries 030 to 021, and then then I want page 2 to start the loop 10 files lower and show galleries 020 to 011.... Where do I need to put my $min variable? :'( Arghhhhhh! I think I've tried every possible combination, but all it does is reduce the number of files in the array, rather than reducing the starting number to $min files less... Help! You definitely need a 'head banging against brick wall' smiley on here Quote Link to comment Share on other sites More sharing options...
djkirstyjay Posted December 7, 2010 Author Share Posted December 7, 2010 am I going in the right direction with this amendment to the earlier code? if($colCtr %$cols == 0) $files[$i] = $fileno; $filepath = $fileno - $min; echo "</tr><tr>"; echo "<td><img src=\"" . $images . $filepath . "/thumb.jpg\" width=\"240px\" height=\"180px\" alt=\"" . $alt . "\" /></td>"; // echo'ing out the $file[$i] on the loop, will give us the last required number of files in the file array. $colCtr++; Quote Link to comment Share on other sites More sharing options...
btherl Posted December 7, 2010 Share Posted December 7, 2010 For page 2 you should subtract items per page from $c before the for loop. That seems like the simplest way to me. But I still recommend using array_slice() If your array is in reverse order, you can also use array_reverse() first. The end result will be simpler to understand. Quote Link to comment Share on other sites More sharing options...
djkirstyjay Posted December 8, 2010 Author Share Posted December 8, 2010 so... how would I use that? I've just looked it up and i 'think' it would be $input = $files[$i]; $output = array_slice($input, $min, $max); Is that correct, and how exactly would it fit in to what I have at the moment? Quote Link to comment Share on other sites More sharing options...
btherl Posted December 8, 2010 Share Posted December 8, 2010 You would start with the page number and the number of galleries per page. $page = 1; $galleries_per_page = 6; Since you want to show the galleries in reverse order: $files = rsort($files); # rsort() instead of sort() Then to find the galleries for a page: $files_for_page = array_slice($files, ($page - 1) * $galleries_per_page, $galleries_per_page); For page 1, the array offset is (1 - 1) * 6 = 0, which is the $min for page 1 in your existing code For page 2, the array offset is (2 - 1) * 6 = 6, which is the $min for page 2 in your code. $max in your code is what I am calling $galleries_per_page. Quote Link to comment Share on other sites More sharing options...
djkirstyjay Posted December 8, 2010 Author Share Posted December 8, 2010 Thanks. Will have a look now and see what I can do... I shall post back if I have a prob. I REALLY want to get my head round this once and for all Quote Link to comment Share on other sites More sharing options...
djkirstyjay Posted December 8, 2010 Author Share Posted December 8, 2010 Right. This is driving me mad now. I have been changing things round for hours and just cannot figure out what is happening, as it is making no sense how the page changes each time. For a start, rsort is not reverse sorting my galleries. It's putting them in their normal order - 001, 002, 003, 004, etc Here is what I have now, just so you can see, as there must be something I'm missing... <?php $images = "gallery/"; # Location of galleries $cols = 4; # Number of columns to display $max = 6; # Maximum number of updates to show per page $page = 2; # Page Number if ($handle = opendir($images)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $files[] = $file; } } closedir($handle); } $colCtr = 0; echo "<div><table><tr>"; rsort($files); $files_for_page = array_slice($files, ($page - 1) * $max, $max); $c = count($files_for_page) - 1; //$c = the number of files in the array, minus 1 = highest index in the array. 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 updates required. { if($colCtr %$cols == 0) echo "</tr><tr>"; echo "<td><img src=\"" . $images . $files[$i] . "/thumb.jpg\" width=\"240px\" height=\"180px\" alt=\"" . $alt . "\" /></td>"; $colCtr++; } echo "</div>" . "\r\n"; ?> Also, I am only getting 3 galleries showing on the page, however the 'space' for the other 3 is actually there they just aren't showing up. the third thing is that when I use sort(), as rsort() is not reverse sorting, then the galleries start from 006 down to 001, rather than 009 to 003, as they should. :confused: Quote Link to comment Share on other sites More sharing options...
sasa Posted December 8, 2010 Share Posted December 8, 2010 try <?php $images = "gallery/"; # Location of galleries $cols = 4; # Number of columns to display $max = 6; # Maximum number of updates to show per page $page = 2; # Page Number, if you have 9 tumb, on 2nd page is just 3 if ($handle = opendir($images)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $files[] = $file; } } closedir($handle); } $colCtr = 0; echo "<div><table><tr>"; rsort($files); $files = array_chunk($files, $max); $files = $files[$page - 1]; foreach ($files as $file) //$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 updates required. { if($colCtr == 0) echo "<tr>"; echo "<td><img src=\"" . $images . $file . "/thumb.jpg\" width=\"240px\" height=\"180px\" alt=\"" . $alt . "\" /></td>"; $colCtr++; if ($colCtr == $cols){ echo '</tr>'; $colCtr = 0; } } if ($colCtr > 0) echo '</tr>'; echo "</table></div>" . "\r\n"; ?> Quote Link to comment Share on other sites More sharing options...
btherl Posted December 9, 2010 Share Posted December 9, 2010 <?php rsort($files); $files_for_page = array_slice($files, ($page - 1) * $max, $max); ... echo "<td><img src=\"" . $images . $files[$i] . "/thumb.jpg\" width=\"240px\" height=\"180px\" alt=\"" . $alt . "\" /></td>"; ?> Here you were using $files in the loop, instead of $files_per_page. The best way to debug a problem like "rsort() is not sorting" is to add this code: var_dump($files); # Show pre-sort data rsort($files); var_dump($files); # Show post-sort data exit(); And then stare at the output.. you would then see that it actually was sorting, and realize the bug is elsewhere. By repeating this process further down the code you can find the point at which the data changes from being what you expect to being not what you expect, and that's where the bug is. I would make this modification to sasa's code: $files_chunks = array_chunk($files, $max); $files_per_page = $files_chunks[$page - 1]; ... foreach ($files_per_page as $file) The behaviour is the same, but this makes it clear that array_chunk() produces chunks, and $files_per_page is one of those chunks. It's my habit to create a new variable for something which is a processed version of something else, so I don't get confused later on and think it's still the original. In any case he has fixed the bug in your latest version. Quote Link to comment Share on other sites More sharing options...
djkirstyjay Posted December 9, 2010 Author Share Posted December 9, 2010 Thanks to both of you for your help. I tried the code earlier, but it was still doing something strange, however that may have just been down to me being too tired, so I think it's best I look again tomorrow with fresh eyes I appreciate the help though and if I can't understand anything I'll reply again tomorrow. I don't just want a fix, I want to understand why and learn from it. Quote Link to comment Share on other sites More sharing options...
djkirstyjay Posted December 10, 2010 Author Share Posted December 10, 2010 ARGHHHHHHH! Just spent another day and a half trying to work out exactly WHY my images were not showing on page 2, only to stumble upon by accident that my co worker had deleted all the images out of the folders it was calling them from. So, I am now so tired with concentration, that I have to get some sleep... however I thought I'd better say that the pages are now working fine. Thanks to all and sundry. I will have another read through the code and ask questions if there's owt I don't understand tomorrow... however my brain is mush right now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.