CodeIgniter Forums
How do I get the total amount from a column? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How do I get the total amount from a column? (/showthread.php?tid=81172)



How do I get the total amount from a column? - castle - 01-31-2022

Hi,

I'm upgrading my CI3 model to CI4, but things are not working well.

CI 3
PHP Code:
// Sales
 
$this->db->select_sum('total');
 
$result $this->x_model->get('order', array('created_on BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()' => null));
 
$data['sales_count'] = $result->total

CI 4
PHP Code:
  public function total_weekly_sales()
    {
        return $this->db->table('order')
        ->selectSum('total')
        ->where("created_on BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()")
        ->get();
    
It doesn't work. It gives me
Code:
Object of class CodeIgniter\Database\MySQLi\Result could not be converted to string.
Any idea where am I doing wrong?


RE: How do I get the total amount from a column? - iRedds - 01-31-2022

The get() method returns an instance of the Result class.
Generating Query Results


RE: How do I get the total amount from a column? - castle - 01-31-2022

Sorry, I still don't get it.


RE: How do I get the total amount from a column? - Chroma - 01-31-2022

You are getting an object of Type Result, is what @iRedds is saying.

That implies you will not be able to convert it to a string like that, because it is a class or a complex set of fields.


RE: How do I get the total amount from a column? - ikesela - 01-31-2022

(01-31-2022, 05:28 AM)castle Wrote: Hi,

I'm upgrading my CI3 model to CI4, but things are not working well.

CI 3
PHP Code:
// Sales
 
$this->db->select_sum('total');
 
$result $this->x_model->get('order', array('created_on BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()' => null));
 
$data['sales_count'] = $result->total

CI 4
PHP Code:
  public function total_weekly_sales()
    {
        return $this->db->table('order')
        ->selectSum('total')
        ->where("created_on BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()")
        ->get();
    
It doesn't work. It gives me
Code:
Object of class CodeIgniter\Database\MySQLi\Result could not be converted to string.
Any idea where am I doing wrong?

Code:
->get()->getResult() // return array of object
->get()->getResultArray() // return array