gpt4 book ai didi

php - Smarty foreach 计数器,在 3 个元素后重置

转载 作者:可可西里 更新时间:2023-11-01 13:54:00 27 4
gpt4 key购买 nike

我想创建带有计数器和 3 个“if”条件的 foreach smarty 循环。在我的计数器值超过 3 之后,我想重置计数器值并返回到 If 的第一个条件

这是我的代码

{foreach $itemscollection as $singleitem name=smartyloop}
{assign var="counter" value=$smarty.foreach.smartyloop.iteration}

{if $counter == 1}
<h1>I am the one</h1>
{/if}
{if $counter == 2}
<h1>I am second</h1>
{/if}
{if $counter == 3}
<h1>I am third</h1>
{/if}
{if $counter > 3}
{$counter = 1}
{/if]

{/foreach}

例如,如果我有 4 个元素要放入 foreach 输出应该如下所示

I am the one
I am second
I am third
I am the one

现在它不工作了,我不知道为什么。有人可以帮助我并告诉我如何解决该问题吗?

最佳答案

{assign var=counter value=1}
{foreach $itemscollection as $singleitem name=smartyloop}
{if $counter == 1}
<h1>I am the one</h1>
{/if}
{if $counter == 2}
<h1>I am second</h1>
{/if}
{if $counter == 3}
<h1>I am third</h1>
{/if}
{if $counter > 3}
{assign var=counter value=1}
{/if]

{$counter++}
{/foreach}

这可能有用

关于php - Smarty foreach 计数器,在 3 个元素后重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26688269/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com