Smarty foreach counter, reset after 3 elements

I want to create foreach smarty loop with counter and 3 "if" conditions inside. After my counter value is greater than 3, I want to reset the counter value and return to the first condition If

This is my code.

{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}

So, for example, if I have 4 elements to place in foreach, the output should look like

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

Now it does not work, and I do not know why. Can someone please help me and tell me how to solve this problem?

+4
source share
6 answers

Try

{if $counter%3 eq 0 && $counter gt 2}
    {assign var=counter value=1}
{/if}
+1
source
{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}

it can work

+1
source

, , smarty:

{foreach $itemscollection as $singleitem name=smartyloop}
    {counter assign='pos'} {* assign counter to variable *}

    {if $pos == 1}
        <h1>I am the one</h1>
    {/if}
    {if $pos == 2}
        <h1>I am  second</h1>
    {/if}
    {if $pos == 3}
        <h1>I am  third</h1>
        {counter start=0} {*reset counter*}
    {/if}
{/foreach}

CMS Made Simple, - ;)

+1

, , , foreach .

  {foreach $blogscollection as $singleblog name=smartyloop}
                {assign var="counter" value=$smarty.foreach.smartyloop.iteration}
                {if $counter>3}
                    {assign var=counter value=1}
                {/if}
                {if $counter == 1}
                    <h1>I am the one</h1>
                {/if}
                {if $counter == 2}
                    <h1>I am the second</h1>
                {/if}
                {if $counter == 3}
                    <h1>I am the third</h1>
                {/if}
            {/foreach}
0
<?php
$array=array(' the ','hap',' pop' ,' grand'); // assume this is your array to pass in for   each
$says=array(' the one','second',' third');  // store your word to appear
$count=0;
foreach ($array as $arr){
if($count == 3) $count=0;
 print "<h1>I am $says[$count]<h1>";
$count++;
}
?>
0
source

You can use the {cycle} http://www.smarty.net/docs/en/language.function.cycle.tpl

{cycle name="counter" values="1,2,3"}

Or you can use the module operator (%) http://php.net/manual/en/language.operators.arithmetic.php

{$counter = ($smarty.foreach.smartyloop.iteration % 3) + 1}
0
source

All Articles