I have decided to spend some time making tutorials for one of the numerous components of Zend Framework. As many of you already know, this framework's components can be used individually, (as I shall demonstrate within this tutorial) so that they often coincide with the PHP library. I hope that this will prove useful to some people, considering that I cover a very important area in PHP coding - pagination. I have also included a zipped version of this tutorial that includes all required components so you can use it out of the box. View attachment paginator.zip index.php <?php //first we set our PHP include path to include our Zend library without changing the initial include path set_include_path(implode(PATH_SEPARATOR, array('./library', get_include_path()))); $currentPageNumber = $_GET['page']; //don't worry about sanitizing the input, we won't use this variable in our script's logic, instead the paginator will give us the correct page number $totalNumberOfItems = 3115; //in most cases you can get this number by using SQL_CALC_FOUND_ROWS together with FOUND_ROWS(), this MUST be an integer bigger then 0 for paginator's factory method to work $itemsPerPage = 20; //pretty self-explanatory, defaults to 10 $pageRange = 15; //maximum number of pages shown in our paginator, in some cases this is approximate number, but is never ignored; defaults to 10 require_once 'Zend/Paginator.php'; $paginator = Zend_Paginator::factory($totalNumberOfItems); $paginator->setCurrentPagenumber($currentPageNumber); $paginator->setItemCountPerPage($itemsPerPage); /* There is a total of 4 paginator scrolling types integrated in Zend Framework (of course, you can always build your own if you have the need to) and they are pretty well explained on the official Zend Framework site http://framework.zend.com/manual/en/zend.paginator.usage.html For the sake of this tutorial, I will copy the part that matters to us here All - Returns every page. This is useful for dropdown menu pagination controls with relatively few pages. In these cases, you want all pages available to the user at once. Elastic - A Google-like scrolling style that expands and contracts as a user scrolls through the pages. Jumping - As users scroll through, the page number advances to the end of a given range, then starts again at the beginning of the new range. Sliding - A Yahoo!-like scrolling style that positions the current page number in the center of the page range, or as close as possible. This is the default style. */ $scrollType = 'Sliding'; //change this to 'All', 'Elastic', 'Sliding' or 'Jumping' to test all scrolling types $paginator = get_object_vars($paginator->getPages($scrollType)); //and that's it! we can now use the $paginator variable as we want //die('<pre>'.print_r($paginator, true)); //uncomment this line to see our paginator's content ?> <html> <head> <title>Zend_Paginator tutorial by Gray Fox</title> </head> <body> <h1>Zend_Paginator demonstration</h1> <ul id="items"> <?php for($i = $paginator['firstItemNumber']; $i <= $paginator['lastItemNumber']; $i++): ?> <li>This is item #<?php echo $i ?></li> <?php endfor ?> </ul> <div id="paginator"> <p>Showing <strong><?php echo $paginator['firstItemNumber'] ?> to <?php echo $paginator['lastItemNumber'] ?></strong> out of <strong><?php echo $paginator['totalitemCount'] ?></strong> items</p> <?php if($paginator['previous']): ?> <a href="?page=<?php echo $paginator['previous'] ?>">« Prev</a> <?php else: ?> <span>« Prev</span> <?php endif ?> <?php if($paginator['firstPageInRange'] > $paginator['first']): ?> <a href="?page=<?php echo $paginator['first'] ?>"><?php echo $paginator['first'] ?></a> <span>...</span> <?php endif ?> <?php foreach($paginator['pagesInRange'] as $page): ?> <?php if($page == $paginator['current']): ?> <span><?php echo $page ?></span> <?php else: ?> <a href="?page=<?php echo $page ?>"><?php echo $page ?></a> <?php endif ?> <?php endforeach ?> <?php if($paginator['lastPageInRange'] < $paginator['last']): ?> <span>...</span> <a href="?page=<?php echo $paginator['last'] ?>"><?php echo $paginator['last'] ?></a> <?php endif ?> <?php if($paginator['next']): ?> <a href="?page=<?php echo $paginator['next'] ?>">Next »</a> <?php else: ?> <span>Next »</span> <?php endif ?> </div> </body> </html> PHP: