From d509d13eb04bc453abd7299bc979efedb35eeb25 Mon Sep 17 00:00:00 2001 From: Myrna Degano Date: Tue, 9 Mar 2004 18:30:21 +0000 Subject: [PATCH] Se agrega paginador para arrays. --- lib/MECON/Array/Pager.php | 150 +++++++++++++++++++++++++++++++++++++ lib/MECON/HTML/TablaDB.php | 6 +- 2 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 lib/MECON/Array/Pager.php diff --git a/lib/MECON/Array/Pager.php b/lib/MECON/Array/Pager.php new file mode 100644 index 0000000..f7d5fcb --- /dev/null +++ b/lib/MECON/Array/Pager.php @@ -0,0 +1,150 @@ +dbh = $res->dbh; + $this->result = $res; + $this->row_counter = $res->row_counter; + $this->limit_from = $res->limit_from; + $this->from = $from; + $this->limit = $limit; + $this->numrows = $numrows; + $this->maxpages = $maxpages; + $this->build(); + } + + /** + * Calcula los datos necesarios para que funcione el paginador + * (es igual a MECON_DB_Pager) + * @return mixed PEAR_Error if error. + */ + function build() + { + // if there is no numrows given, calculate it + if ($this->numrows === null) { + $this->numrows = $this->numRows(); + if (DB::isError($this->numrows)) { + return $this->numrows; + } + } + // Si no hay resultados no se hace nada. + if (empty($this->numrows) or ($this->numrows < 0)) { + return; + } + $this->from = empty($this->from) ? 0 : $this->from; + + if ($this->limit <= 0) { + return PEAR::raiseError (null, 'wrong "limit" param', null, + null, null, 'DB_Error', true); + } + + // Total number of pages + $this->numpages = ceil($this->numrows/$this->limit); + + // first & last page + $this->firstpage = 1; + $this->lastpage = $this->numpages; + + // Build pages array + $this->pages = array(); + for ($i = 1; $i <= $this->numpages; $i++) { + $offset = $this->limit * ($i - 1); + $this->pages[$i] = $offset; + // $from must point to one page + if ($this->from == $offset) { + // The current page we are + $this->currentpage = $i; + } + } + if (!isset($this->currentpage)) { + return PEAR::raiseError (null, 'wrong "from" param', null, + null, null, 'DB_Error', true); + } + + // Limit number of pages (goole algoritm) + if ($this->maxpages) { + $radio = floor($this->maxpages/2); + $this->firstpage = $this->currentpage - $radio; + if (!($this->maxpages % 2)) { + $this->firstpage++; + } + if ($this->firstpage < 1) { + $this->firstpage = 1; + } + $this->lastpage = $this->currentpage + $radio; + if ($this->lastpage > $this->numpages) { + $this->lastpage = $this->numpages; + } + foreach (range($this->firstpage, $this->lastpage) as $page) { + $tmp[$page] = $this->pages[$page]; + } + $this->pages = $tmp; + } + + // Prev link + $this->prev = $this->from - $this->limit; + $this->prev = ($this->prev >= 0) ? $this->prev : null; + + // Next link + $this->next = $this->from + $this->limit; + $this->next = ($this->next < $this->numrows) ? $this->next : null; + + // Results remaining in next page & Last row to fetch + if ($this->currentpage == $this->numpages) { + $this->remain = 0; + $this->to = $this->numrows; + } else { + if ($this->currentpage == ($this->numpages - 1)) { + $this->remain = $this->numrows - ($this->limit * ($this->numpages - 1)); + } else { + $this->remain = $this->limit; + } + $this->to = $this->currentpage * $this->limit; + } + + // Current item (when fetching). + $this->current = $this->from - 1; + } + + /** + * Obtiene la siguiente fila de la tabla + * @return array + */ + function fetchRow($mode=null, $rownum=null) + { + $this->current++; + if ($this->current >= $this->to) { + return null; + } + return $this->result[$this->current]; + } + + /** + * Devuelve la cantidad de filas de la tabla + * @return int. + */ + function numRows(){ + return count($this->result); + } + +} +?> diff --git a/lib/MECON/HTML/TablaDB.php b/lib/MECON/HTML/TablaDB.php index 0378f96..4e06d25 100644 --- a/lib/MECON/HTML/TablaDB.php +++ b/lib/MECON/HTML/TablaDB.php @@ -26,6 +26,7 @@ $Id$ require_once 'DB.php'; require_once 'MECON/DB/Pager.php'; +require_once 'MECON/Array/Pager.php'; require_once 'MECON/HTML/Error.php'; require_once 'MECON/HTML/Link.php'; require_once 'MECON/HTML/Tabla.php'; @@ -144,7 +145,10 @@ class MECON_HTML_TablaDB extends MECON_HTML_Tabla { */ function addPager($result, $tipo = null, $link = null, $limit = 10, $maxpages = 21, $getvar = 'from') { // Creo el pager con el resultado. - $pager = new MECON_DB_Pager($result, @$_GET[$this->getGetVarPrefix().$getvar], $limit, $maxpages); + $pager = (is_array($result))? + new MECON_Array_Pager($result, @$_GET[$this->_getVarPrefix.$getvar], $limit, $maxpages): + new MECON_DB_Pager($result, @$_GET[$this->_getVarPrefix.$getvar], $limit, $maxpages); + // Obtengo un link válido. if (!$link) { $link = @$_SERVER['PHP_SELF']; -- 2.43.0