Smarty template engine is not running foreach

100 Views Asked by At

the foreach method is not working properly in the smarty template engine. Can you show how it might look using a while loop

// Foreach data in post array saved : add a new input in the redirection form
foreach($_SESSION['POSTDATA'] as $key => $value)
{
    if($key!='ACTION' && $key!='AES_KEYS')
    {
        $smarty->assign('postdata',array('id' => $key, 'name' => $key, 'value'  => $value));
    }
}
$_SESSION['POSTDATA'] = '';
1

There are 1 best solutions below

0
Juanga Covas On

As per your code, you could try this to be correct (since you're trashing the previous 'postdata' value at each loop iteration and want to exclude ACTION and AES_KEYS keys):

unset($_SESSION['POSTDATA']['ACTION']);
unset($_SESSION['POSTDATA']['AES_KEYS']);
$smarty->assign('postdata', $_SESSION['POSTDATA']);

Then you can access your 'postdata' variable assigned to smarty using this:

{$postdata.yourkey}

Anyway you could directly access the session global using this in smarty 3:

{$smarty.session.POSTDATA.desiredkey}

or this

{$smarty['session']['POSTDATA']['desiredkey']}