Page 1 of 1

Regarding Pagination(Not using database)

Posted: Thu Mar 27, 2008 2:32 am
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

Re: Regarding Pagination(Not using database)

Posted: Thu Mar 27, 2008 3:10 am
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.