【问题标题】:[Smarty, and maybe doctrine][Smarty, and maybe doctrine]
【发布时间】:2010-10-19 13:39:37
【问题描述】:

我在 smarty 中使用 foreach 时遇到问题, 一个包含 2 项的数组在 foreach 中循环,但结果是循环了 3 次。

我使用教义从数据库中获取用户的评论列表

Doctrine_Core::getTable('review')->findByUser($userId);

然后我将它分配给 smarty 并在 foreach 中循环:

{foreach from=$reviewList item=review}

  <p>User {$review.User.name} said: {$review.content}</p>

{/foreach}

但是结果是例如:

用户乔说:哟哟

用户玛丽说:嗨

用户说:

请注意,多余的行不会从数组中得到任何东西。

我检查了数据库中只有2条记录,并且我通过count($reviewList)计算了$reviewList,结果也是2。 当我在数据库中再插入一条记录时,forloop 也会额外循环一次。 谁能告诉我为什么会这样?非常感谢!

【问题讨论】:

    标签: doctrine foreach smarty


    【解决方案1】:

    这应该过滤空行:

    {foreach from=$reviewList item=review}{if $review.User.name}
      <p>User {$review.User.name} said: {$review.content}</p>
    {/if}{/foreach}
    

    【讨论】:

      猜你喜欢
      • 2022-12-27
      • 2016-02-03
      • 2013-09-09
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多