P. Nick P. Nick - 2 months ago 6
PHP Question

PHP - Latte template foreach property of non-object

I'm currently using Latte to do things.
What I can't figure out is how to use a foreach loop on a query with it's template variables. My code below would always return in

Trying to get property of non-object


$query = $this->db->query("SELECT id FROM table");
$array = array();
$fetch = $query->fetch_array();
$array[] = $fetch;

$Template["qclisting"] = $array;


And the template code

{foreach $qclisting as $item}
<a href="" class="list-group-item clearfix">
<span class="clear">
<span>{$item->id}</span>
</span>
</a>
{/foreach}

Answer

In your template, use

$item['id']

...instead of...

$item->id

And, remove the [] from line 4 of your code:

$array = $fetch;
Comments