4 * Paginador de tablas, a partir de un array
5 * Basado en MECON_DB_Pager
7 class MECON_Array_Pager
16 * @param array $res Contenido de la taba
17 * @param int $from Nro. de fila 'desde'
18 * @param int $limit Cantidad de resultados por página
19 * @param int $maxpages Cantidad máxima de páginas a mostrar
20 * @param int $numrows Nro. de filas de la tabla
22 function MECON_Array_Pager (&$res, $from = 0, $limit = 10, $maxpages = 21, $numrows = null)
24 $this->dbh = $res->dbh;
26 $this->row_counter = $res->row_counter;
27 $this->limit_from = $res->limit_from;
29 $this->limit = $limit;
30 $this->numrows = $numrows;
31 $this->maxpages = $maxpages;
36 * Calcula los datos necesarios para que funcione el paginador
37 * (es igual a MECON_DB_Pager)
38 * @return mixed PEAR_Error if error.
42 // if there is no numrows given, calculate it
43 if ($this->numrows === null) {
44 $this->numrows = $this->numRows();
45 if (DB::isError($this->numrows)) {
46 return $this->numrows;
49 // Si no hay resultados no se hace nada.
50 if (empty($this->numrows) or ($this->numrows < 0)) {
53 $this->from = empty($this->from) ? 0 : $this->from;
55 if ($this->limit <= 0) {
56 return PEAR::raiseError (null, 'wrong "limit" param', null,
57 null, null, 'DB_Error', true);
60 // Total number of pages
61 $this->numpages = ceil($this->numrows/$this->limit);
65 $this->lastpage = $this->numpages;
68 $this->pages = array();
69 for ($i = 1; $i <= $this->numpages; $i++) {
70 $offset = $this->limit * ($i - 1);
71 $this->pages[$i] = $offset;
72 // $from must point to one page
73 if ($this->from == $offset) {
74 // The current page we are
75 $this->currentpage = $i;
78 if (!isset($this->currentpage)) {
79 return PEAR::raiseError (null, 'wrong "from" param', null,
80 null, null, 'DB_Error', true);
83 // Limit number of pages (goole algoritm)
84 if ($this->maxpages) {
85 $radio = floor($this->maxpages/2);
86 $this->firstpage = $this->currentpage - $radio;
87 if (!($this->maxpages % 2)) {
90 if ($this->firstpage < 1) {
93 $this->lastpage = $this->currentpage + $radio;
94 if ($this->lastpage > $this->numpages) {
95 $this->lastpage = $this->numpages;
97 foreach (range($this->firstpage, $this->lastpage) as $page) {
98 $tmp[$page] = $this->pages[$page];
104 $this->prev = $this->from - $this->limit;
105 $this->prev = ($this->prev >= 0) ? $this->prev : null;
108 $this->next = $this->from + $this->limit;
109 $this->next = ($this->next < $this->numrows) ? $this->next : null;
111 // Results remaining in next page & Last row to fetch
112 if ($this->currentpage == $this->numpages) {
114 $this->to = $this->numrows;
116 if ($this->currentpage == ($this->numpages - 1)) {
117 $this->remain = $this->numrows - ($this->limit * ($this->numpages - 1));
119 $this->remain = $this->limit;
121 $this->to = $this->currentpage * $this->limit;
124 // Current item (when fetching).
125 $this->current = $this->from - 1;
129 * Obtiene la siguiente fila de la tabla
132 function fetchRow($mode=null, $rownum=null)
135 if ($this->current >= $this->to) {
138 return $this->result[$this->current];
142 * Devuelve la cantidad de filas de la tabla
146 return count($this->result);