|
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 |
canoramix Smarty Rookie
Joined: 12 Oct 2003 Posts: 9
|
Posted: Mon Oct 13, 2003 10:49 pm Post subject: {foreach} with variable number of columns |
|
|
Hi everybody,
As you may notice, I'm a Smarty newbie . I managed to get a {foreach from=$result_array item=$row} with $result_array build from a mysql query fetched array. Now, I'm facing a new challenge:
How can I use {foreach} with $result_array being a array with variable number of columns?
Q: Why I need this?
A: I want to create an html table with a variable number of columns, so, some times this $result_array will have 4, 5, 6 or 8 columns, doesn't matter.
Now I'm using {foreach from=$result_array item=$row} and when I need to access some data I use {$row.fieldname}. But with a variable number of fields, I don't know the names of those field. Is threre any way to access this data?
Thanks in advance,
Canoramix |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Oct 14, 2003 7:11 am Post subject: Re: {foreach} with variable number of columns |
|
|
canoramix wrote: | I don't know the names of those field. Is threre any way to access this data? |
sure. use two nested foreachs:
Code: | {foreach from=$result_array item=row}
<tr>
{foreach from=$row key=name item=cell}
<td>{$row.$name} is the same as {$cell}</td>
{/foreach}
</tr>
{/foreach}
|
|
|
Back to top |
|
canoramix Smarty Rookie
Joined: 12 Oct 2003 Posts: 9
|
Posted: Tue Oct 14, 2003 9:03 pm Post subject: |
|
|
Thanks Messju, it worked.
BTW, I tried to use {section} instead of {foreach} and the code is:
Code: | {section loop=$result_array name=row}
<tr>
{section loop=$result_array[row] name=cell}
<td>{$result_array[row][cell]}</td>
{/section}
</tr>
{/section}
|
Canoramix |
|
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
|