Regarding Pagination(Not using database)

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
subeeshkk
Forum Newbie
Posts: 3
Joined: Thu Jan 31, 2008 2:19 am

Regarding Pagination(Not using database)

Post by subeeshkk »

Hi all

My issue is

I am taking images from a server with names like, img1,img2,img3 etc..(Not from the database)
If there are more than 5 images, i need pagination.

currently using a loop it is displayed..all the images..like

for($i=0;$i<cnt;$i++)
<img src="http://otherserver...../img$i.jpg">

But i need to use pagination for this..

How to make this..pls help
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Regarding Pagination(Not using database)

Post by onion2k »

You do it in exactly the same way as any other paging system. The fact there's a database involved or not doesn't change anything. At it's most basic you'd do something like ...

Code: Select all

$total_number_of_images = 100;
$records_per_page = 5;
$total_number_of_pages = floor($total_number_of_images/$records_per_page);
$current_page = (!empty($_GET['page'])) ? intval($_GET['page']) : 0;
 
for($i=($current_page*$records_per_page);$i<(($current_page+1)*$records_per_page);$i++) {
  echo "<img src=\"img".$i.".jpg\">";
}
 
for ($p=0; $p<$total_number_of_pages; $p++) {
  echo "<a href=\"gallery.php?page=".$p."\">Page ".$p."</a>";
}
 
Obviously that code is pretty incomplete (and completely untested) ... it'll break if someone sets $_GET['page'] to -1 for example. The point is that you need to start the loop that displays the images at $page*$records_per_page, and stop it at ($page+1)*$records_per_page. The rest is easy.
Post Reply