Welcome Guest, Not a member yet? Register   Sign In
Basic question about count_all_results()
#1

[eluser]Unknown[/eluser]
Hello everybody,

I have a simple question about the count_all_results method of Active Record. I have 2 entries in my table (returned by the following query):

Code:
$data['query'] = $this->db->get_where('produits', array('categories_id' => $this->uri->segment(3)));

And, when I put the total in the following variable:

Code:
$data['total'] = $this->db->count_all_results();

I'm always getting 1 instead of 2. Do you guys have an idea?

Thanks.
#2

[eluser]bretticus[/eluser]
Would your where clause that specifies a single categories_id not return a single result???

Perhaps you meant...

Code:
$data['total'] = $this->db->count_all_results('produits');

...or...

Code:
$data['total'] = $this->db->count_all('produits');
#3

[eluser]TheFuzzy0ne[/eluser]
I'm surprised you're getting anything at all. I think you might find this works as expected:
Code:
$where = array('categories_id' => $this->uri->segment(3));
$data['query'] = $this->db->get_where('produits', $where);

$this->db->where($where);
$data['total'] = $this->db->count_all_results('produits');
#4

[eluser]Jailbird[/eluser]
Code:
$data['query'] = $this->db->where('categories_id',$this->uri->segment(3));
$data['query'] = $this->db->from('produits');    
$data['total'] = $this->db->count_all_results();
#5

[eluser]TheFuzzy0ne[/eluser]
Uhm... Other than the error in the top line of code, how is that any different from what the OP is doing? Tongue
#6

[eluser]Jailbird[/eluser]
Correct me if I'm wrong, but up to my knowledge,
Code:
$data['query'] = $this->db->where('categories_id',$this->uri->segment(3));
$data['query'] = $this->db->from('produits');  
$data['total'] = $this->db->count_all_results();
or
Code:
$data['query'] = $this->db->where('categories_id',$this->uri->segment(3));
$data['total'] = $this->db->count_all_results('produits');
produces the following query:
Code:
SELECT COUNT(*) AS `numrows` FROM (`produits`) WHERE `categories_id` = '$this->uri->segment(3)'
with correctly counted rows.

Whereas the OP's,
Code:
$data['query'] = $this->db->get_where('produits', array('categories_id' => $this->uri->segment(3)));
$data['total'] = $this->db->count_all_results();
results in:
Code:
SELECT * FROM (`produits`) WHERE `user_type_id` = '$this->uri->segment(3)'

Seems like a working solution, but might be I'm not using a pattern efficient enough?




Theme © iAndrew 2016 - Forum software by © MyBB