Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

foreach help

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
mark
Smarty Rookie


Joined: 17 Dec 2003
Posts: 5

PostPosted: Wed Dec 17, 2003 12:20 pm    Post subject: foreach help Reply with quote

Hi, I am totally new to Smarty. I am confused on using foreach loops. Does anyone know how to convert this into smarty?

[php:1:a94befb672]
foreach($arr['content'] as $v) {
echo $v['url'];
echo $v['caption'];
}
[/php:1:a94befb672]
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Wed Dec 17, 2003 12:30 pm    Post subject: Reply with quote

Code:

{foreach from=$arr.content item=v}
{$v.url}
{$v.caption}
{/foreach}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mark
Smarty Rookie


Joined: 17 Dec 2003
Posts: 5

PostPosted: Wed Dec 17, 2003 1:22 pm    Post subject: Reply with quote

thanks for the quick reply. am i doing this correctly? here is my code:
[php:1:6adf98d057]
// php file

include 'libs/Smarty.class.php';
$smarty = new Smarty;
$smarty->assign('arr', $n4s['content']);
$smarty->display('template.tpl');
[/php:1:6adf98d057]

Code:

// smarty file

{foreach from=$arr.content item=v}
{$v.url}
{$v.caption}
{/foreach}
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Wed Dec 17, 2003 1:28 pm    Post subject: Reply with quote

i think you have to *either* change the assign to
$smarty->assign('arr', $n4s);

*or* change the foreach to:
{foreach from=$arr item=v}

in your example you are accessing $n4s['content']['content']
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mark
Smarty Rookie


Joined: 17 Dec 2003
Posts: 5

PostPosted: Wed Dec 17, 2003 2:54 pm    Post subject: Reply with quote

{foreach from=$arr item=v} worked. thank you very much. this is great. Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development 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