Skip to content
Merged

fixes #353

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions MysqliDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,9 @@ protected function replacePlaceHolders ($str, $vals) {
$i = 1;
$newStr = "";

if (empty ($vals))
return $str;

while ($pos = strpos ($str, "?")) {
$val = $vals[$i++];
if (is_object ($val))
Expand Down
10 changes: 10 additions & 0 deletions dbObject.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ Object could be easily converted to a json string or an array.
$userArray = $user->toArray();
```

###Pagination
Use paginate() instead of get() to fetch paginated result
```php
$page = 1;
// set page limit to 2 results per page. 20 by default
product::$pageLimit = 2;
$products = product::arraybuilder()->paginate($page);
echo "showing $page out of " . product::$totalPages;

```
###Examples

Please look for a use examples in <a href='tests/dbObjectTests.php'>tests file</a> and test models inside the <a href='tests/models/'>test models</a> directory
10 changes: 5 additions & 5 deletions dbObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ class dbObject {
*
* @var int
*/
public $pageLimit = 20;
public static $pageLimit = 20;
/**
* Variable that holds total pages count of last paginate() query
*
* @var int
*/
public $totalPages = 0;
public static $totalPages = 0;
/**
* An array that holds insert/update/select errors
*
Expand Down Expand Up @@ -427,10 +427,10 @@ protected function count () {
* @return array
*/
private function paginate ($page, $fields = null) {
$offset = $this->pageLimit * ($page - 1);
$offset = self::$pageLimit * ($page - 1);
$this->db->withTotalCount();
$results = $this->get (Array ($this->pageLimit, $offset), $fields);
$this->totalPages = round ($this->db->totalCount / $this->pageLimit);
$results = $this->get (Array ($offset, self::$pageLimit), $fields);
self::$totalPages = round ($this->db->totalCount / self::$pageLimit);

return $results;
}
Expand Down