woj*_*jas 5 php foreach smarty
我想创建带有计数器和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}
Run Code Online (Sandbox Code Playgroud)
因此,例如,如果我有4个元素放入foreach输出中,
I am the one
I am second
I am third
I am the one
Run Code Online (Sandbox Code Playgroud)
现在不起作用,我也不知道为什么。有人可以帮助我,告诉我如何解决这个问题?
{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}
Run Code Online (Sandbox Code Playgroud)
这可能有效
尝试像
{if $counter%3 eq 0 && $counter gt 2}
{assign var=counter value=1}
{/if}
Run Code Online (Sandbox Code Playgroud)