diff --git a/MysqliDb.php b/MysqliDb.php index 8de15d83..f2736011 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -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)) diff --git a/dbObject.md b/dbObject.md index 1dd45ff1..aa3184ba 100644 --- a/dbObject.md +++ b/dbObject.md @@ -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 tests file and test models inside the test models directory diff --git a/dbObject.php b/dbObject.php index 3ade1ae2..82ee56cd 100644 --- a/dbObject.php +++ b/dbObject.php @@ -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 * @@ -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; }