Page 1 of 1

pagination using an array

Posted: Mon Apr 30, 2007 9:17 pm
by trent2800
I'm trying to do pagination for a gallery. I'm using an array from a glob function. What I want to do is to start the array further along if you're displaying a page further along. I also only want to display 10, 20, 30, etc.. results. I can't seem to figure out how to do it.

Code: Select all

$result = glob("/home/guttersg/www/images/clothing/sold/th_*.jpg");
				
				foreach ($result as $filename) {
					
					echo "<td align=\"center\">\n";
					
					preg_match('#^.*?_(\d+)\.(jpg|png)$#',$filename,$match);
					
					echo "<A HREF=\"javascript:popUp('http://www.guttersgoods.com/gallery.php?ProductID=".$match[1]."')\"><img border=0 class=galleryimg width=112px height=150px src=\"/images/clothing/sold/".basename($filename)."\"></a>\n";
					
					echo "</td>\n";
					
					if ($i == 4){
						echo "</tr>\n<tr>\n";
						$i = 1;
					}else{					
						$i++;
					}
}

Posted: Mon Apr 30, 2007 9:33 pm
by John Cartwright
instead of a foreach() loop, use a for() loop using the limits as the parameters.

Posted: Mon Apr 30, 2007 9:40 pm
by trent2800
Ok, that will work for the number of items to display, but how to I move further ahead in the array?

Posted: Mon Apr 30, 2007 9:55 pm
by John Cartwright
It is most often done using some form of page variable in the url

Code: Select all

$current = isset($_GET['listing']) ? intval($_GET['listing']) : 0;
$perPage = 10;

echo '<a href="listing.php?listing='. ($perPage+$current).'">Next '. $perPage.'</a>';
This should give you a good start. Have a look at some of the pagination scripts posted in the Code Snipplets forum to get an idea of what else is involved.
Essentially when incrementing the page number, you should check to make sure your not going over the number of listings, or below 0.

Let us know how you progress.

Posted: Tue May 01, 2007 10:37 am
by timvw