0

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}
P. Nick
  • 955
  • 1
  • 12
  • 33
  • For now, please, try to dump your variable like this `{dump $qclisting}` - before foreach cycle - and see what your debug tracy console returns – pedrouan Sep 20 '16 at 16:51
  • @pedrouan It returns: array (1) 0 => array (2) 0 => "1" id => "1" – P. Nick Sep 20 '16 at 16:57

1 Answers1

1

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;
Dave
  • 822
  • 1
  • 7
  • 17
  • Updated my answer. It would appear based on comments on your question that you also have an extra dimension in your $qclisting array. – Dave Sep 20 '16 at 17:03
  • @P.Nick Well I thought you are coding in nette database, so it couldn't be possible to access it like this though. It's simple solution, for sure. – pedrouan Sep 20 '16 at 17:03
  • @pedrouan My bad for not informing you. However, my code only shows the first database row for some reason... help? (op updated) – P. Nick Sep 20 '16 at 18:06
  • Since you've already accepted answer I assume you want to post another question as this is another point. In tour question don't forget to mention database layer you use. – pedrouan Sep 20 '16 at 18:12
  • Apologies. Your code is not indented in your example and I mis-read it. Add back the [] in line 4 of your code: $array[] = $fetch; – Dave Sep 22 '16 at 00:25