paparanch Posted February 28, 2009 Share Posted February 28, 2009 hello gurus! i have this code which display all images from a folder, but i wanted to have pagination for this like 4 images per page... so how am i going to do this? please help... <?php $handle = opendir('comments/'); if($handle) { while(false !== ($file = readdir($handle))) { if(preg_match("/\w+(.gif)/",$file)) { echo "<div class='update_pics_frame'>"; echo "<div class='update_pics'>"; echo "<a href='comments/$file'><img src='comments/$file' width='200'border='0'></a>"; echo "</div>"; echo "</div>"; } } closedir($handle); } ?> thnx in advanced.. Quote Link to comment Share on other sites More sharing options...
RussellReal Posted February 28, 2009 Share Posted February 28, 2009 <?php $page = $_GET['page']; $rpp = 4; $c = 0; $handle = opendir('comments/'); if($handle) { while(false !== ($file = readdir($handle))) { if(preg_match("/\w+(.gif)/",$file)) { $c++; if ($c > (($page * $rpp) + $rpp)) break; if ($c <= ($page * $rpp)) { echo "<div class='update_pics_frame'>"; echo "<div class='update_pics'>"; echo "<a href='comments/$file'><img src='comments/$file' width='200'border='0'></a>"; echo "</div>"; echo "</div>"; } } Quote Link to comment Share on other sites More sharing options...
paparanch Posted February 28, 2009 Author Share Posted February 28, 2009 thnx mister but its not working for me...i dont know where i get wrong...but it just display blank page... Quote Link to comment Share on other sites More sharing options...
RussellReal Posted February 28, 2009 Share Posted February 28, 2009 oh sorry bro it should be greater than instead of less than for the second if.. <?php $page = $_GET['page']; $rpp = 4; $c = 0; $handle = opendir('comments/'); if($handle) { while(false !== ($file = readdir($handle))) { if(preg_match("/\w+(.gif)/",$file)) { $c++; if ($c > (($page * $rpp) + $rpp)) break; if ($c >= ($page * $rpp)) { echo "<div class='update_pics_frame'>"; echo "<div class='update_pics'>"; echo "<a href='comments/$file'><img src='comments/$file' width='200'border='0'></a>"; echo "</div>"; echo "</div>"; } } ?> Quote Link to comment Share on other sites More sharing options...
paparanch Posted February 28, 2009 Author Share Posted February 28, 2009 wow! it works! but now, how am i going to configure the link to next page and previous? or prev 1 2 3 4 next something like this... Quote Link to comment Share on other sites More sharing options...
paparanch Posted February 28, 2009 Author Share Posted February 28, 2009 hellow! any help please? Quote Link to comment Share on other sites More sharing options...
RussellReal Posted February 28, 2009 Share Posted February 28, 2009 page=1 page=2 page=3 Quote Link to comment Share on other sites More sharing options...
paparanch Posted February 28, 2009 Author Share Posted February 28, 2009 would you please explain more? T_T Quote Link to comment Share on other sites More sharing options...
RussellReal Posted February 28, 2009 Share Posted February 28, 2009 well.. in the script I altered for you.. I expect a GET variable called 'page' so you'd just do whatever.php?page=2 3 4 etc Quote Link to comment Share on other sites More sharing options...
timecatcher Posted February 28, 2009 Share Posted February 28, 2009 I think although im still a newby at the myself you can set up some kind of while statement so it checks what the most amount of pages is and automatically echos out the links to those pages, but Im busy so can't show you right now but I might later if you still need help. TC. Quote Link to comment Share on other sites More sharing options...
paparanch Posted March 1, 2009 Author Share Posted March 1, 2009 yes of corz! i still need help more help which is better way for this. tnx Quote Link to comment Share on other sites More sharing options...
paparanch Posted March 1, 2009 Author Share Posted March 1, 2009 elow mr.russel...i tried to put a link <a href='view_pix.php?page=2'>next</a> below the page...but only two comments were shown in the next page....instead of another 4 images and last image in the last page... Quote Link to comment Share on other sites More sharing options...
RussellReal Posted March 1, 2009 Share Posted March 1, 2009 you get the number of pages by doing this 1 math operation: $numOfPages = ceil( TOTAL_IMAGES / MAX_IMAGES_PER_PAGE ); Quote Link to comment Share on other sites More sharing options...
paparanch Posted March 1, 2009 Author Share Posted March 1, 2009 Warning: Division by zero in D:\Personal Files\Paparanch\xampp\htdocs\emo\view_pix.php on line 24 this is what i got from that code mr...i don't know why Quote Link to comment Share on other sites More sharing options...
paparanch Posted March 1, 2009 Author Share Posted March 1, 2009 oww..i see...so the problem now is how to get the total number of images in a folder.... Quote Link to comment Share on other sites More sharing options...
paparanch Posted March 1, 2009 Author Share Posted March 1, 2009 mr.russel....im really messing around here...i really don't know what to do.... Quote Link to comment Share on other sites More sharing options...
RussellReal Posted March 1, 2009 Share Posted March 1, 2009 scandir Quote Link to comment Share on other sites More sharing options...
paparanch Posted March 2, 2009 Author Share Posted March 2, 2009 good morning mister russel! here's what i got so far...the problem is when i declare value "3" in the $rpp variable or row per page...it will display 4 images instead of 3...and i also noticed that the last image in every page is still present on the second page(and it was placed first)...i'm sure i got some mistake with my code but i just can't figure it out....would you please help... im getting frustrated with this...T_T <?php $page = $_GET['page']; $rpp = 3; $c = 0; $handle = opendir('comments/'); $d = opendir("comments/"); $count = 0; while(($f = readdir($d)) !== false) if(ereg('.gif$', $f)) ++$count; closedir($d); print "$count"; $numOfPages = ceil( $count / $rpp ); if($handle) { while(false !== ($file = readdir($handle))) { if(preg_match("/\w+(.gif)/",$file)) { $c++; if ($c > (($page * $rpp) + $rpp)) break; if ($c >= ($page * $rpp)) { echo "<div class='update_pics_frame'>"; echo "<div class='update_pics'>"; echo "<a href='comments/$file'><img src='comments/$file' width='200'border='0'></a>"; echo "</div>"; echo "</div>"; } } } closedir($handle); } for ($i = 1; $i <= $numOfPages; $i++) { echo "<a href='view_pix.php?page=$i'>"; echo " ".$i." "; echo "</a>"; } ?> Quote Link to comment Share on other sites More sharing options...
RussellReal Posted March 2, 2009 Share Posted March 2, 2009 <?php $page = $_GET['page']; $rpp = 4; $dirArr = scandir('comments/'); function rem_invalid($arr) { foreach ($arr as $k => $v) { if (stristr($v,'.gif') === false) unset($arr[$k]); } } $dirArr = preg_grep("/^.*\.gif$/i",$dirArr); if ($c = count($dirArr)) { $dirArr = array_combine(range(0,count($dirArr) - 1),$dirArr); $numOfPages = ceil($c / $rpp); for ($i = (($page - 1) * $rpp); $i < min($c,($page * $rpp)); $i++) { echo $dirArr[$i]."<br/>"; } } echo "<br /><br />"; print_r(range(1,$numOfPages)); ?> tested and working example: http://lovinglori.com/omglol/?page=1 http://lovinglori.com/omglol/?page=2 Quote Link to comment Share on other sites More sharing options...
paparanch Posted March 2, 2009 Author Share Posted March 2, 2009 wew! it works mr.russel! i just added this code below as the links: for ($i = 1; $i <= $numOfPages; $i++) { echo "<a href='view_pix.php?page=$i'>"; echo " ".$i." "; echo "</a>"; } i know you spend some time in helping me....thank you very much mr.russel! Godbless Quote Link to comment Share on other sites More sharing options...
RussellReal Posted March 2, 2009 Share Posted March 2, 2009 anytime buddy Don't be shy to add me to MSN and ask other questions Quote Link to comment Share on other sites More sharing options...
paparanch Posted March 2, 2009 Author Share Posted March 2, 2009 ahhmnnn...speaking of other questions....yah i have! hehe...i just figure this out... what if i want to display the latest added image first? i mean in my code i declare 4 images per page...how am i going to do to make the latest added image to display at the top? i heard about filectime function but i don't know how to integrate it in my code... would you please help mr.russel? tnx Quote Link to comment Share on other sites More sharing options...
RussellReal Posted March 2, 2009 Share Posted March 2, 2009 yeah that'd be pretty difficult to toy with what you should do, is avoid this whole loop thing, throw all the upload files into a database, with the filename and path to the file in the db, and an auto increment id that way you can order by descending an then get the filenames from there. Quote Link to comment Share on other sites More sharing options...
paparanch Posted March 2, 2009 Author Share Posted March 2, 2009 hehe...actually i already solved this one with database, but i want to try it without database so that i can just copy bunch of images to my folder at one time....which i can't do with database...T_T 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.