Page 1 of 1

Pagination links top and bottom

Posted: Thu Nov 18, 2004 6:53 am
by bradles
Hi All,

I have an image gallery that reads images from a thumbnails folder and displays them in the browser. I would like pagination links like, eg:
PREVIOUS 10 | PREVIOUS | 11 12 13 14 15 16 17 18 19 20 | NEXT | NEXT 10

I want to have these links at the top and bottom of the page and to do that at the moment means writing the code twice. I am thinking that the best way to do it is by computing what links are required, putting them into an array so that they can be easily output to html rather than writing the code twice.

I have looked at a lot of pagination tutorials but cannot seem to find what I am trying to do. Can anyone point me in the right direction?

Brad

Posted: Thu Nov 18, 2004 6:59 am
by CoderGoblin
Perform a search for pagination topics within these forums. Most will talk about database access. In your case you need to store where you are at the moment. Load the complete list of files and then you can use that to calculate how many pages etc. Knowing where you are enables you to move and display links appropriately.

Posted: Thu Nov 18, 2004 7:08 am
by bradles
Hi CoderGoblin,

I already have a pager class that helps me do what you say:

Code: Select all

//Set up Pager Helper Class
class Pager { 
	function getPagerData($numHits, $limit, $page) 
	{ 
		$numHits  = (int) $numHits; 
		$limit    = max((int) $limit, 1); 
		$page     = (int) $page; 
		$numPages = ceil($numHits / $limit); 
		
		
		$page = max($page, 1); 
		$page = min($page, $numPages); 
		
		$offset = ($page - 1) * $limit; 
		$last   = min((int)$offset+$limit, $numHits);
		
		$ret = new stdClass; 
		
		$ret->offset   = $offset; 
		$ret->limit    = $limit; 
		$ret->numPages = $numPages; 
		$ret->page     = $page; 
		$ret->last     = $last;
		
		return $ret; 
	} 
}

$pager = Pager::getPagerData(count($thumbs), $maxperpage, $page);
The challenge I face is constructing the pagination links. If I want to do them at the top and bottom it means double the code. My guess is that there is a way to compute what PREV/NEXT and other 10 links are required and put them in an array that can be easily output to html at the top and bottom of the page...or included...but I don't know how to achieve this.

Brad.

Posted: Thu Nov 18, 2004 7:16 am
by CoderGoblin
Easiest way is to create a variable holding the HTML code. (Only need to process it once). Then simply output the variable wherever you need it.

Code: Select all

<html>
  <head>
    <title>Erm</title>
  </head>
  <body>
     <?php
        $pager = Pager::getPagerData(count($thumbs), $maxperpage, $page);
        echo($pager); 
     ?>
     /*
      *
      * Main Body
      *
      */
     <?php echo($pager); ?>
    </body>
</html>
OK That's mixing the PHP and HTML. I generally prefer however to build a $body variable within the a single PHP section to avoid mixing PHP and HTML as I often find it is less readable.

Posted: Thu Nov 18, 2004 10:54 pm
by bradles
Hmmmm....

All the pager class does is help me with what particular range of images need to be output to the screen.

It does nothing about displaying the links in the range that I mentioned above, eg:
PREVIOUS 10 | PREVIOUS | 11 12 13 14 15 16 17 18 19 20 | NEXT | NEXT 10

How do I get the pager to know what range of links to display from what page it is on. i.e, it needs to know if it should show links 11-20 or 21-30 etc.

Any help would be appreciated.

Brad

Posted: Fri Nov 19, 2004 12:09 am
by rehfeld
<a href="display.php?start=10">10-20</a>
<a href="display.php?start=20">20-30</a>

then you could just use a for loop, and start the loop at $_GET['start']

there was a post just last week about this, there was some code samples given too.