Welcome Guest, Not a member yet? Register   Sign In
Counting items in table via native count function fails?
#1

[eluser]nagata[/eluser]
So I have a forum on my website, all works fine except post counter code,
this code for some reason keeps returning 1...
Code:
function count_user_posts($id)
{
     $this->load->database();
  $this->db->from('messages');
  $this->db->where('user_id', $id);
  $postcount = $this->db->get();
  $postcount = count($postcount);
        return $postcount;
}
  
  function count_user_topics($id)
{
     $this->load->database();
  $this->db->from('topics');
  $this->db->where('user_id', $id);
  $postcount = $this->db->get();
  $postcount = count($postcount);
        return $postcount;
}
the bit I use in profile page to show users post count:
Code:
$user = $this->access->get_user();
echo '<br>Your Postcount is '. $user->posts .'(Post:'.$user->posts .'/Topic'.$user->topics .')';
and it echos "Your Postcount is 1(Post:1/Topic1)"
and for some reason it dosent change, I made test acc with NO posts made yet,
and it still was showing 1 post...
also this is access bit that returns the code:
Code:
function get_user()
{  
  if ( ! $this->logged_in())
  {
   $user   = new stdClass;
   $user->id  = -1;
   $user->is_admin = FALSE;
  }
  else
  {
   $user = $this->CI->users_model->get_user($this->CI->session->userdata('id'));
  
   $user->id = $user->user_id;
   $user->is_admin = ($user->user_group === 'Administrators') ? TRUE : FALSE;
   $user->confirm_logout = $this->CI->config->item('logout_confirmation');
   $user->postcount = $this->CI->users_model->count_user_posts($user->id)+$this->CI->users_model->count_user_topics($user->id);
   $user->posts = $this->CI->users_model->count_user_posts($user->id);
   $user->topics = $this->CI->users_model->count_user_topics($user->id);
  }
  
  return $user;
}
#2

[eluser]InsiteFX[/eluser]
Code:
$this->db->count_all();

CodeIgniter Users Guide - Query Helper Functions
#3

[eluser]nagata[/eluser]
Code:
function count_user_posts($id)
{
     $this->load->database();
  $this->db->from('messages');
  $this->db->where('user_id', $id);
  $postcount = $this->db->count_all();  //$this->db->get();
  //$postcount = $this->db->count_all(); //count($postcount);
        return $postcount;
}
  
  function count_user_topics($id)
{
     $this->load->database();
  $this->db->from('topics');
  $this->db->where('user_id', $id);
  $postcount = $this->db->count_all(); //$this->db->get();
  //$postcount = $this->db->count_all(); //count($postcount);
        return $postcount;
}
changed it to this as you said, now it shows "Your Postcount is 0(Post:0/Topic0)"
atleast something changed.... now it dosent see them XD
#4

[eluser]InsiteFX[/eluser]
Your missing your
Code:
$query = $this->db->get('table_name');

// your code should be
function count_user_posts($id)
{
  $this->load->database();

  $this->db->where('user_id', $id);
  $query = $this->db->get('messages');
  
  if ($query->num_rows() > 0)
  {
    return $query->row_array();
  }

  return FALSE;
}

// then in your controller
$cnt = count('returned query');

#5

[eluser]nagata[/eluser]
your code didnt help but gave an idea of how to do it,
so I wrote this, it output exectly what i wanted, user post count Smile
so if anyone ever would need help with counting total posts, here ya go.
Code:
function count_user_posts($id)
{
  $this->load->database();

  $this->db->where('user_id', $id);
  $query = $this->db->get('messages');
  return $query->num_rows();
}

function count_user_topics($id)
{
  $this->load->database();

  $this->db->where('user_id', $id);
  $query = $this->db->get('topics');
  return $query->num_rows();

}
#6

[eluser]vitoco[/eluser]
(I'm not trying to be a dick about it ) Sometimes getting the desire result isn't enough, especially in apps constantly querying databases.

Some pointers :
- get ONLY what you need
- try to avoid getting what you don't need

From your code, you need only the NUMBER of rows that match your criteria, in this case with the id_user = $id, so by doing this :

Code:
$this->db->where('user_id', $id);
  $query = $this->db->get('messages');
  return $query->num_rows();

1.- you're getting all fields, because there's an implicit "SELECT *", i guess that includes some text fields.
2.- you're getting all records with user_id , that can be slow and doesn't make much sense.
3.- after that you count the records ( maybe a pre-calculated value, but i think that maybe with some iterations ).

That's not right.

Instead, use a simple BUILT-IN function ( that most RDBMS support ) "COUNT(*|field)" that returns an INT , also, try to especify the field because there are some exceptions counting NULL values.

Code:
$this->db->select('COUNT( user_id ) AS number');
$this->db->where('user_id', $id);
$query = $this->db->get('messages');
$row = $query->row_array();
$query->free_result();

return $row['number'] ;

Hope it helps.
Saludos




Theme © iAndrew 2016 - Forum software by © MyBB