不能在codeigniter中使用stdClass类型的对象作为数组

时间:2015-11-24 09:08:15

标签: codeigniter

我的控制器代码

function edtpost($id)
{
    $this->load->model('post');
    $data = $this->post->edt_post($id);

    $this->load->model('category');
    $data['catname'] = $this->category->retrivecat();

    $this->load->view('dashboard/edit_post',$data);
}

我的型号代码

模特帖

public function edt_post($id)
{
    $query = $this->db->get_where('post', array('id' => $id));
    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            return $row;
        }
    }
    return false;
}

类别模型

public function retrivecat()
{
    $query = $this->db->get('category');
    foreach ($query->result() as $row)
    {
        $data[] = $row->catname;
    }
    return $data;
}

在控制器&此代码$data['catname'] = $this->category->retrivecat();

我有一个错误:

Cannot use object of type stdClass as array

1 个答案:

答案 0 :(得分:0)

只需更改以下内容: -

$data->catname = $this->category->retrivecat();