{foreach},{foreachelse}

{foreach},{foreachelse}

{foreach} loops are an alternative to {section} loops. {foreach} is used to loop over a single associative array. The syntax for {foreach} is much easier than {section}, but as a tradeoff it can only be used for a single array. {foreach} tags must be paired with {/foreach} tags. Required parameters are from and item. The name of the {foreach} loop can be anything you like, made up of letters, numbers and underscores. {foreach} loops can be nested, and the nested {foreach} names must be unique from each other. The from variable (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.

Attribute NameTypeRequiredDefaultDescription
fromarrayYesn/aThe array you are looping through
itemstringYesn/aThe name of the variable that is the current element
keystringNon/aThe name of the variable that is the current key
namestringNon/aThe name of the foreach loop for accessing foreach properties

Example 7-5. {foreach} - item

<?php
$arr 
= array( 1001,1002,1003);
$smarty->assign('custid'$arr);
?>
{* this example will print out all the values of the $custid array *}
{foreach from=$custid item=curr_id}
  id: {$curr_id}<br />
{/foreach}

The above example will output:

id: 1000<br />
id: 1001<br />
id: 1002<br />

Example 7-6. {foreach} - item and key

// The key contains the key for each looped value
// assignment looks like this:
<?php
 $smarty
->assign('contacts', array(
                             array(
'phone' => '1',
                                   
'fax' => '2',
                                   
'cell' => '3'),
                             array(
'phone' => '555-4444',
                                   
'fax' => '555-3333',
                                   
'cell' => '760-1234')
                             ));
?>
{foreach name=outer item=contact from=$contacts}
  <hr />
  {foreach key=key item=item from=$contact}
    {$key}: {$item}<br />
  {/foreach}
{/foreach}

The above example will output:

<hr />
  phone: 1<br />
  fax: 2<br />
  cell: 3<br />
<hr />
  phone: 555-4444<br />
  fax: 555-3333<br />
  cell: 760-1234<br />

Example 7-7. {foreach} - database example (eg PEAR or ADODB)

<?php
  $sql 
'select contact_id, name, nick from contacts order by contact';
  
$smarty->assign("contacts"$db->getAssoc($sql));
?>
{foreach key=cid item=con from=$contacts}
  <a href="contact.php?contact_id={$cid}">{$con.name} - {$con.nick}</a><br />
{/foreach}

{foreach} loops also have their own variables that handle {foreach} properties. These are indicated like so: {$smarty.foreach.foreachname.varname} with foreachname being the name specified as the name attribute of foreach

See {section} for examples of the properties below as they are identical

iteration

iteration is used to display the current loop iteration.Iteration always starts with 1 and is incremented by one on each iteration.

first

first is set to true if the current foreach iteration is the first one.

last

last is set to true if the current foreach iteration is the last one.

show

show is used as a parameter to foreach. show is a boolean value, true or false. If false, the foreach will not be displayed. If there is a foreachelse present, that will be alternately displayed.

total

total is used to display the number of iterations that this foreach will loop. This can be used inside or after the foreach.

See also {section} and $smarty.foreach.

© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.