Illegal string offset 'order_status_id' in

2019-08-30 04:42发布

I am getting error Illegal string offset 'order_status_id' when I want to get loop data in view

Here's the code:

controller.php

if (isset($_POST["email"])) {
    $email = $_POST["email"];
}

$this->load->model('order/myorder');
$data['data1'] = $this->model_order_myorder->getOrder($email) ;

view.php

foreach ($data1 as $row) {
    echo echo $row->order_id;
}

model.php

class ModelOrderMyorder extends Model {

    public function getOrder($email) {

        $sql = "SELECT * FROM ".DB_PREFIX."order, ".DB_PREFIX."order_product WHERE ".DB_PREFIX."order.email = '$email'";
        $query = $this->db->query($sql);

        return $query ;
    }

}

Still not getting it showing Trying to get property of non-object in view.

1条回答
做自己的国王
2楼-- · 2019-08-30 05:01

First off, if you want to iterate through all the order products for a given email (which is what I think you want) you should change the getOrder() method to return:

 return $query->rows;

Then in the controller you need to change:

$data['data1'] = $this->model_order_myorder->getOrder($email) ;

to

$this->data['data1'] = $this->model_order_myorder->getOrder($email);

Finally, in your view, you'll be accessing an array not an object so you should lose the extra echo (assuming this is a typo) and change:

echo   echo $row->order_id;

and get the index as:

echo $row['order_id']

Also, in addition to the above, I'll suggest you utilize some of the methods and code conventions found in Opencart:

  • When accessing the $_POST global you can use the sanitized version $this->request->post

  • Your query fails to backtick the order table which can result in errors in you didn't set a prefix. And you are not escaping $email which is a good idea for a number of reasons. Also, it makes things easy if you give your tables an alias. Finally, a join on the tables... so I might consider rewriting that query like this:

    $sql = "SELECT * FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_product op USING (order_id) WHERE o.email = '" . $this->db->escape($email) . "'";

To be honest, I'm not sure what results you're expecting from that query but bear in mind that if there are multiple products for an given order you will end up with multiple rows returned.

Just a few tips.. hopefully this is useful to you.

查看更多
登录 后发表回答