| <?php |
| |
| //============================================================= |
| //=== Class: pagination |
| //=== Dependancies: none |
| //=== Author: VBAssassin ( www.coderprofile.com/coder/VBAssassin ) |
| //=== Version: 1.00.00 |
| //=== PHP version: 5.X |
| //=== License: GNU |
| //=== Description: takes care of the hard work when creating pagination and can carry across variables between pages in the pages URL. Multiple instances of pagination can be used on a single page. |
| //============================================================= |
| class pagination { |
| |
| //============================================================= |
| //=== GLOBAL PROPERTIES |
| //============================================================= |
| private $instance = ''; |
| private $total_per_page = 1; |
| private $total_items = 1; |
| private $total_pages = 1; |
| private $current_page = 1; |
| private $query_string = ''; //does not include a ? character |
| |
| //============================================================= |
| //=== SET THE MAIN PROPERTIES |
| //============================================================= |
| function __construct($instance, $total_per_page, $total_items) { |
| |
| //Add some basic vars to the object |
| $this->instance = $instance; |
| $this->total_per_page = $total_per_page; |
| $this->total_items = $total_items; |
| |
| //calculate some more basic vars |
| $this->total_pages = ceil($total_items / $total_per_page); |
| $this->current_page = isset($_GET[$this->instance]) ? intval($_GET[$this->instance]) : 1; |
| |
| //check that the current page is not over the max pages |
| if ($this->current_page > $this->total_pages) { |
| $this->current_page = $this->total_pages; |
| } |
| |
| //check that the current page is not over the max pages |
| if ($this->current_page < 1) { |
| $this->current_page = 1; |
| } |
| |
| //generate the query string |
| $this->query_string = $this->get_query_string(); |
| |
| } |
| |
| //============================================================= |
| //=== GET THE LIMIT NEEDED FOR SQL DATABASES |
| //============================================================= |
| function get_sql_limit() { |
| return (($this->current_page-1) * $this->total_per_page) . ',' . $this->total_per_page; |
| } |
| |
| //============================================================= |
| //=== GENERATE THE HTML PAGINATION FOR PEOPLE TO CLICK |
| //============================================================= |
| function get_pagination() { |
| |
| //store the generated html |
| $pages = ''; |
| |
| //generate the html for the pagination |
| for ($counter = 1; $counter <= $this->total_pages; $counter++) { |
| |
| //see if this page is currently selected |
| $selected_style = ($this->current_page == $counter ? ' style="font-weight:bold;color:green;"' : ''); |
| |
| //put together the html for this page number |
| if ($counter == $this->total_pages) { |
| //last page number |
| $pages .= "<a href='{$_SERVER['PHP_SELF']}?{$this->query_string}{$this->instance}={$counter}'{$selected_style}>{$counter}</a>"; |
| } else { |
| //more pages to come... |
| $pages .= "<a href='{$_SERVER['PHP_SELF']}?{$this->query_string}{$this->instance}={$counter}'{$selected_style}>{$counter}</a>, "; |
| } |
| |
| } |
| |
| //return the generated html for the pagination |
| return $pages; |
| |
| } |
| |
| //============================================================= |
| //=== GETS THE QUERY STRING WITHOUT THE PAGINATION VARS |
| //============================================================= |
| private function get_query_string() { |
| |
| //get all the params |
| $query_string = ''; |
| foreach ($_GET as $key=>$value) { |
| if ($key != $this->instance) { |
| $query_string .= "{$key}={$value}&"; |
| } |
| } |
| return $query_string; |
| |
| } |
| |
| } |
| |
| /* |
| |
| //Example of how to use this class |
| |
| $pagination = new pagination('page', 25, 500); |
| //mysql_query("SELECT * FROM `table` WHERE `something` = '2' LIMIT " . $pagination->get_sql_limit()); |
| print $pagination->get_pagination(); |
| |
| print '<br /><br />'; |
| |
| $pagination = new pagination('page_again', 25, 500); |
| //mysql_query("SELECT * FROM `table` WHERE `something` = '2' LIMIT " . $pagination->get_sql_limit()); |
| print $pagination->get_pagination(); |
| |
| */ |
| |
| ?> |