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

Split array into same displayed parts

 
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
HZiegler
Smarty Rookie


Joined: 04 Aug 2007
Posts: 5
Location: Germany

PostPosted: Sat Aug 04, 2007 9:19 pm    Post subject: Split array into same displayed parts Reply with quote

If you want to add something after an amount of array entries, e.g. an image or #top-link or </td><td> etc., you can use the following code.
$part can easily be loaded from settings or given with assign()


Code:
$array = array(
   'one', 'two', 'tree', 'four', 'five', 'six', 'seven', 'eight',  'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
);

$smarty->assign('array', $array);


Code:
{assign var=parts value=3}
{math equation="ceil(values / parts)" assign=part_items values=$array|@count parts=$parts}

{$part_items}<br><br>

{section name=part loop=$parts}
   {section name=id loop=$array start=$part_items*$smarty.section.part.index max=$part_items}
      {$array[id]}<br>
   {/section}
<br><br>
{/section}
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Aug 05, 2007 12:17 am    Post subject: Reply with quote

Better to use a plugin, not? Or even more succinct, you can use PHP's array_slice() as a modifier:
Code:
{foreach from=$array|@array_slice:$start:$length item=cell}
    {$cell}
{/foreach}

where $start and $length are whatever you want to determine as the slicing range.

see also: http://php.net/manual/en/function.array-slice.php
Back to top
View user's profile Send private message
HZiegler
Smarty Rookie


Joined: 04 Aug 2007
Posts: 5
Location: Germany

PostPosted: Sun Aug 05, 2007 7:35 am    Post subject: Reply with quote

That still would be
Code:
{assign var=parts value=3}
{math equation="ceil(values / parts)" assign=part_items values=$array|@count parts=$parts}

{section name=part loop=$parts}
   {foreach from=$array|@array_slice:$part_items*$smarty.section.part.index:$part_items item=cell}
       {$cell}<br>
   {/foreach}
<br><br>
{/section}


A way slower

[EDIT:]
I just saw that I did something similliar to this thread. It's a workaround for {html_table}.

Some examples
Code:

{assign var=parts value=5}
{math equation="ceil(values / parts)" assign=part_items values=$array|@count parts=$parts}

$parts = {$parts}<br>
$part_items = {$part_items} | ceil(values / parts) = ceil({$array|@count} / {$parts})<br><br>

{html_table}:<br>
{html_table loop=$array}<br><br>

table with <td> for each entry | horizontally sorted | $parts = rows:<br>
<!-- begin -->
<table>
<tr><td>
   {section name=part loop=$parts}
      {section name=id loop=$array start=$part_items*$smarty.section.part.index max=$part_items}
         {$array[id]}
         {if !$smarty.section.id.last}
            </td><td>
         {/if}
      {/section}
      {if !$smarty.section.part.last}
         </td></tr><tr><td>
      {/if}
   {/section}

{* fill the table with empty <td>-tags *}
   {if $smarty.section.id.total != ($parts*$part_items)}
      {section name=fill loop=$parts*$part_items-$smarty.section.id.index}
         </td><td>{$smarty.section.fill.index}
      {/section}
   {/if}
</td></tr>
</table>
<br><br><br>
<!-- end -->

table with <td> for each entry | horizontally sorted | $parts = columns:<br>
<!-- begin -->
<table>
<tr><td>
   {section name=part loop=$part_items}
      {section name=id loop=$array start=$parts*$smarty.section.part.index max=$parts}
         {$array[id]}
         {if !$smarty.section.id.last}
            </td><td>
         {/if}
      {/section}
      {if !$smarty.section.part.last}
         </td></tr><tr><td>
      {/if}
   {/section}

{* fill the table with empty <td>-tags *}

   {if $smarty.section.id.total != ($parts*$part_items)}
      {section name=fill loop=$parts*$part_items-$smarty.section.id.index}
         </td><td>{$smarty.section.fill.index}
      {/section}
   {/if}
