├── .github └── FUNDING.yml ├── README.md ├── license.txt ├── pagination.css └── paginator.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [dcblogdev] 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | pagination 2 | ========== 3 | 4 | Paginate record sets, not tied in directly to a database. 5 | 6 | ## Usage 7 | 1. include the class 8 | 2. instantiate a new object pass in the number of items per page and the instance identifier, this is used for the GET parameter such as ?p=2 9 | 3. pass the set_total method the total number of records 10 | 4. show the records 11 | 5. call the page_links method to create the navigation links 12 | 13 | ````php 14 | include('paginator.php'); 15 | 16 | $pages = new Paginator('10','p'); 17 | $pages->set_total('100'); //or a number of records 18 | 19 | //display the records here 20 | 21 | echo $pages->page_links(); 22 | ```` 23 | if using a database you limit the records by placing $pages->get_limit() in your query, this will limit the number of records 24 | 25 | ````php 26 | select * from table $pages->get_limit() 27 | ```` 28 | 29 | by default the page_links method created links starting with ? this can be changed by passing in a parameter to the method: 30 | 31 | ````php 32 | echo $pages->page_links('&'); 33 | ```` 34 | 35 | The method also allows you to pass in extra data such as a series of GET's 36 | 37 | ````php 38 | echo $pages->page_links('?','&status='.$_GET['status'].'&active='.$_GET['active']); 39 | ```` 40 | 41 | ## Database example 42 | 43 | ````php 44 | //include the class 45 | include('paginator.php'); 46 | 47 | //create new object pass in number of pages and identifier 48 | $pages = new Paginator('10','p'); 49 | 50 | //get number of total records 51 | $stmt = $db->query('SELECT id FROM table'); 52 | $total = $stmt->rowCount(); 53 | 54 | //pass number of records to 55 | $pages->set_total($total); 56 | 57 | $data = $db->query('SELECT * FROM table '.$pages->get_limit()); 58 | foreach($data as $row) { 59 | //display the records here 60 | } 61 | 62 | //create the page links 63 | echo $pages->page_links(); 64 | ```` 65 | 66 | ## MVC example 67 | 68 | using this class in an MVC environment its almost the same, only the database or dataset calls come from the model instead of the page directly. 69 | 70 | in the controller: 71 | 72 | ````php 73 | 74 | //create a new object 75 | $pages = new Paginator('10','p'); 76 | 77 | //set the total records, calling a method to get the number of records from a model 78 | $pages->set_total( $this->_model->get_all_count() ); 79 | 80 | //calling a method to get the records with the limit set 81 | $data['records'] = $this->_model->get_all( $pages->get_limit() ); 82 | 83 | //create the nav menu 84 | $data['page_links'] = $pages->page_links(); 85 | 86 | //then pass this to the view, may be different depending on the system 87 | $this->_view->render('index', $data); 88 | ```` 89 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 David Carr - https://github.com/daveismyname/pagination 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /pagination.css: -------------------------------------------------------------------------------- 1 | .pagination { 2 | clear: both; 3 | padding: 0; 4 | } 5 | .pagination li { 6 | display:inline; 7 | } 8 | .pagination a { 9 | border: 1px solid #D5D5D5; 10 | color: #666666; 11 | font-size: 11px; 12 | font-weight: bold; 13 | height: 25px; 14 | padding: 4px 8px; 15 | text-decoration: none; 16 | margin:2px; 17 | } 18 | .pagination a:hover, .pagination a:active { 19 | background:#efefef; 20 | } 21 | .pagination span.current { 22 | background-color: #687282; 23 | border: 1px solid #D5D5D5; 24 | color: #ffffff; 25 | font-size: 11px; 26 | font-weight: bold; 27 | height: 25px; 28 | padding: 4px 8px; 29 | text-decoration: none; 30 | margin:2px; 31 | } 32 | .pagination span.disabled { 33 | border: 1px solid #EEEEEE; 34 | color: #DDDDDD; 35 | margin: 2px; 36 | padding: 2px 5px; 37 | } 38 | -------------------------------------------------------------------------------- /paginator.php: -------------------------------------------------------------------------------- 1 | _instance = $instance; 65 | $this->_perPage = $perPage; 66 | $this->set_instance(); 67 | $this->_customCSS = $customCSS; 68 | } 69 | 70 | /** 71 | * get_start 72 | * 73 | * creates the starting point for limiting the dataset 74 | * @return numeric 75 | */ 76 | public function get_start(){ 77 | return ($this->_page * $this->_perPage) - $this->_perPage; 78 | } 79 | 80 | /** 81 | * set_instance 82 | * 83 | * sets the instance parameter, if numeric value is 0 then set to 1 84 | * 85 | * @var numeric 86 | */ 87 | private function set_instance(){ 88 | $this->_page = (int) (!isset($_GET[$this->_instance]) ? 1 : $_GET[$this->_instance]); 89 | $this->_page = ($this->_page == 0 ? 1 : ($this->_page < 0 ? 1 : $this->_page)); 90 | } 91 | 92 | /** 93 | * set_total 94 | * 95 | * collect a numberic value and assigns it to the totalRows 96 | * 97 | * @var numeric 98 | */ 99 | public function set_total($_totalRows){ 100 | $this->_totalRows = $_totalRows; 101 | } 102 | 103 | /** 104 | * get_limit 105 | * 106 | * returns the limit for the data source, calling the get_start method and passing in the number of items perp page 107 | * 108 | * @return string 109 | */ 110 | public function get_limit(){ 111 | return "LIMIT ".$this->get_start().",$this->_perPage"; 112 | } 113 | 114 | /** 115 | * get_limit_keys 116 | * 117 | * returns an array of the offset and limit returned on each call 118 | * 119 | * @return string 120 | */ 121 | public function get_limit_keys(){ 122 | return ['offset' => $this->get_start(), 'limit' => $this->_perPage]; 123 | } 124 | 125 | /** 126 | * page_links 127 | * 128 | * create the html links for navigating through the dataset 129 | * 130 | * @var sting $path optionally set the path for the link 131 | * @var sting $ext optionally pass in extra parameters to the GET 132 | * @return string returns the html menu 133 | */ 134 | public function page_links($path = '?', $ext = null) 135 | { 136 | $adjacents = "2"; 137 | $prev = $this->_page - 1; 138 | $next = $this->_page + 1; 139 | $lastpage = ceil($this->_totalRows / $this->_perPage); 140 | $lpm1 = $lastpage - 1; 141 | 142 | $pagination = ""; 143 | if ($lastpage > 1) { 144 | $pagination .= "