回显视图页面 [英] echoing in the view page
问题描述
我试图在视图中回显我数据库的一个字段,但我收到错误:
消息:尝试获取非对象的属性
文件名:views / admin_view_report.php
行号:7
当我回显整个数组时,它工作得很好。我找不到错误。
我的控制器的一部分:
function index(){
$ this-> load-> model('viewreport');
$ data ['records'] = $ this-> viewreport-> getAllByChk();
$ this-> load-> view('admin_view_report',$ data);
}
我的模型的一部分:
function getAllByChk(){
$ q = $ this-> db-> get('info');
if($ q-> num_rows()> 0){
foreach($ q-> result_array()as $ row)
{
$ data [] = $ row;
}
return $ data;
}
}
视图:
<?php foreach($ records as $ row):?>
<?php echo $ row-> subject; >
<?php endforeach;?>
如果我只打印 data ['records']
它给出以下输出
Array
(
[0] =& $ b(
[id] => 1
[address] => 11 / siddeshwari
[area] => sid
[lat] =& b $ b [lng] => 21
[subject] => hello
[问题] =>大量问题
[image] =>
[time ] => 2011-08-11 23:49:29
[register_id] => 1
[category_id] => 1
[city_city_id] => 1
[status_status_id] => 0
)
[1] => Array
(
[id] => 2
[address ] => 134 banani
[area] => banai
[lat] => 1223
[lng] => 2133
[subject] => not working
[problem] => yesproblem问题问题
[image] =>
[time] => 2011-08-12 01:09:44
[register_id] => 1
[category_id] => 2
[city_city_id] => 1
[status_status_id] => 0
)
)
<$ p $ c> result_array()
$在您的视图中:
code> $ row-> subject
应为:
$ row ['subject']
$ b
如果你想要$ row为一个对象,将 result_array()
更改为 result()
。这将给你一个对象数组。
也 foreach($ q-> result_array()as $ row)$ c $
只是将 $ q-> result_array()
复制到 $ data
。
你不需要这样做,只需 return $ q-> result_array );
。
I'm trying to echo a field of my database in the view, but I'm getting the error:
Message: Trying to get property of non-object
Filename: views/admin_view_report.php
Line Number: 7
When I echo the whole array, it works just fine. I can't find what's wrong.
Part of my controller:
function index(){
$this->load->model('viewreport');
$data['records']=$this->viewreport->getAllByChk();
$this->load->view('admin_view_report',$data);
}
Part of my model:
function getAllByChk(){
$q = $this->db->get('info');
if ($q->num_rows()>0){
foreach ($q->result_array() as $row)
{
$data[]=$row;
}
return $data;
}
}
The view:
<?php foreach($records as $row):?>
<?php echo $row->subject; ?>
<?php endforeach;?>
If I only print data['records']
it gives the following output
Array
(
[0] => Array
(
[id] => 1
[address] => 11/siddeshwari
[area] => sid
[lat] => 21
[lng] => 21
[subject] => hello
[problem] => lots of problem
[image] =>
[time] => 2011-08-11 23:49:29
[register_id] => 1
[category_id] => 1
[city_city_id] => 1
[status_status_id] => 0
)
[1] => Array
(
[id] => 2
[address] => 134 banani
[area] => banai
[lat] => 1223
[lng] => 2133
[subject] => not working
[problem] => yesproblem problem problem
[image] =>
[time] => 2011-08-12 01:09:44
[register_id] => 1
[category_id] => 2
[city_city_id] => 1
[status_status_id] => 0
)
)
but when I try to print the problem or subject only it gives the error.
result_array()
returns you an array of arrays, not of objects.
In your view:
$row->subject
should be:
$row['subject']
If you want $row to be an object, change result_array()
to result()
. This will give you an array of objects.
Also foreach ($q->result_array() as $row)
is redundant.
It's just copying $q->result_array()
to $data
.
You don't need to do that, just return $q->result_array();
.
这篇关于回显视图页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!