9

I want to create foreach smarty loop with counter and 3 "if" conditions inside. After my counter value is more than 3 I want to reset counter value and get back to first condition of 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 into foreach output should look like

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

Now it's not working and i don't know why. Can somebody please help me and tell how to resolve that problem ?

woj_jas
  • 1,092
  • 7
  • 23
  • 50
  • that happens because you set value of counter to 1, and in a next step you again assign it to foreach loop iteration, try without assigning foreach iteration number, just increase your counter on the end of loop – Aleksandar Vasić Nov 01 '14 at 10:27
  • You mean divide like {if $counter/3 == 1 } ? I think that will not work because 12 is divided by 2 and 3 – woj_jas Nov 01 '14 at 10:30

6 Answers6

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

this might work

2

I know this is an old one but try to use counter as a custom function from 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}

Had to go back to CMS Made Simple to make something related ;)

Maciej
  • 126
  • 6
1

Try like

{if $counter%3 eq 0 && $counter gt 2}
    {assign var=counter value=1}
{/if}
GautamD31
  • 28,552
  • 10
  • 64
  • 85
1

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

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

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

{$counter = ($smarty.foreach.smartyloop.iteration % 3) + 1}
Jeff B
  • 88
  • 4
0

This solution is working I dont't know why , maybe foreach checks conditions from last to first .

  {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}
woj_jas
  • 1,092
  • 7
  • 23
  • 50
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++;
}
?>
Fas M
  • 429
  • 2
  • 11
  • Although the PHP tag was there, The original question mentions a Smarty foreach loop, not a PHP foreach loop – Tricky Mar 22 '19 at 16:28