ds08541
2016-09-20 16:43
浏览 519
已采纳

PHP - Latte模板非对象的foreach属性

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}

图片转代码服务由CSDN问答提供 功能建议

我目前正在使用 Latte 做事。 我无法弄清楚如何在查询中使用foreach循环及其模板变量。 我的代码总是返回尝试获取非对象的属性

  $ query = $ this-&gt; db-&gt; query(  “SELECT id FROM table”); 
 $ array = array(); 
 while($ fetch = $ query-&gt; fetch_array()){
 $ array = $ fetch; 
} 
 
 \  n $ Template [“qclisting”] = $ array; 
   
 
 

模板代码

  {foreach $  qclisting as $ item} 
&lt; a href =“”class =“list-group-item clearfix”&gt; 
&lt; span class =“clear”&gt; 
&lt; span&gt; {$ item-&gt;  id}&lt; / span&gt; 
&lt; / span&gt; 
&lt; / a&gt; 
 {/ foreach} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqby43944 2016-09-20 16:59
    已采纳

    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;
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题