From 694b5b8ee6a40b57c91be3c5448bc8f5540d32d8 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Tue, 18 Dec 2007 15:58:03 +0000 Subject: Added count_all_results() function to Active Record. --- system/database/DB_active_rec.php | 41 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 6a991a2ed..cb134ea6a 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -540,6 +540,7 @@ class CI_DB_active_record extends CI_DB_driver { * and runs the query * * @access public + * @param string the table * @param string the limit clause * @param string the offset clause * @return object @@ -565,6 +566,39 @@ class CI_DB_active_record extends CI_DB_driver { // -------------------------------------------------------------------- + /** + * "Count All Results" query + * + * Generates a platform-specific query string that counts all records + * returned by an Active Record query. + * + * @access public + * @param string + * @return string + */ + function count_all_results($table = '') + { + if ($table != '') + { + $this->from($table); + } + + $sql = $this->_compile_select($this->count_string); + + $query = $this->query($sql); + $this->_reset_select(); + + if ($query->num_rows() == 0) + { + return '0'; + } + + $row = $query->row(); + return $row->numrows; + } + + // -------------------------------------------------------------------- + /** * Get_Where * @@ -806,12 +840,17 @@ class CI_DB_active_record extends CI_DB_driver { * @access private * @return string */ - function _compile_select() + function _compile_select($select_override = FALSE) { $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT '; $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select); + if ($select_override !== FALSE) + { + $sql = $select_override; + } + if (count($this->ar_from) > 0) { $sql .= "\nFROM "; -- cgit v1.2.3-24-g4f1b