Commit bbd80bc5 authored by Alexander Butenko's avatar Alexander Butenko

withTotalCount() method to return total number of rows

parent 32b81aa1
......@@ -64,7 +64,7 @@ class MysqliDb
*/
protected $_groupBy = array();
/**
* Dynamic array that holds a combination of where condition/table data value types and parameter referances
* Dynamic array that holds a combination of where condition/table data value types and parameter references
*
* @var array
*/
......@@ -75,6 +75,13 @@ class MysqliDb
* @var string
*/
public $count = 0;
/**
* Variable which holds an amount of returned rows during get/getOne/select queries with withTotalCount()
*
* @var string
*/
public $totalCount = 0;
protected $fetchTotalCount = false;
/**
* Variable which holds last statement error
*
......@@ -253,6 +260,16 @@ class MysqliDb
return $this->_dynamicBindResults($stmt);
}
/**
* Function to enable SQL_CALC_FOUND_ROWS in the get queries
*
* @return MysqliDb
*/
public function withTotalCount () {
$this->fetchTotalCount = true;
return $this;
}
/**
* A convenient SELECT * function.
*
......@@ -266,8 +283,9 @@ class MysqliDb
if (empty ($columns))
$columns = '*';
$this->_query = $this->fetchTotalCount == true ? 'SELECT SQL_CALC_FOUND_ROWS ' : 'SELECT ';
$column = is_array($columns) ? implode(', ', $columns) : $columns;
$this->_query = "SELECT $column FROM " .self::$_prefix . $tableName;
$this->_query .= "$column FROM " .self::$_prefix . $tableName;
$stmt = $this->_buildQuery($numRows);
if ($this->isSubQuery)
......@@ -677,6 +695,7 @@ class MysqliDb
call_user_func_array(array($stmt, 'bind_result'), $parameters);
$this->totalCount = 0;
$this->count = 0;
while ($stmt->fetch()) {
$x = array();
......@@ -687,6 +706,13 @@ class MysqliDb
array_push($results, $x);
}
if ($this->fetchTotalCount === true) {
$this->fetchTotalCount = false;
$stmt = $this->_mysqli->query ('SELECT FOUND_ROWS();');
$totalCount = $stmt->fetch_row();
$this->totalCount = $totalCount[0];
}
return $results;
}
......
......@@ -265,6 +265,14 @@ $res = $db->get ("users");
```
Find the total number of rows matched. Simple pagination example:
```php
$offset = 10;
$count = 15;
$users = $db->withTotalCount()->get('users', Array ($offset, $count));
echo "Showing {$count} from {$db->totalCount}";
```
Optionally you can use method chaining to call where multiple times without referencing your object over an over:
```php
......@@ -309,7 +317,6 @@ print_r ($products);
### Properties sharing
Its is also possible to copy properties
Simple pagination example:
```php
$db->where ("agentId", 10);
$db->where ("active", true);
......
......@@ -163,7 +163,6 @@ if ($db->count != 3) {
echo "Invalid total insert count";
exit;
}
echo $db->getLastQuery();
// order by field
$db->orderBy("login","asc", Array ("user3","user2","user1"));
......@@ -341,6 +340,12 @@ if ($db->count != 5) {
echo "invalid join with subquery count";
exit;
}
$db->withTotalCount()->get('users', 1);
if ($db->totalCount != 3) {
echo "error in totalCount";
exit;
}
///
//TODO: insert test
$db->delete("users");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment