View previous topic :: View next topic |
Author |
Message |
HZiegler Smarty Rookie
Joined: 04 Aug 2007 Posts: 5 Location: Germany
|
Posted: Sat Aug 04, 2007 9:19 pm Post subject: Split array into same displayed parts |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Aug 05, 2007 12:17 am Post subject: |
|
|
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 |
|
HZiegler Smarty Rookie
Joined: 04 Aug 2007 Posts: 5 Location: Germany
|
Posted: Sun Aug 05, 2007 7:35 am Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Aug 06, 2007 3:02 am Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Aug 06, 2007 1:29 pm Post subject: |
|
|
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 |
|
HZiegler Smarty Rookie
Joined: 04 Aug 2007 Posts: 5 Location: Germany
|
Posted: Wed Aug 22, 2007 8:18 pm Post subject: |
|
|
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
Thanks! _________________ Thinking is the hardest work there is, which is probably the reason why so few engage in it. (Henry Ford) |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
|
Back to top |
|
|