Associative arrays with smarty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Associative arrays with smarty

Post by GeXus »

If anyone is a smarty pro, I have a question I cannot seem to find an answer for... I've built the following array, which will be used to display a series of categories and sub-categories (ie, directory). I'm trying to display it in 3 columns (ie, typical directory style) but I'm not having any luck. Any help would be amazing! Thanks


Array:

Code: Select all

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

What I have now:

Code: Select all

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

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

{/foreach}
gavin1996
Forum Newbie
Posts: 22
Joined: Tue Jan 30, 2007 8:30 pm

Post by gavin1996 »

Try "recursion"
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

I'm pretty sure there is a more efficient way of doing it... I think recursion has pretty high overhead
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Are you looping categories, then looping subcategories that match the category, and trying to make all this span three columns?
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Everah wrote:Are you looping categories, then looping subcategories that match the category, and trying to make all this span three columns?

I'm doing one query and assigning it into an array formated like the one above, then I would want it to display like such:

Code: Select all

Category 1                Category 2                    Category 3
 - Sub 1                          - Sub 1                          - Sub 1 
 - Sub 2                          - Sub 2                         - Sub 2

Category 4                Category 5                    Category 6
 - Sub 1                          - Sub 1                          - Sub 1 
 - Sub 2                          - Sub 2                          - Sub 2

Basically the same way http://dmoz.org displays their categories.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I am sure you can perform some type of template side logic using modulus to achieve what you want. Or you could use a section tag with a skip parameter.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Everah wrote:I am sure you can perform some type of template side logic using modulus to achieve what you want. Or you could use a section tag with a skip parameter.
Someone had mentioned using the 'iteration' property, which will let you know how many times you have iterated through each loop, can you think of how that could be applied to the tr/td's?

I don't think section will handle associated arrays.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You're right, section handles multidimensional numerical arrays. Sorry. I am a user of TemplateLite myself, and while the syntax for both are similar, there may be differences in what I am telling you...

Is there a relatable category ID of some sort in the array you are building? In that way you could tap into that for checking on the modulus...

Code: Select all

{foreach from=$categories key=k item=v}
  {$k}:
  {if $v.cat_id % 3 == 0}
  {*
    This would be the left most column
  *}

  {elseif $v.cat_id % 3 == 1}
  {*
    This is the middle column
  *}

  {elsif $v.cat_id % 3 == 2}
  {*
    This is the right column
  *}

  {/if}
{/foreach} 
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Hmm.. I'm not sure how that would work, this is what I have.. (thanks for your help on this btw!).. I tried using the if statements.. but I really don't know how it should be setup.

Code: Select all

$db->sql_query('SELECT categories.category_id AS cat_id, sub_categories.sub_category_id AS sub_id, categories.name as cat_name, sub_categories.name as sub_name FROM categories LEFT JOIN sub_categories ON (categories.category_id = sub_categories.category_id) ORDER by cat_name');

while ($row = $db->fetch_array()) {
  $categories[$row['cat_name']][] = array('sub_id' => $row['sub_id'], 'cat_id' => $row['cat_id'], 'sub_name' => $row['sub_name']);
 
}

Code: Select all

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

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

  {/foreach}
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok, I was able to get it to work.. Thank you so much! This is what i did.

[syntax="smarty"]
{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}

feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Glad you got it worked out.
Post Reply