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

{foreach} with variable number of columns

 
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 -> General
View previous topic :: View next topic  
Author Message
canoramix
Smarty Rookie


Joined: 12 Oct 2003
Posts: 9

PostPosted: Mon Oct 13, 2003 10:49 pm    Post subject: {foreach} with variable number of columns Reply with quote

Hi everybody,

As you may notice, I'm a Smarty newbie Wink . 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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Oct 14, 2003 7:11 am    Post subject: Re: {foreach} with variable number of columns Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
canoramix
Smarty Rookie


Joined: 12 Oct 2003
Posts: 9

PostPosted: Tue Oct 14, 2003 9:03 pm    Post subject: Reply with quote

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
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 -> General 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