By submitting PHP Resources you own, or know of, you'll help us build the largest PHP Resource website on the net. Please double check that your resource doesn't already exist before you submit it!!. We thank you for helping make this a better website.
{foreach} is used to loop over an
associative array as well a numerically-indexed array,
unlike {section}
which is for looping over numerically-indexed arrays only.
The syntax for
{foreach} is much easier than
{section},
but as a tradeoff it can only be used
for a single array. Every {foreach} tag must be
paired with a closing {/foreach} tag.
Attribute Name
Type
Required
Default
Description
from
array
Yes
n/a
The array you are looping through
item
string
Yes
n/a
The name of the variable that is the current
element
key
string
No
n/a
The name of the variable that is the current key
name
string
No
n/a
The name of the foreach loop for accessing
foreach properties
Required attributes are from and item.
The name of the {foreach} loop can be anything
you like, made up of letters, numbers and underscores, like
PHP variables.
{foreach} loops can be nested, and the nested
{foreach} names must be unique from each other.
The from attribute, usually an array of values,
determines the number of times {foreach} will loop.
{foreachelse} is executed when there are no
values in the from variable.
{foreach} loops also have their own variables that handle properties.
These are accessed with:
{$smarty.foreach.name.property} with
"name" being the
name attribute.
Note: The name attribute is only required when
you want to access a {foreach} property, unlike
{section}.
Accessing a {foreach} property with name
undefined does not throw an error, but leads to unpredictable results instead.
A database (eg PEAR or ADODB) example of a search script, the query results assigned to Smarty
<?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) ); ?>
The template which display "None found"
if no results with {foreachelse}.
{foreach key=cid item=con from=$results}
<a class="blue" HREF="res_contact.php?contact_id={$cid}">{$con.name} - {$con.nick}</a><br />
{foreachelse}
No items were found in the search
{/foreach}
.index
index contains the current array index, starting with zero.
Example 7-10. index example
{* The header block is output every five rows *} <table> {foreach from=$items key=myId item=i name=foo} {if $smarty.foreach.foo.index % 5 == 0} <tr><th>Title</th></tr> {/if} <tr><td>{$i.label}</td></tr> {/foreach} </table>
.iteration
iteration contains the current loop iteration and always
starts at one, unlike index.
It is incremented by one on each iteration.
Example 7-11. iteration and index example
{* this will output 0|1, 1|2, 2|3, ... etc *} {foreach from=$myArray item=i name=foo} {$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration}, {/foreach}
.first
first is TRUE if the current {foreach}
iteration is the initial one.
Example 7-12. first property example
{* show LATEST on the first item, otherwise the id *} <table> {foreach from=$items key=myId item=i name=foo} <tr> <td>{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if}</td> <td>{$i.label}</td> </tr> {/foreach} </table>
.last
last is set to TRUE if the current
{foreach} iteration is the final one.
Example 7-13. last property example
{* Add horizontal rule at end of list *} {foreach from=$items key=part_id item=prod name=products} <a class="blue" HREF="res_#{$part_id}">{$prod}</a>{if $smarty.foreach.products.last}<hr>{else},{/if} {foreachelse} ... content ... {/foreach}
.show
show is used as a parameter to {foreach}.
show is a boolean value. If
FALSE, the {foreach} will not be displayed.
If there is a {foreachelse} present, that will be alternately displayed.
.total
total contains the number of iterations that this
{foreach} will loop.
This can be used inside or after the {foreach}.
Example 7-14. total property example
{* show rows returned at end *} {foreach from=$items key=part_id item=prod name=foo} {$prod.name><hr/> {if $smarty.foreach.foo.last} <div id="total">{$smarty.foreach.foo.total} items</div> {/if} {foreachelse} ... something else ... {/foreach}