</td></tr>
</table>
<br><br><br>
<!-- end -->


table with <td> for each entry | vertically sorted | $parts = rows:<br>
<!-- begin -->
<table>
<tr><td>
   {section name=part loop=$parts}
      {section name=id loop=$array start=$smarty.section.part.index max=$part_items step=$parts}
         {$array[id]}
         {if !$smarty.section.id.last}
            </td><td>
         {/if}
      {/section}

{* fill the table with empty <td>-tag *}
      {if $smarty.section.id.iteration <= $part_items}
         </td><td>{$smarty.section.id.iteration}
      {/if}

      {if !$smarty.section.part.last}
         </td></tr><tr><td>
      {/if}
   {/section}
</td></tr>
</table>
<br><br><br>
<!-- end -->

table with <td> for each entry | vertically sorted | $parts = columns:<br>
<!-- begin -->
<table>
<tr><td>
   {section name=part loop=$part_items}
      {section name=id loop=$array start=$smarty.section.part.index max=$parts step=$part_items}
         {$array[id]}
         {if !$smarty.section.id.last}
            </td><td>
         {/if}
      {/section}

{* fill the table with empty <td>-tag *}
      {if $smarty.section.id.iteration <= $parts}
         </td><td>{$smarty.section.id.iteration}
      {/if}

      {if !$smarty.section.part.last}
         </td></tr><tr><td>
      {/if}
   {/section}
</td></tr>
</table>
<br><br><br>
<!--end -->



table with <br> for each entry | $parts = rows:<br>
<!-- begin -->
<table>
<tr><td>
   {section name=part loop=$parts}
      {section name=id loop=$array start=$part_items*$smarty.section.part.index max=$part_items}
         {$array[id]}
         {if !$smarty.section.id.last}
            <br>
         {/if}
      {/section}
      {if !$smarty.section.part.last}
         </td><td>
      {/if}
   {/section}
</td></tr>
</table>
<br><br><br>
<!-- end -->


table with <br> for each entry | $parts = columns:<br>
<!-- begin -->
<table>
<tr><td>
   {section name=part loop=$part_items}
      {section name=id loop=$array start=$parts*$smarty.section.part.index max=$parts}
         {$array[id]}
         {if !$smarty.section.id.last}
            <br>
         {/if}
      {/section}
      {if !$smarty.section.part.last}
         </td><td>
      {/if}
   {/section}
</td></tr>
</table>
<br><br><br>
<!-- end -->
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Aug 06, 2007 3:02 am    Post subject: Reply with quote

I think I missed you original premise. Mea culpa!

It still seems, though, that a simpler approach would be to do a modulus test on the iteration count of the loop, not?
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Aug 06, 2007 1:29 pm    Post subject: Reply with quote

as boots suggested, modulus works too, which is what 'div by' does:

Code:
{if $smarty.section.part.index div by $part_items}
  .. do something ..
{/if}
Back to top
View user's profile Send private message Visit poster's website
HZiegler
Smarty Rookie


Joined: 04 Aug 2007
Posts: 5
Location: Germany

PostPosted: Wed Aug 22, 2007 8:18 pm    Post subject: Reply with quote

Ok,

Code:

<table><tr>
{section loop=$big_array name=id}
   <td>{$big_array[id]}</td>
   {if $smarty.section.id.iteration % $parts == 0}
       </tr><tr>
   {/if}
{/section}
</tr></table>


would be the easiest and fastest solution (with foreach quite same).

Why didn't I think of that? Mayby it's too easy Smile
Thanks!
_________________
Thinking is the hardest work there is, which is probably the reason why so few engage in it. (Henry Ford)
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Aug 22, 2007 11:37 pm    Post subject: Reply with quote

It might read better as:

Code:
{if $smarty.section.id.iteration is not div by $parts}


Wink

http://smarty.php.net/manual/en/language.function.if.php
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