19-08-2008

Smarty Smarty foreach loop example

The template below will display "None found" if no results are found with {foreachelse}.

PHP code:

<?php
  $search_condition = "where name like '$foo%' ";
  $sql = 'select contact_id, name, nick from contacts '.$search_condition.' order by name';
  $smarty->assign('results', $db->getAssoc($sql) );
?>

Smarty code:

{foreach key=cid item=con from=$results}
    <a href="contact.php?contact_id={$cid}">{$con.name} - {$con.nick}</a><br />
{foreachelse}
    No items were found in the search
{/foreach}

This is from the Smarty Online Manual

Comments:

Your comment:

»

 

[x]