Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Add a counter into foreach

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
djibi
Smarty n00b


Joined: 17 Jul 2012
Posts: 2

PostPosted: Tue Jul 17, 2012 11:49 am    Post subject: Add a counter into foreach Reply with quote

Hi,

i'm new with Smarty and i try to do something basic with a foreach loop.

I'll try to add a custom counter in the loop but that don't work ...

Here is my code :

Code:

{counter start=1 skip=1 print=false assign=count}
{foreach from=$new_products item='product' name='newProducts'}
// Do somthing

//Here i want to increment {$count} if it's lower than 3 or set to 1 if higher than 3

{/foreach}


Can u help me ?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Jul 17, 2012 6:31 pm    Post subject: Reply with quote

You can make use of the @iteration property. See http://www.smarty.net/docs/en/language.function.foreach.tpl

Code:
{foreach from=$new_products item='product' name='newProducts'}
// Do somthing

//Here i want to increment {$count} if it's lower than 3 or set to 1 if higher than 3
{if $product@iteration <= 3}{$count =  $product@iteration}{else}{$count = 1}{/if}

{/foreach}
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Jul 17, 2012 7:11 pm    Post subject: Reply with quote

You probably don't need to create this counter. As Uwe says, the index and iteration are both available in the loop. To do something every 3rd iteration you can do:

Code:
{if $smarty.foreach.newProducts.iteration is div by 3}
  ...
{/if}
Back to top
View user's profile Send private message Visit poster's website
djibi
Smarty n00b


Joined: 17 Jul 2012
Posts: 2

PostPosted: Thu Jul 19, 2012 9:09 am    Post subject: Reply with quote

Ok thanks so, what do you think about the code below ?

This code work fine and he write an html table with 4 products by line but, if the number of products is not div by 4 (EX :5 products), it write 3 empty <td> before closing the current <tr> and stop the loop.

Code:

   {$productByLine = 4}
   {$productLineTurn = 1}
   {$tr = true}
   {$turns = 1}
   
   <table style="border: 1px solid #CCC;">
      {foreach from=$products item=product name=products}
         
         <!-- Start a new line -->
         {if $tr}
            <tr>
            {$productLineTurn = 1}
            {$tr = false}
         {/if}
         
         <td style="padding: 5px; border: 1px solid #CCC;">
            {$productLineTurn} - {$product.name}
         </td>
         
         <!-- In end of products array -->
         {if $smarty.foreach.products.iteration == $products|@count}
            
            {section name=boucle start=0 loop=$productByLine-$productLineTurn step=1}
               <td style="padding: 5px; border: 1px solid #CCC; text-align: center; color: #AAA;">Empty</td>
            {/section}
            
         {/if}
         
         {if $smarty.foreach.products.iteration is div by 4}
            </tr>
            {$tr = true}
         {/if}
      
         {$productLineTurn = $productLineTurn+1}
         {$turns = $turns+1}
         
      {/foreach}
   </table>


Thx
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Jul 19, 2012 1:55 pm    Post subject: Reply with quote

it would be easier to do this:

Code:
<tr>
  {foreach ...}
      {if ... is div by 4}</tr><tr>{/if}
  {/foreach}
</tr>


This starts a new table row every 4 elements.
Back to top
View user's profile Send private message Visit poster's website
Jenny88
Smarty n00b


Joined: 20 Aug 2012
Posts: 1

PostPosted: Mon Aug 20, 2012 6:06 am    Post subject: Reply with quote

Thanks for the assists! I'm quite a newbie myself and your tips always aid me tremendously.


________________________
thesudokugame.net
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP