Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter count_all_results

I'm working with the latest codeIgniter released, and i'm also working with jquery datatables from datatables.net

I've written this function: https://gist.github.com/4478424 which, as is works fine. Except when I filter by using the text box typing something in. The filter itself happens, but my count is completely off.

I tried to add in $res = $this->db->count_all_results() before my get, and it stops the get from working at all. What I need to accomplish, if ($data['sSearch'] != '') then to utilize the entire query without the limit to see how many total rows with the search filter exists.

If you need to see any other code other than whats in my gist, just ask and I will go ahead and post it.

like image 406
Johnny Avatar asked Jan 07 '13 21:01

Johnny


5 Answers

Count first with no_reset_flag.

$this->db->count_all_results('', FALSE);
$rows = $this->db->get()->result_array();

system/database/DB_query_builder.php

public function count_all_results($table = '', $reset = TRUE) { ... }
like image 192
Jehong Ahn Avatar answered Nov 02 '22 05:11

Jehong Ahn


$this->db->count_all_results() replaces $this->db->get() in a database call.

I.E. you can call either count_all_results() or get(), but not both.

You need to do two seperate active record calls. One to assign the results #, and one to get the actual results.

Something like this for the count:

$this->db->select('id');
$this->db->from('table');
$this->db->where($your_conditions);
$num_results = $this->db->count_all_results();

And for the actual query (which you should already have):

$this->db->select($your_columns);
$this->db->from('table');
$this->db->where($your_conditions);
$this->db->limit($limit);
$query = $this->db->get();
like image 28
Brendan Avatar answered Nov 02 '22 06:11

Brendan


Have you read up on https://www.codeigniter.com/userguide2/database/active_record.html#caching ?

I see you are trying to do some pagination where you need the "real" total results and at the same time limiting.

This is my practice in most of my codes I do in CI.


    $this->db->start_cache();

    // All your conditions without limit
    $this->db->from();
    $this->db->where(); // and etc...
    $this->db->stop_cache();

    $total_rows = $this->db->count_all_results(); // This will get the real total rows

    // Limit the rows now so to return per page result
    $this->db->limit($per_page, $offset);
    $result = $this->db->get();

    return array(
        'total_rows' => $total_rows,
        'result'     => $result,
    ); // Return this back to the controller.

I typed the codes above without testing but it should work something like this. I do this in all of my projects.

like image 44
Kong Jin Jie Avatar answered Nov 02 '22 05:11

Kong Jin Jie


You dont actually have to have the from either, you can include the table name in the count_all_results like so.

$this->db->count_all_results('table_name');
like image 4
DevNinjaJeremy Avatar answered Nov 02 '22 05:11

DevNinjaJeremy


The

$this->db->count_all_results();

actually replaces the:

$this->db->get();

So you can't actually have both.

If you want to do have both get and to calculate the num rows at the same query you can easily do this:

$this->db->from(....);
$this->db->where(....);
$db_results = $this->get();

$results = $db_results->result();
$num_rows = $db_results->num_rows();
like image 1
John Skoumbourdis Avatar answered Nov 02 '22 07:11

John Skoumbourdis