aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2012-12-08 02:50:42 +0100
committerGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2012-12-08 02:50:42 +0100
commita4173dd2e979773f9a65b51c20ef0708ba1474d5 (patch)
treebba54686152793b0d4dae1bca721384c9f1b4da2 /lib
parentadc704c3d75518fd7ed7a32b9ed21d9b7eb71c99 (diff)
On simule la pagination pour optimiser les requêtes en BDD (évite de tout charger puis d'en jeter les 3/4)
Diffstat (limited to 'lib')
-rwxr-xr-xlib/Paginator.php26
1 files changed, 19 insertions, 7 deletions
diff --git a/lib/Paginator.php b/lib/Paginator.php
index dccb9014d..1a8376e75 100755
--- a/lib/Paginator.php
+++ b/lib/Paginator.php
@@ -29,11 +29,17 @@ class Paginator {
private $nbPage = 1;
/**
+ * $nbItems le nombre d'éléments
+ */
+ private $nbItems = 0;
+
+ /**
* Constructeur
* @param $items les éléments à gérer
*/
public function __construct ($items) {
$this->_items ($items);
+ $this->_nbItems (count ($this->items (true)));
$this->_nbItemsPerPage ($this->nbItemsPerPage);
$this->_currentPage ($this->currentPage);
}
@@ -66,7 +72,7 @@ class Paginator {
}
$i++;
- } while (!$page && $i < count ($this->items));
+ } while (!$page && $i < $this->nbItems ());
return $page;
}
@@ -86,7 +92,7 @@ class Paginator {
} else {
$i++;
}
- } while (!$find && $i < count ($this->items));
+ } while (!$find && $i < $this->nbItems ());
return $i;
}
@@ -98,7 +104,7 @@ class Paginator {
*/
public function itemByPosition ($pos) {
if ($pos < 0) {
- $pos = count ($this->items) - 1;
+ $pos = $this->nbItems () - 1;
}
if ($pos >= count($this->items)) {
$pos = 0;
@@ -115,7 +121,7 @@ class Paginator {
*/
public function items ($all = false) {
$array = array ();
- $nbItems = count ($this->items);
+ $nbItems = $this->nbItems ();
if ($nbItems <= $this->nbItemsPerPage || $all) {
$array = $this->items;
@@ -147,6 +153,9 @@ class Paginator {
public function nbPage () {
return $this->nbPage;
}
+ public function nbItems () {
+ return $this->nbItems;
+ }
/**
* SETTEURS
@@ -159,8 +168,8 @@ class Paginator {
$this->_nbPage ();
}
public function _nbItemsPerPage ($nbItemsPerPage) {
- if ($nbItemsPerPage > count ($this->items)) {
- $nbItemsPerPage = count ($this->items);
+ if ($nbItemsPerPage > $this->nbItems ()) {
+ $nbItemsPerPage = $this->nbItems ();
}
if ($nbItemsPerPage < 0) {
$nbItemsPerPage = 0;
@@ -178,7 +187,10 @@ class Paginator {
}
private function _nbPage () {
if ($this->nbItemsPerPage > 0) {
- $this->nbPage = ceil (count ($this->items) / $this->nbItemsPerPage);
+ $this->nbPage = ceil ($this->nbItems () / $this->nbItemsPerPage);
}
}
+ public function _nbItems ($value) {
+ $this->nbItems = $value;
+ }
}