CodeIgniter Forums
Can someone help me understand count_all_results? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Can someone help me understand count_all_results? (/showthread.php?tid=22861)



Can someone help me understand count_all_results? - El Forum - 09-22-2009

[eluser]Unknown[/eluser]
So, what exactly does count_all_results return? I was doing a test to try and nail it down, and I'm stumped.

I have a table called "widgets" that has 2 rows in it.

If I do this:
Code:
$this->db->cache_off();   // Just in case.
$query = $this->db->get('widgets');
print($this->db->count_all_results());
print($query->num_rows());
print_r($query->result());

...I get that count_all_results() returns "1", but num_rows() returns "2" and result() returns two objects.

What exactly is count_all_results() returning, here? Why is it one and not two?

The reason I ask is that I'm trying to paginate a search of widgets, and it seems like count_all_results() should return the value that I want to drop into the paginate class for total records, but it doesn't seem to be doing that.


Can someone help me understand count_all_results? - El Forum - 09-22-2009

[eluser]daparky[/eluser]
From what i gather, num_rows is how many rows are in a table no matter what query you are performing and count_all_results returns the number of rows in a query with other db queries such as like and where.

Do something like this:-

Code:
$this->db->get('widgets');
$count = $this->db->count_all_results();

Or

Code:
$this->db->like('name', $letter, 'after');
$this->db->from('clients');
$count = $this->db->count_all_results();

You will then use $count for the pagination. The only problem with this is that you are going to have to run the same query twice, one for the view and the other to count for the pagination.


Can someone help me understand count_all_results? - El Forum - 09-22-2009

[eluser]BrianDHall[/eluser]
Code:
/**
     * "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->_track_aliases($table);
            $this->from($table);
        }
        
        $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));

        $query = $this->query($sql);
        $this->_reset_select();
    
        if ($query->num_rows() == 0)
        {
            return '0';
        }

        $row = $query->row();
        return $row->numrows;
    }

What does that mean? Beats the hell out of me. I thought it might help, but I don't immediately see the difference.