ll87 Posted February 23, 2010 Report Share Posted February 23, 2010 I am using array_slice in PHP to hold 12 elements, as there are 12 images per page on my site. However, now that I have moved over to this method, I can no longer see whether or not there are any images remaining and therefore whether there should be a Next hyperlink. Before i was using readdir and the pointer was always on the next image so it was easy, but now that i am putting all images into one array in one go, i can no longer do it this way. any help? if($skip != 0){ $has_previous = true; } while(($file = readdir($handle)) != false){ //read in all files. $extension = strtolower(substr(strrchr($file, '.'), 1)); //Read string after the . and get substring from the . if($file != '.' || $file != '..'){ if($extension == 'jpg' || $extension == 'jpeg' || $extension == 'JPG'){ $all_images[] = $file; //add file to array. } } } sort($all_images); //sort array alphabetically. $photos = array_slice($all_images,$skip,$per_page); //contains 12 images for each page, in order. //code here to process images. basically i only need the last array slice and then i can get the last element and say if there are any images left, if not dont add a Next link. sample gallery at www.alexbarron.co.uk/galleries.php?trip=Leaving_do Quote Link to comment Share on other sites More sharing options...
krillz Posted February 23, 2010 Report Share Posted February 23, 2010 Hmm yeah although you can get array_slice to work as you want with help of some nifty mathematical expression to get it to slice well even at the end of the array. I think you might be better using a different function to cut the array into pieces of 12 that requires a lot less effort. check out the array_chunk() function instead, this will spare you of some variables that you are using right now. example of splitting an array into chunks of 12: $input = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"); $new = array_chunk($input, 12); to get the amount of pages you have, perhaps save it in a variable pages simply do: $pages = count($new); $new is a multidimensional array: $new[rows][cols] where in your case rows = page and cols = images So in my example we got two 2 pages: 1st has 12 elements and 2nd has the rest. So if you don't have an ammount that devides evenly with 12 then your last chunk ( the last row ) will not have 12 elements in it, something you need to pay attention to when you loop through to not end up outside its bounderies causing a out of bounds error. but that is easily done with some control statements. 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.
Note: Your post will require moderator approval before it will be visible.