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

How to loop this array with section tags

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


Joined: 16 Aug 2006
Posts: 28

PostPosted: Mon Jan 29, 2007 5:32 am    Post subject: How to loop this array with section tags Reply with quote

Code:


Array ( [Automotive] => Array ( [0] => Array ( [sub_id] => [cat_id] => 37 [sub_name] => ) ) [Business] => Array ( [0] => Array ( [sub_id] => 27 [cat_id] => 51 [sub_name] => Services ) )



I have a bunch of data setup in this format, I'm currently using the foreach statement below to go through it
Code:


{foreach from=$categories key='cat_name' item='category'}

 {$cat_name}:
  {foreach from=$category item='sub_category'}
      {$sub_category.sub_name}
  {/foreach}

{/foreach}




Does anyone know how I would loop through this using section instead of foreach?
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Mon Jan 29, 2007 5:46 pm    Post subject: Reply with quote

You can't and why would you want to? Section tags require 0-based indexed arrays -- yours is associative.
Back to top
View user's profile Send private message
GeXus
Smarty Rookie


Joined: 16 Aug 2006
Posts: 28

PostPosted: Mon Jan 29, 2007 8:10 pm    Post subject: Reply with quote

I want to display the data in 3x columns, I can't seem to figure out how I would do that with foreach.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Mon Jan 29, 2007 8:38 pm    Post subject: Reply with quote

Yeah, I kinda got that based on your other posts Smile Okay, I know that you know this thread: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=1391 you can adapt that method by using the iteration property on the foreach loop (don't forget to name the foreach loop).

http://smarty.php.net/manual/en/language.function.foreach.php
Back to top
View user's profile Send private message
GeXus
Smarty Rookie


Joined: 16 Aug 2006
Posts: 28

PostPosted: Wed Jan 31, 2007 10:16 pm    Post subject: Reply with quote

Boots, I've looked at the thread but and I tried one option that seemed like it might work, where I had section tags within the first foreach, but it did not work and I was not using the iteration.. I'm really not sure what to do. I appreciate your help!
Back to top
View user's profile Send private message
GeXus
Smarty Rookie


Joined: 16 Aug 2006
Posts: 28

PostPosted: Fri Feb 02, 2007 9:57 pm    Post subject: Reply with quote

Got it to work, for the record this is what I did

Code:

{foreach from=$categories key='cat_name' item='category' name='cats'}



{if $smarty.foreach.cats.iteration % 3 == 1}
<td>
    {$cat_name}:
    {foreach from=$category item='sub_category'}
         {$sub_category.sub_name}
        {/foreach}
</td>
{/if}

{if $smarty.foreach.cats.iteration % 3 == 2}
<td>
    {$cat_name}:
    {foreach from=$category item='sub_category'}
         {$sub_category.sub_name}
     {/foreach}
</td>
{/if}

{if $smarty.foreach.cats.iteration % 3 == 0}
<td>
    {$cat_name}:
    {foreach from=$category item='sub_category'}
         {$sub_category.sub_name}
     {/foreach}

</td>
</tr>
<tr>
{/if}



{/foreach}
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