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

Modulo in my 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
Pierre-Henri
Smarty Rookie


Joined: 09 Nov 2010
Posts: 5

PostPosted: Tue Nov 09, 2010 4:07 pm    Post subject: Modulo in my foreach Reply with quote

Hello everybody,

In first, please excuse me for my english. I'm french Wink

I'm currently using Smarty to display an array content.

I want to display a portion of HTML each 6 items for create a pagination.

I think the best way is to use modulo by 6 but it doesn't work.

For example, I want this result (The <br/> is writed every 6 items)

Code:
<div class="items">item 1</div>
<div class="items">item 1</div>
<div class="items">item 2</div>
<div class="items">item 3</div>
<div class="items">item 4</div>
<div class="items">item 5</div>
<br/>
<div class="items">item 6</div>
<div class="items">item 7</div>
<div class="items">item 8</div>
<div class="items">item 9</div>
<div class="items">item 10</div>
<div class="items">item 11</div>
<br/>
<div class="items">item 12</div>
<div class="items">item 13</div>


Do you understand my request ?

Thanks forward for your help !

pH
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Tue Nov 09, 2010 7:19 pm    Post subject: straight forward solution Reply with quote

Here is a straight forward solution to this:

Template file:
Code:
{section name='i' loop=$items}
<div class="items"><a href="{$items[i].1}">{$items[i].0}</a></div>
{if $smarty.section.i.index_next%7==6}
   <br />
{/if}
{/section}


Assigned PHP variable, $item is like:
Code:
$items = array(
   array('item 1', '1.php'),
   array('item 2', '2.php'),
   array('item 3', '3.php'),
   array('item 4', '4.php'),
   array('item 5', '5.php'),
   array('item 6', '6.php'),
   array('item 7', '7.php'),
   array('item 8', '8.php'),
   array('item 9', '9.php'),
   array('item 10', '10.php'),
   array('item 11', '11.php'),
   array('item 12', '12.php'),
   array('item 13', '13.php'),
   array('item 14', '14.php'),
   array('item 15', '15.php'),
);
$smarty->assign('items', $items);


Just guess, why there is a need to use $number%7==6 ... Smile

Output is:
Code:
<div class="items"><a href="1.php">item 1</a></div>
<div class="items"><a href="2.php">item 2</a></div>
<div class="items"><a href="3.php">item 3</a></div>
<div class="items"><a href="4.php">item 4</a></div>
<div class="items"><a href="5.php">item 5</a></div>
<div class="items"><a href="6.php">item 6</a></div>
   <br />
<div class="items"><a href="7.php">item 7</a></div>
<div class="items"><a href="8.php">item 8</a></div>
<div class="items"><a href="9.php">item 9</a></div>
<div class="items"><a href="10.php">item 10</a></div>
<div class="items"><a href="11.php">item 11</a></div>
<div class="items"><a href="12.php">item 12</a></div>
<div class="items"><a href="13.php">item 13</a></div>
   <br />
<div class="items"><a href="14.php">item 14</a></div>
<div class="items"><a href="15.php">item 15</a></div>
Back to top
View user's profile Send private message Visit poster's website
Pierre-Henri
Smarty Rookie


Joined: 09 Nov 2010
Posts: 5

PostPosted: Wed Nov 10, 2010 4:33 pm    Post subject: Reply with quote

Hi,

Thank you for reply me.

I've tried your solution and it solve my problem !

Thanks a lot !

Bye.

pH
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