php 消息:尝试在 Codeigniter 中获取非对象的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22354514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Message: Trying to get property of non-object in Codeigniter
提问by Kino
I have this code in my Model page:
我的模型页面中有此代码:
public function getAll($researcherpk){
$this->db->select('*');
$this->db->from('research');
$this->db->join('researcher', 'researcher = lastname');
$this->db->where('researcherpk', $researcherpk);
return $this->db->get()->row_array();
But when I use it on my View Page
但是当我在查看页面上使用它时
Age: <?php echo $data['age'];?> // this work
<?php foreach ($data as $v){ ?> //this has an error
<tr>
<td><?php echo $v->title?></td>
<td><?php echo $v->track_records?></td>
</tr>
<?php } ?>
I get the error:
我收到错误:
Message: Trying to get property of non-object
In my Controller page:
在我的控制器页面中:
public function researcher($researcherpk){
$this->load->model('ResearchModel');
$result['data'] = $this->ResearchModel->getAll($researcherpk);
$this->load->view('researcher',$result);
}
What do you think is the problem here? What is my alternative solution for it or changes?
你认为这里的问题是什么?我的替代解决方案或更改是什么?
回答by Albzi
Change your return to:
将您的退货更改为:
return $this->db->get()->result_array();
Then try using $v['title']and $v['track_records']
然后尝试使用$v['title']和$v['track_records']
<?php foreach ($data as $v): ?>
<tr>
<td><?php echo $v['title']?></td>
<td><?php echo $v['track_records']?></td>
</tr>
<?php endforeach; ?>
Look at the codeigniter docsto learn more.
查看codeigniter 文档以了解更多信息。
result_array():
result_array():
This function returns the query result as a pure array
该函数以纯数组形式返回查询结果
That is what you want.
这就是你想要的。
You are mixing it with:
你将它与:
row_array():
row_array():
This function returns a single result row. If your query has more than one row, it returns only the first row.
此函数返回单个结果行。如果您的查询有多于一行,则它只返回第一行。
This is not what you want.
这不是你想要的。
回答by Rocket Hazmat
You're calling row_arraythis means $datais an arrayof a single row. $data['age']works, because ageis a field in the row.
您正在调用row_array这意味着$data是一个单行数组。 $data['age']有效,因为age是行中的一个字段。
You can foreach($data as $v), but this will only loop over the fieldsin that particular row.
您可以foreach($data as $v),但这只会遍历该特定行中的字段。
You can just do echo $data['title']. $datais not a multidimensional array.
你可以做echo $data['title']。 $data不是多维数组。
If you want multiple rows, then use return $this->db->get()->result_array();. Then you can loop over the rows:
如果您想要多行,请使用return $this->db->get()->result_array();. 然后你可以遍历行:
foreach ($data as $v){
echo $v['age'];
echo $v['title'];
}
回答by Daan
It is an array not an object so:
它是一个数组而不是一个对象,所以:
Change this:
改变这个:
<td><?php echo $v->title?></td>
<td><?php echo $v->track_records?></td>
To this:
对此:
<td><?php echo $v['title']; ?></td>
<td><?php echo $v['track_records']; ?></td>

