For Programmers: Free Programming Magazines  


Home > Archive > PHP Smarty Templates > February 2005 > indexing arrays in smarty









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author indexing arrays in smarty
Atte André Jensen

2005-02-21, 4:02 pm

I'm trying to use indexes in smarty, but can't make it work... I have
the following tow arrays:

$cats = array(
array('name' => 'hans', 'favorite_food_id' => '1'),
array('name' => 'john', 'favorite_food_id' => '2'));

$food = array('1' => array('id' => '1', 'food_name' => 'water'),
'2' => array('id' => '2', 'food_name' => 'bread'));


I would like to get output something like this:

hans loves water
john loves bread

with this template:

{foreach from=$cats item=item}
<p>{$item.name} loves {$food.item.favorite_food_id.food_name}
{/foreach}

The above template doesn't work but this is how it looks in php:

foreach($cats as $cat){
echo $cat['name'].
' loves '.
$food[$cat['favorite_food_id']]['food_na
me'].
'<br>';
}

Can it be done in a smarty template?

Needless to say the above example is a simplified version of a general,
often more complex thing I wish to accomplish.

--
peace, love & harmony
Atte

http://www.atte.dk
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com