Ticket #261: jPageCounter.class.php

File jPageCounter.class.php, 3.0 kB (added by bballizlife, 1 year ago)

jPageCounter class

Line 
1 <?php
2 /**
3 * @package      jelix
4 * @subpackage   utils
5 * @author       Loic Mathaud <loic@mathaud.net>
6 * @copyright    Loic Mathaud 2007
7 * @link         http://www.jelix.org
8 * @licence  http://www.gnu.org/licenses/gpl.html GNU General Public Licence, see LICENCE file
9 */
10
11 /**
12 * Utility class to deal with pagination
13 * Action :
14 * $pageCounter = new jPageCounter(15);
15 * $pageCounter->setTotalItems(47);
16 * $pageCounter->setCurrentPage(3);
17 * $pageCounter->count();
18 *
19 * Template : using the pagination plugin
20 * {pagination 'm~c_a', array('action params'), $pageCounterObject, 'page', array('plugin config options')}
21 */
22 class jPageCounter {
23     /**
24     * Number of total items in the list
25     * @var int
26     */
27     protected $total_items;
28     
29     /**
30     * Number of item to display on a page
31     * @var int
32     */
33     protected $items_per_page;
34     
35     /**
36     * Number of the page asked
37     * @var int
38     */
39     protected $current_page = 1;
40     
41     /**
42     * Number of pages
43     * @var int
44     */
45     protected $total_pages;
46     
47     /**
48     * Offset value in the DB query
49     * @var int
50     */
51     protected $offset;
52     
53     
54     /**
55     * Constructor of the class
56     * @param int $item_pp Number of items to display on a page (default 10)
57     */
58     function __construct($items_pp = 10) {
59         $this->items_per_page = intval($items_pp);
60     }
61     
62     /**
63     * Set $total_items
64     * @param int $items Total number of items
65     */
66     public function setTotalItems($items) {
67         $this->total_items = intval($items);
68     }
69     
70     /**
71     * Set the current page
72     * @param int $page Number of the current page
73     */
74     public function setCurrentPage($page) {
75         if (empty($page)) {
76             $page = 1;
77         }
78         $this->current_page = intval($page);
79     }
80     
81     /**
82     * Calculate the number of pages and the offset
83     */
84     public function count() {
85         // Calculate the number of pages
86         $this->total_pages = ceil($this->total_items / $this->items_per_page);
87
88         // Calculate the offset
89         if ($this->current_page == 1) {
90             $this->offset = 0;
91         } else {
92             $this->offset = ($this->current_page - 1) * $this->items_per_page;
93         }
94     }
95     
96     /**
97     * Get $total_items
98     * @return int the number of items
99     */
100     public function getTotalItems() {
101         return (int)$this->total_items;
102     }
103     
104     /**
105     * Get $current_page
106     * @return int the current page
107     */
108     public function getCurrentPage() {
109         return (int)$this->current_page;
110     }
111     
112     /**
113     * Get $total_pages
114     * @return int the number of pages
115     */
116     public function getTotalPages() {
117         return (int)$this->total_pages;
118     }
119     
120     /**
121     * Get $items_per_page
122     * @return int the number of items to display per page
123     */
124     public function getItemsPerPage() {
125         return (int)$this->items_per_page;
126     }
127     
128     /**
129     * Get $offset
130     * @return int the offset
131     */
132     public function getOffset() {
133         return (int)$this->offset;
134     }
135 }
136
137 ?>
138
Download in other formats: Original Format