Files
smarty/docs/en/designers/language-builtin-functions/language-function-foreach.xml
2006-09-25 22:17:33 +00:00

455 lines
13 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<sect1 id="language.function.foreach">
<title>{foreach},{foreachelse}</title>
<para>
<varname>{foreach}</varname> is used to loop over a
<emphasis role="bold">single associative array</emphasis>,
unlike <link linkend="language.function.section"><varname>{section}</varname></link>
which is for looping over <emphasis role="bold">arrays of data</emphasis>.
The syntax for
<varname>{foreach}</varname> is much easier than
<link linkend="language.function.section"><varname>{section}</varname></link>,
but as a tradeoff it <emphasis role="bold">can only be used
for a single array</emphasis>. Every <varname>{foreach}</varname> tag must be
paired with a closing <varname>{/foreach}</varname> tag.
</para>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc" />
<thead>
<row>
<entry>Attribute Name</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>from</entry>
<entry>array</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>The array you are looping through</entry>
</row>
<row>
<entry>item</entry>
<entry>string</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>The name of the variable that is the current
element</entry>
</row>
<row>
<entry>key</entry>
<entry>string</entry>
<entry>No</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>The name of the variable that is the current key</entry>
</row>
<row>
<entry>name</entry>
<entry>string</entry>
<entry>No</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>The name of the foreach loop for accessing
foreach properties</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<itemizedlist>
<listitem><para>
Required attributes are <parameter>from</parameter> and <parameter>item</parameter>.
</para></listitem>
<listitem><para>
The <parameter>name</parameter> of the <varname>{foreach}</varname> loop can be anything
you like, made up of letters, numbers and underscores, like
<ulink url="&url.php-manual;language.variables">PHP variables</ulink>.
</para></listitem>
<listitem><para>
<varname>{foreach}</varname> loops can be nested, and the nested
<varname>{foreach}</varname> names must be unique from each other.
</para></listitem>
<listitem><para>
The <parameter>from</parameter> attribute, usually an array of values,
determines the number of times <varname>{foreach}</varname> will loop.
</para></listitem>
<listitem><para>
<varname>{foreachelse}</varname> is executed when there are no
values in the <parameter>from</parameter> variable.
</para></listitem>
<listitem><para>
<varname>{foreach}</varname> loops also have their own variables that handle properties.
These are accessed with:
<link linkend="language.variables.smarty.loops">
<parameter>{$smarty.foreach.name.property}</parameter></link> with
<quote>name</quote> being the
<parameter>name</parameter> attribute.
</para>
<note>
<title>Note</title>
<para>The <parameter>name</parameter> attribute is only required when
you want to access a <varname>{foreach</varname>} property, unlike
<link linkend="language.function.section"><varname>{section}</varname></link>.
Accessing a <varname>{foreach}</varname> property with <parameter>name</parameter>
undefined does not throw an error, but leads to unpredictable results instead.
</para>
</note>
</listitem>
<listitem><para>
<varname>{foreach}</varname> properties are
<link linkend="foreach.property.index"><parameter>index</parameter></link>,
<link linkend="foreach.property.iteration"><parameter>iteration</parameter></link>,
<link linkend="foreach.property.first"><parameter>first</parameter></link>,
<link linkend="foreach.property.last"><parameter>last</parameter></link>,
<link linkend="foreach.property.show"><parameter>show</parameter></link>,
<link linkend="foreach.property.total"><parameter>total</parameter></link>.
</para></listitem>
</itemizedlist>
<example>
<title>The <parameter>item</parameter> attribute</title>
<programlisting role="php">
<![CDATA[
<?php
$arr = array(1000, 1001, 1002);
$smarty->assign('myArray', $arr);
?>
]]>
</programlisting>
<para>Template to output <parameter>$myArray</parameter> in an un-ordered list</para>
<programlisting>
<![CDATA[
<ul>
{foreach from=$myArray item=foo}
<li>{$foo}</li>
{/foreach}
</ul>
]]>
</programlisting>
<para>
The above example will output:
</para>
<screen>
<![CDATA[
<ul>
<li>1000</li>
<li>1001</li>
<li>1002</li>
</ul>
]]>
</screen>
</example>
<example>
<title>Demonstrates the <parameter>item</parameter> and <parameter>key</parameter> attributes</title>
<programlisting role="php">
<![CDATA[
<?php
$arr = array(9 => 'Tennis', 3 => 'Swimming', 8 => 'Coding');
$smarty->assign('myArray', $arr);
?>
]]>
</programlisting>
<para>Template to output <parameter>$myArray</parameter> as key/val pair,
like PHP's <ulink url="&url.php-manual;foreach"><varname>foreach</varname></ulink>.</para>
<programlisting>
<![CDATA[
<ul>
{foreach from=$myArray key=k item=v}
<li>{$k}: {$v}</li>
{/foreach}
</ul>
]]>
</programlisting>
<para>
The above example will output:
</para>
<screen>
<![CDATA[
<ul>
<li>9: Tennis</li>
<li>3: Swimming</li>
<li>8: Coding</li>
</ul>
]]>
</screen>
</example>
<example>
<title>{foreach} with associative <parameter>item</parameter> attribute</title>
<programlisting role="php">
<![CDATA[
<?php
$items_list = array(23 => array('no' => 2456, 'label' => 'Salad'),
96 => array('no' => 4889, 'label' => 'Cream')
);
$smarty->assign('items', $items_list);
?>
]]>
</programlisting>
<para>Template to output <parameter>$items</parameter> with
<parameter>$myId</parameter> in the url</para>
<programlisting>
<![CDATA[
<ul>
{foreach from=$items key=myId item=i}
<li><a href="item.php?id={$myId}">{$i.no}: {$i.label}</li>
{/foreach}
</ul>
]]>
</programlisting>
<para>
The above example will output:
</para>
<screen>
<![CDATA[
<ul>
<li><a href="item.php?id=23">2456: Salad</li>
<li><a href="item.php?id=96">4889: Cream</li>
</ul>
]]>
</screen>
</example>
<example>
<title>{foreach} with nested <parameter>item</parameter> and <parameter>key</parameter></title>
<para>Assign an array to Smarty, the key contains the key for each looped value.</para>
<programlisting role="php">
<![CDATA[
<?php
$smarty->assign('contacts', array(
array('phone' => '1',
'fax' => '2',
'cell' => '3'),
array('phone' => '555-4444',
'fax' => '555-3333',
'cell' => '760-1234')
));
?>
]]>
</programlisting>
<para>The template to output <parameter>$contact</parameter>.</para>
<programlisting>
<![CDATA[
{foreach name=outer item=contact from=$contacts}
<hr />
{foreach key=key item=item from=$contact}
{$key}: {$item}<br />
{/foreach}
{/foreach}
]]>
</programlisting>
<para>
The above example will output:
</para>
<screen>
<![CDATA[
<hr />
phone: 1<br />
fax: 2<br />
cell: 3<br />
<hr />
phone: 555-4444<br />
fax: 555-3333<br />
cell: 760-1234<br />
]]>
</screen>
</example>
<example>
<title>Database example with {foreachelse}</title>
<para>A database (eg PEAR or ADODB) example of a search script, the query results assigned to Smarty</para>
<programlisting role="php">
<![CDATA[
<?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) );
?>
]]>
</programlisting>
<para>The template which display <quote>None found</quote>
if no results with <varname>{foreachelse}</varname>.</para>
<programlisting>
<![CDATA[
{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}
]]>
</programlisting>
</example>
<sect2 id="foreach.property.index">
<title>.index</title>
<para>
<parameter>index</parameter> contains the current array index, starting with zero.
</para>
<example>
<title><parameter>index</parameter> example</title>
<programlisting role="php">
<![CDATA[
{* 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>
]]>
</programlisting>
</example>
</sect2>
<sect2 id="foreach.property.iteration">
<title>.iteration</title>
<para>
<parameter>iteration</parameter> contains the current loop iteration and always
starts at one, unlike <link linkend="foreach.property.index"><parameter>index</parameter></link>.
It is incremented by one on each iteration.
</para>
<example>
<title><parameter>iteration</parameter> and <parameter>index</parameter> example</title>
<programlisting role="php">
<![CDATA[
{* 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}
]]>
</programlisting>
</example>
</sect2>
<sect2 id="foreach.property.first">
<title>.first</title>
<para>
<parameter>first</parameter> is &true; if the current <varname>{foreach}</varname>
iteration is the initial one.
</para>
<example>
<title><parameter>first</parameter> property example</title>
<programlisting role="php">
<![CDATA[
{* 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>
]]>
</programlisting>
</example>
</sect2>
<sect2 id="foreach.property.last">
<title>.last</title>
<para>
<parameter>last</parameter> is set to &true; if the current
<varname>{foreach}</varname> iteration is the final one.
</para>
<example>
<title><parameter>last</parameter> property example</title>
<programlisting role="php">
<![CDATA[
{* Add horizontal rule at end of list *}
{foreach from=$items key=part_id item=prod name=products}
<a href="#{$part_id}">{$prod}</a>{if $smarty.foreach.products.last}<hr>{else},{/if}
{foreachelse}
... content ...
{/foreach}
]]>
</programlisting>
</example>
</sect2>
<sect2 id="foreach.property.show">
<title>.show</title>
<para>
<parameter>show</parameter> is used as a parameter to <varname>{foreach}</varname>.
<parameter>show</parameter> is a boolean value. If
&false;, the <varname>{foreach}</varname> will not be displayed.
If there is a <varname>{foreachelse}</varname> present, that will be alternately displayed.
</para>
</sect2>
<sect2 id="foreach.property.total">
<title>.total</title>
<para>
<parameter>total</parameter> contains the number of iterations that this
<varname>{foreach}</varname> will loop.
This can be used inside or after the <varname>{foreach}</varname>.
</para>
<example>
<title><parameter>total</parameter> property example</title>
<programlisting role="php">
<![CDATA[
{* 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}
]]>
</programlisting>
</example>
<para>
See also <link linkend="language.function.section"><varname>{section}</varname></link>
and <link linkend="language.variables.smarty.loops"><parameter>$smarty.foreach</parameter></link>.
</para>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->