/ Published in: PHP
very easy to use pagination class, see website for comments
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /************************************************************\ * * PHP Array Pagination Copyright 2007 - Derek Harvey * www.lotsofcode.com * * This file is part of PHP Array Pagination . * * PHP Array Pagination is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * PHP Array Pagination is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with PHP Array Pagination ; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * \************************************************************/ class pagination { var $page = 1; // Current Page var $perPage = 10; // Items on each page, defaulted to 10 var $showFirstAndLast = false; // if you would like the first and last page options. function generate($array, $perPage = 10) { // Assign the items per page variable $this->perPage = $perPage; // Assign the page variable $this->page = $_GET['page']; // using the get method } else { $this->page = 1; // if we don't have a page number then assume we are on the first page } // Take the length of the array // Get the number of pages // Calculate the starting point // Return the part of the array we have requested } function links() { // Initiate the links array // Concatenate the get variables to add to the page numbering string $queryURL = ''; foreach ($_GET as $key => $value) { if ($key != 'page') { $queryURL .= '&'.$key.'='.$value; } } } // If we have more then one pages if (($this->pages) > 1) { // Assign the 'previous page' link into the array if we are not on the first page if ($this->page != 1) { if ($this->showFirstAndLast) { $plinks[] = ' <a href="?page=1'.$queryURL.'">«« First </a> '; } $plinks[] = ' <a href="?page='.($this->page - 1).$queryURL.'">« Prev</a> '; } // Assign all the page numbers & links to the array for ($j = 1; $j < ($this->pages + 1); $j++) { if ($this->page == $j) { $links[] = ' <a class="selected">'.$j.'</a> '; // If we are on the same page as the current item } else { $links[] = ' <a href="?page='.$j.$queryURL.'">'.$j.'</a> '; // add the link to the array } } // Assign the 'next page' if we are not on the last page if ($this->page < $this->pages) { $slinks[] = ' <a href="?page='.($this->page + 1).$queryURL.'"> Next » </a> '; if ($this->showFirstAndLast) { $slinks[] = ' <a href="?page='.($this->pages).$queryURL.'"> Last »» </a> '; } } // Push the array into a string using any some glue } return; } } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <title>PHP Array Pagination</title> <style> <!-- body { font-family: Tahoma, Verdana, Arial, Sans-serif; font-size: 11px; } hr { border: 1px #ccc; border-style: none none solid none; margin: 20px 0; } a { color: #333; text-decoration: none; } a:hover { text-decoration: underline; } a.selected { font-weight: bold; text-decoration: underline; } .numbers { line-height: 20px; word-spacing: 4px; } //--> </style> </head> <body> <h1>PHP Array Pagination</h1> <hr /> <?php // Include the pagination class include 'pagination.class.php'; // Create the pagination object $pagination = new pagination; // some example data 'Product' => 'Product '.$value, ); } // If we have an array with items // Parse through the pagination class $productPages = $pagination->generate($products, 10); // If we have items // Create the page numbers echo $pageNumbers = '<div class="numbers">'.$pagination->links().'</div>'; // Loop through all the items in the array foreach ($productPages as $productArray) { // Show the information about the item echo '<p><b>'.$productArray['Product'].'</b> £'.$productArray['Price'].'</p>'; } // print out the page numbers beneath the results echo $pageNumbers; } } ?> <hr /> <p><a href="http://www.lotsofcode.com/php/projects/php-array-pagination" target="_blank">PHP Array Pagination</a> provided by <a href="http://www.lotsofcode.com/" target="_blank">Lots of Code</a></p> </body> </html>
URL: http://www.lotsofcode.com/php/php-array-pagination.htm