|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Greg R Smarty n00b
Joined: 20 Apr 2004 Posts: 1
|
Posted: Tue Apr 20, 2004 9:37 am Post subject: How to list vertically in a table (dynamically) |
|
|
I had to sign up once I worked out how to do this - I have fallen in love with this template engine - everything is 10x easier to code now
Although I'm a n00b to Smarty I have lots of PHP experience behind me - I'm hoping this helps others who may want to do the same (and learn at the same time )
FYI: You should also read this thread for information:
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=1391
First of all the code:
Template:
Code: |
{section name=tr loop=$columnHeight}
<tr>
{section name=td start=$smarty.section.tr.index step=$columnHeight loop=$recordCount}
<td>
{if isset($alphabet[td])}
{$alphabet[td]}
{else}
{/if}
</td>
{/section}
</tr>
{/section}
|
PHP Pseudocode:
Code: |
$alphabet = array('a', 'b', 'c', 'd', 'e', 'f'.....'z');
$smarty->assign('recordCount', ceil(<"Number of database records"> / <"number of columns">) * <"number of columns">);
$smarty->assign('stepSize', ceil(<"Number of database records"> / <"number of columns">));
|
Now an explanation, for the example I will use the alphabet (as it's easier to undertand )
The goal:
To get table to display like this in 4 columns (column number can be anything):
Code: |
A | H | O | V
B | I | P | W
C | J | Q | X
D | K | R | Y
E | L | S | Z
F | M | T |
G | N | U |
|
First of all the variables are set up in PHP:
The record count has to be expanded to make sure the td elements are generated for empty cells. So in this example we get:
ceil(26 / 4) * 4
ceil(6.5) * 4
7 * 4 = 28 (makes sense as you need at least 28 cells in a 4 column layout to hold the alphabet)
The column height is ceil(26 / 4) = 7
Next we write the template:
Code: | {section name=tr loop=$columnHeight} |
i.e. We are making 7 rows. (0,1...6 of the array)
Code: | {section name=td start=$smarty.section.tr.index step=$columnHeight loop=$recordCount} |
for each iteration we get:
start = 0, 1, 2, 3, 4, 5, 6 (the first entry in each row)
step = this goes up by 7 in this instance, therefore the first row goes 0, 7, 14, 21 and the second 1, 8, 15, 22
loop = we don't go past 27 (the 28th item) in this case.
if there is no white space between the <td></td> tags use an if or default (see the linked thread at the top) to ensure a non breaking space is entered to make all the table borders display.
I haven't tested this - but it should work fine down to 2 rows/columns at the very least.
Hopefully this helps some people who may want to do the same. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Apr 20, 2004 3:55 pm Post subject: |
|
|
Hi Greg,
thanks for the nice tip! I'm sure it will help someone. |
|
Back to top |
|
B00zy Smarty Rookie
Joined: 19 Jul 2004 Posts: 12
|
Posted: Tue Jul 20, 2004 12:01 am Post subject: |
|
|
Thanks for the tip, Greg! In addition, here's how this can be done dynamically:
Code: |
{assign var="columns" value="2"}
{math equation="ceil(count / columns)"
count=$array|@count
columns=$columns
assign="columnHeight"}
{section name=tr loop=$columnHeight}
{if $smarty.section.tr.first == true}
<table>
{/if}
<tr>
{section name=td start=$smarty.section.tr.index step=$columnHeight loop=$columnHeight*$columns}
<td>
{if isset($array[td])}
{$array[td]}
{else}
{/if}
</td>
{/section}
</tr>
{if $smarty.section.tr.last == true}
<table>
{/if}
{/section}
|
|
|
Back to top |
|
punknroll Smarty n00b
Joined: 06 Oct 2005 Posts: 3
|
Posted: Tue Oct 11, 2005 9:21 am Post subject: false end tag |
|
|
nice one, but the last <table> should be </table>! |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
|
Back to top |
|
intel352 Smarty Rookie
Joined: 11 Nov 2007 Posts: 5
|
Posted: Fri Jun 20, 2008 11:45 am Post subject: |
|
|
I know this thread isn't long, but for a Sticky, you'd think any corrections would already be in the posted code. |
|
Back to top |
|
fl1 Smarty n00b
Joined: 15 Dec 2008 Posts: 4
|
Posted: Mon Dec 15, 2008 3:13 pm Post subject: |
|
|
split_array works like a charm |
|
Back to top |
|
|
|
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
|