echoing in the view page
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();
.
I would do the following
function getAllByChk()
{
$q = $this->db->get('info');
if ($q->num_rows() > 0)
{
return $data->result();
}
return false;
}
then in the view
if($records) :
foreach($records as $row) :
print_r($row);
endforeach;
else:
echo 'No Records';
endif;
function getAllByChk(){
$data = array();
//remove if
Also, fetch result either as object $q->result()
or echo $row['subject']
Also you can return the result()
function getAllByChk(){
$q = $this->db->get('info');
if ($q->num_rows()>0){
foreach ($q->result_array() as $row)
{
$data[]=$row;
}
return $data->result();
}
}
精彩评论