CodeIgniter Forums
Getting the sum - 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: Getting the sum (/showthread.php?tid=47735)



Getting the sum - El Forum - 12-20-2011

[eluser]Unknown[/eluser]
I'm tryna get the sum of each counted records but have no luck

Code:
function counter() {
for ($i=1; $i<=10; $i++) {
      $data['a'] = $this->input->post('i'.$i);
      $data['b'] = $this->input->post('answer'.$i);
      
      $ck_answers = $this->Profile_model->ck_answers($data);
      
      $sum = 0;
      if(count($ck_answers) > 0):
       foreach($ck_answers as $c):
        $sum += $c->x;
       endforeach;
      endif;
      
     }
}



function ck_answers($data)
    {
    
     for($i=0; $i<=10; $i++) {
      $d = $data['b'];
      $item = $data['a'];
      $a = $this->input->post('cid');
      
      $query = $this->db->query("SELECT COUNT(*) as x FROM table where colA = $a AND colB = $item AND colC = '$d'");
    
      return $query->result();
     }
    }


When I tried to echo it echoes the row not the sum. Please help