From a98c9851644857e33a73f4e362f2802df8385e59 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Thu, 28 Feb 2019 02:20:43 -0300 Subject: [PATCH] Add pagination DB function --- system/database/library/db.php | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/system/database/library/db.php b/system/database/library/db.php index 9c003dc..1dee502 100644 --- a/system/database/library/db.php +++ b/system/database/library/db.php @@ -37,7 +37,36 @@ final class DB { public function getLastId() { return $this->driver->getLastId(); - } + } + + public function pagination($sql, $pageNum_exibe = 1, $maxRows_exibe = 10, $cache = true, $sqlTotal = null){ + + if (($pageNum_exibe >= 1)) { + $pageNum_exibe = $pageNum_exibe-1; + } + $startRow_exibe = $pageNum_exibe * $maxRows_exibe; + + $query_exibe = $sql; + + $query_limit_exibe = sprintf("%s LIMIT %d, %d", $query_exibe, $startRow_exibe, $maxRows_exibe); + + $exibe = $this->query($query_limit_exibe, $cache); + + $re = '/^(SELECT \*)/i'; + + $all_exibe_query = ($sqlTotal != null) ? $sqlTotal : ((preg_match($re, $query_exibe)) ? preg_replace($re, "SELECT COUNT(*) as __TOTALdeREG_DB_Pagination", $query_exibe) : $query_exibe); + + $all_exibe = $this->query($all_exibe_query, $cache); + $totalRows_exibe = (isset($all_exibe->row['__TOTALdeREG_DB_Pagination'])) ? $all_exibe->row['__TOTALdeREG_DB_Pagination'] : $all_exibe->num_rows; + + $totalPages_exibe = ceil($totalRows_exibe/$maxRows_exibe); + + $exibe->totalPages_exibe = $totalPages_exibe; + $exibe->totalRows_exibe = $totalRows_exibe; + $exibe->pageNum_exibe = $pageNum_exibe+1; + + return $exibe; + } private function Cache($sql) { if(class_exists('Caches')) {