Skip to content

Commit 7d08fbc

Browse files
committed
added paginate() to mysqlidb
1 parent d547858 commit 7d08fbc

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

MysqliDb.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,20 @@ class MysqliDb
180180
protected $traceStripPrefix;
181181
public $trace = array();
182182

183+
/**
184+
* Per page limit for pagination
185+
*
186+
* @var int
187+
*/
188+
189+
public $pageLimit = 20;
190+
/**
191+
* Variable that holds total pages count of last paginate() query
192+
*
193+
* @var int
194+
*/
195+
public $totalPages = 0;
196+
183197
/**
184198
* @param string $host
185199
* @param string $username
@@ -1838,6 +1852,22 @@ public function map($idField)
18381852
$this->_mapKey = $idField;
18391853
return $this;
18401854
}
1855+
1856+
/**
1857+
* Pagination wraper to get()
1858+
*
1859+
* @access public
1860+
* @param string $table The name of the database table to work with
1861+
* @param int $page Page number
1862+
* @param array|string $fields Array or coma separated list of fields to fetch
1863+
* @return array
1864+
*/
1865+
public function paginate ($table, $page, $fields = null) {
1866+
$offset = $this->pageLimit * ($page - 1);
1867+
$res = $this->withTotalCount()->get ($table, Array ($offset, $this->pageLimit), $fields);
1868+
$this->totalPages = round($this->totalCount / $this->pageLimit);
1869+
return $res;
1870+
}
18411871
}
18421872

1843-
// END class
1873+
// END class

dbObject.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,10 @@ protected function count () {
427427
* @return array
428428
*/
429429
private function paginate ($page, $fields = null) {
430-
$offset = self::$pageLimit * ($page - 1);
431-
$this->db->withTotalCount();
432-
$results = $this->get (Array ($offset, self::$pageLimit), $fields);
433-
self::$totalPages = round ($this->db->totalCount / self::$pageLimit);
434-
435-
return $results;
430+
$this->db->pageLimit = self::$pageLimit;
431+
$res = $this->db->paginate ($this->dbTable, $page, $fields);
432+
self::$totalPages = $this->db->totalPages;
433+
return $res;
436434
}
437435

438436
/**

readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ foreach ($logins as $login)
198198
echo $login;
199199
```
200200

201+
###Pagination
202+
Use paginate() instead of get() to fetch paginated result
203+
```php
204+
$page = 1;
205+
// set page limit to 2 results per page. 20 by default
206+
$db->pageLimit = 2;
207+
$products = $db->arraybuilder()->paginate("products", $page);
208+
echo "showing $page out of " . $db->totalPages;
209+
210+
```
211+
201212
### Result transformation / map
202213
Instead of getting an pure array of results its possible to get result in an associative array with a needed key. If only 2 fields to fetch will be set in get(),
203214
method will return result in array($k => $v) and array ($k => array ($v, $v)) in rest of the cases.

0 commit comments

Comments
 (0)