pagination using an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
trent2800
Forum Commoner
Posts: 48
Joined: Mon Oct 02, 2006 7:02 am

pagination using an array

Post 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++;
					}
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

instead of a foreach() loop, use a for() loop using the limits as the parameters.
trent2800
Forum Commoner
Posts: 48
Joined: Mon Oct 02, 2006 7:02 am

Post by trent2800 »

Ok, that will work for the number of items to display, but how to I move further ahead in the array?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Post Reply