phpnettelatte

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();
    while($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}

Solution

  • In your template, use

    $item['id']
    

    ...instead of...

    $item->id
    

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

    $array = $fetch;
    

    UPDATE:

    If you are only seeing the first row of your output, then I was wrong. Add back the [] in the fourth line of your code:

    $array[] = $fetch;