Welcome Guest, Not a member yet? Register   Sign In
How do I get the total amount from a column?
#1

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?
Reply
#2

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

Sorry, I still don't get it.
Reply
#4

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.
Reply
#5

(This post was last modified: 01-31-2022, 11:10 AM by ikesela.)

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB