OpenCart中的非法字符串偏移量'order_status_id' [英] Illegal string offset 'order_status_id' in opencart

查看:193
本文介绍了OpenCart中的非法字符串偏移量'order_status_id'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在视图中获取循环数据时出现错误Illegal string offset 'order_status_id'

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

这是代码:

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.

推荐答案

首先,如果您要遍历给定电子邮件的所有订购产品(我想您想要的),则应更改getOrder()返回方法:

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) ;

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

最后,在您看来,您将访问一个 array 而不是一个 object ,因此您应该丢失多余的echo(假设这是一个错字),并且更改:

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;

并获取索引为:

echo $row['order_id']

此外,除上述内容外,我建议您利用Opencart中的一些方法和代码约定:

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

  • 访问全局$_POST时,可以使用已清理的 版本$this->request->post

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

您的查询未能重新勾选 order 表,这可能会导致 错误中您没有设置前缀.而且你没有逃脱 $email出于多种原因,这是一个好主意.而且,它使 如果给表起别名,这很容易.最后,加入 表...,所以我可能考虑像这样重写该查询:

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.

这篇关于OpenCart中的非法字符串偏移量'order_status_id'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