Welcome Guest, Not a member yet? Register   Sign In
help noob with select_sum
#1

[eluser]xxIdroxx[/eluser]
Hello everyone, I'm a beginner with CodeIgniter and I need to select a field to a table and add up all the data I can find.
I tried to do this script but it goes:
Code:
$query = $this->db->get_where($this->session->userdata('universe').'_alleanza_utenti', array('ally_id' => $param5));
     $this->db->select_sum('points');
  foreach ($query->result_array() as $row)
{
    $query1 = $this->db->get_where($this->session->userdata('universe').'_users', array('id' => $row['user_id']));
}
  $row = $query1->row();
  
     echo number_format($row->points);
#2

[eluser]vbsaltydog[/eluser]
From the user guide:

Quote:
Code:
$this->db->select_sum('age');
$query = $this->db->get('members');
// Produces: SELECT SUM(age) as age FROM members

You are calling the select_sum() method of the db class after you run your query using get_where() which is wrong.

#3

[eluser]xxIdroxx[/eluser]
how do I do?
#4

[eluser]vbsaltydog[/eluser]
I don't do it for you, I only point the way.

Look into OOP basics and the answer will be clear.
#5

[eluser]xxIdroxx[/eluser]
I followed the user guide but I can not do the sum runs
#6

[eluser]xxIdroxx[/eluser]
up help me please
#7

[eluser]smilie[/eluser]
Just read once more what vbsaltydog said;

Let me give you a hint:

[quote author="vbsaltydog" date="1327693579"]From the user guide:

Quote:
Code:
$this->db->select_sum('age');
$query = $this->db->get('members');
// Produces: SELECT SUM(age) as age FROM members

You are calling the select_sum() method of the db class after you run your query using get_where() which is wrong.

[/quote]

So, hints:
- after;
- code vbsaltydog already posted from user guide...

MegaHint of the day: 'before' ;-)

Cheers,
Smilie
#8

[eluser]xxIdroxx[/eluser]
i have tried but it doesn't work if i use where or get_where
#9

[eluser]Bhashkar Yadav[/eluser]
simple way, you can pass your whole query into $this->db->query("SELECT ......."); like :
Code:
$rs = $this->db->query("SELECT .......");
foreach($rs->result_array() as $row){
.......
.......
}




Theme © iAndrew 2016 - Forum software by © MyBB