Jump to content

[SOLVED] displaying images from a folder with pagination...pls help


Recommended Posts

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..

<?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>";
       }


   }

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>";
       }


   }
?>

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.

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>";

	}

?>

<?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

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

 

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.