wmguk Posted March 17, 2008 Share Posted March 17, 2008 hey, I use some code to show images in a directory, on the fly, however if there are say 500 images in the directory, it just shows all 500 (obviously taking some time to show) so I was wondering can I adapt the code to just show 10 at a time, and have next/previous 10 link? this is above the page <?php $login = $_GET['login']; $NBFile=0; $dir ="clients/$login"; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $FileArray[] = $dir."/".$file; $NBFile=$NBFile+1; }}} closedir($handle); ?> On the page <?php $NBPicswidth=1; $NBPics=0; for ($i=0; $i<$NBFile; $i++ ) { $image=$FileArray[$i]; echo "<img src='$image'>"; $NBPics=$NBPics+1; if ( $NBPics==$NBPicswidth ) { $NBPics=0; } } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 17, 2008 Share Posted March 17, 2008 http://www.roscripts.com/PHP_pagination-70.html PHP pagination Quote Link to comment Share on other sites More sharing options...
wmguk Posted March 17, 2008 Author Share Posted March 17, 2008 http://www.roscripts.com/PHP_pagination-70.html PHP pagination Thanks for the linky I'm gonna have a look at it in a bit, it looks seriously advanced, i was hoping there would be a few lines of code I could add to the existing code i have, rather than a totally new script, ... Just having a glance at the pagnation code, it looks really really heavy.. is it just a case of pasting in to an existing page and it will self generate ? Quote Link to comment Share on other sites More sharing options...
wmguk Posted March 17, 2008 Author Share Posted March 17, 2008 also to use a pagination method call as a function with the total_pages and current page as the parameters e.g. $pagination = pagination_one($total_pages,$page); the only issue is, I wont know how many pages there are, so i wont be able to set this parameter? Quote Link to comment Share on other sites More sharing options...
Jeremysr Posted March 17, 2008 Share Posted March 17, 2008 There should be some way to find the number of pages. I think you find the number of images, divide by number of images per page, and round up. To get number of images, maybe use scandir() to get the directories and files in that directory, and loop through them counting the ones that end in a certain extension? Quote Link to comment Share on other sites More sharing options...
wmguk Posted March 17, 2008 Author Share Posted March 17, 2008 ok thats cool, thank you they are all .jpg and always will be so thats excellent thank you. if i can find out how many images are there then might be able to customise the original script or something... this will sound really dumb, but im only now getting really in to php, so please bear with me how do you use a function? would you create the function on an include page then call to it otherwise the php will be a nightmare? Quote Link to comment Share on other sites More sharing options...
wmguk Posted March 17, 2008 Author Share Posted March 17, 2008 ok, i used COUNT and SCANDIR to make a script, now, the amount of images are in a variable called $filecount... Quote Link to comment Share on other sites More sharing options...
wmguk Posted March 17, 2008 Author Share Posted March 17, 2008 I cannot find anyway to restrict the script to only display 10 or 20 images, ... I was thinking, now i have the counting variable, I can set the the next variable, etc etc.... can anyone help on this, as im in over my head i think? Quote Link to comment Share on other sites More sharing options...
Jeremysr Posted March 17, 2008 Share Posted March 17, 2008 You must have an array of image filenames to print, right? Just loop through them with a counter variable, incrementing the counter each time, and only print the image if the counter is within a certain range, which depends on what page you're on. $image_counter = 0; foreach ($image_array as $image_filename) { if ($image_counter >= ($page-1)*10 && $image_counter < ($page)*10) { echo '<img src="'.$image_filename.'" />'; } $image_counter++; } Edit: Actually in your original script to display images in the first post of this topic, the $i would work as a counter. So it'd look like this: <?php $NBPicswidth=1; $NBPics=0; for ($i=0; $i<$NBFile; $i++) { if ($i >= ($page-1)*10 && $i < ($page)*10) { $image=$FileArray[$i]; echo "<img src='$image'>"; $NBPics=$NBPics+1; if ($NBPics == $NBPicswidth) { $NBPics=0; } } } ?> 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.