Reputation: 15700
I have a ci / bonfire application. I'm trying to loop through results of a query. However, the logic in my view that loops through the records and their individual fields fails with the message that I'm trying to get a property of a non-object. I've made sure that in my model, I only return data if I have something to return... like so:
if ($query->num_rows() > 0)
{
return $query->result_array(); //returns an object of data
}
In the view, I've also added the following debug statement to prove that I'm getting data back:
print_r($records);
That gives me the following output: (I've only included a subset to be brief..)
Array ( [0] => Array ( [category_id] => 59 [name] => procs [description] => [parent_id] => 4 [deleted] => 0 [created_on] => 2012-09-28 00:00:00 [modified_on] => 0000-00-00 00:00:00 ) [1] => Array ( [category_id] => 62 [name] => test [description] => [parent_id] => 4 [deleted] => 0 [created_on] => 2012-09-28 00:00:00 [modified_on] => 0000-00-00 00:00:00 )
And finally, here's the logic (again, only subset) I use to loop through $records:
<?php foreach ($records as $record) : ?>
<tr>
<td><input type="checkbox" name="checked[]" value="<?php echo $record->category_id ?>" /></td>
It's dying on the call to $record->category_id
EDIT 1
The index() method my controller that bonfire created to return all records from the same table, successfully uses the syntax $record->category_id
in the view. I know that I can use $record['category_id']
but I'm wondering why I can't get it to work the other way.
Any suggestions would be appreciated. Thanks.
Upvotes: 0
Views: 195
Reputation: 1754
I do not think you are getting an object (the print_r would have been different otherwise). That said, the 'foreach' method is used for array traversal, not object, even if a object is supplied to the 'foreach' loop, it is converted to equivalent associative array, so, in your case '$record' is no longer an object, but an associative array.
Upvotes: 1