2004-04-13 08:46:28 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
|
|
|
|
<!-- $Revision$ -->
|
2005-05-10 11:42:40 +00:00
|
|
|
<sect1 id="language.function.foreach">
|
2005-05-25 19:13:20 +00:00
|
|
|
<title>{foreach},{foreachelse}</title>
|
2005-05-10 11:42:40 +00:00
|
|
|
<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>
|
|
|
|
|
<para>
|
2005-05-24 14:01:49 +00:00
|
|
|
<emphasis>{foreach}</emphasis> loops are an alternative to
|
2005-05-10 16:01:12 +00:00
|
|
|
<link
|
2005-05-24 14:01:49 +00:00
|
|
|
linkend="language.function.section"><emphasis>{section}</emphasis></link>
|
2005-05-25 19:13:20 +00:00
|
|
|
loops. <emphasis>{foreach}</emphasis> is used to loop over a
|
|
|
|
|
<emphasis role="bold">single associative array</emphasis>. The syntax for
|
2005-05-24 14:01:49 +00:00
|
|
|
<emphasis>{foreach}</emphasis> is much easier than
|
|
|
|
|
<emphasis>{section}</emphasis>, but as a tradeoff it
|
|
|
|
|
<emphasis role="bold">can only be used
|
|
|
|
|
for a single array</emphasis>. <emphasis>{foreach}</emphasis> tags must be
|
|
|
|
|
paired with <emphasis>{/foreach}</emphasis> tags. Required parameters
|
2005-05-10 11:42:40 +00:00
|
|
|
are <emphasis>from</emphasis> and <emphasis>item</emphasis>. The
|
2005-05-24 14:01:49 +00:00
|
|
|
name of the {foreach} loop can be anything you like, made up of
|
|
|
|
|
letters, numbers and underscores. <emphasis>{foreach}</emphasis>
|
|
|
|
|
loops can be nested, and the nested {foreach} names must be unique
|
2005-05-10 11:42:40 +00:00
|
|
|
from each other. The <emphasis>from</emphasis> variable (usually an
|
|
|
|
|
array of values) determines the number of times
|
2005-05-24 14:01:49 +00:00
|
|
|
<emphasis>{foreach}</emphasis> will loop.
|
|
|
|
|
<emphasis>{foreachelse}</emphasis> is executed when there are no
|
2005-05-10 11:42:40 +00:00
|
|
|
values in the <emphasis>from</emphasis> variable.
|
|
|
|
|
</para>
|
|
|
|
|
<example>
|
2005-05-25 19:13:20 +00:00
|
|
|
<title>{foreach} - item</title>
|
2005-05-27 16:25:02 +00:00
|
|
|
<programlisting role="php">
|
2005-05-25 19:13:20 +00:00
|
|
|
<![CDATA[
|
|
|
|
|
<?php
|
2005-05-27 16:25:02 +00:00
|
|
|
$arr = array( 1001,1002,1003);
|
|
|
|
|
$smarty->assign('custid', $arr);
|
2005-05-25 19:13:20 +00:00
|
|
|
?>
|
|
|
|
|
]]>
|
2005-05-27 16:25:02 +00:00
|
|
|
</programlisting>
|
2005-05-10 11:42:40 +00:00
|
|
|
<programlisting>
|
|
|
|
|
<![CDATA[
|
2005-05-27 16:25:02 +00:00
|
|
|
{* this example will print out all the values of the $custid array *}
|
|
|
|
|
{foreach from=$custid item=curr_id}
|
|
|
|
|
id: {$curr_id}<br />
|
|
|
|
|
{/foreach}
|
2005-05-10 11:42:40 +00:00
|
|
|
]]>
|
|
|
|
|
</programlisting>
|
|
|
|
|
<para>
|
|
|
|
|
The above example will output:
|
|
|
|
|
</para>
|
|
|
|
|
<screen>
|
|
|
|
|
<![CDATA[
|
2005-05-27 16:25:02 +00:00
|
|
|
id: 1000<br />
|
|
|
|
|
id: 1001<br />
|
|
|
|
|
id: 1002<br />
|
2005-05-10 11:42:40 +00:00
|
|
|
]]>
|
|
|
|
|
</screen>
|
|
|
|
|
</example>
|
|
|
|
|
|
|
|
|
|
<example>
|
2005-05-25 19:13:20 +00:00
|
|
|
<title>{foreach} - item and key</title>
|
2005-05-27 16:25:02 +00:00
|
|
|
<programlisting role="php">
|
2005-05-25 19:13:20 +00:00
|
|
|
<![CDATA[
|
|
|
|
|
// The key contains the key for each looped value
|
|
|
|
|
// assignment looks like this:
|
|
|
|
|
<?php
|
2005-05-27 16:25:02 +00:00
|
|
|
$smarty->assign('contacts', array(
|
|
|
|
|
array('phone' => '1',
|
|
|
|
|
'fax' => '2',
|
|
|
|
|
'cell' => '3'),
|
|
|
|
|
array('phone' => '555-4444',
|
|
|
|
|
'fax' => '555-3333',
|
|
|
|
|
'cell' => '760-1234')
|
|
|
|
|
));
|
2005-05-25 19:13:20 +00:00
|
|
|
?>
|
|
|
|
|
]]>
|
2005-05-27 16:25:02 +00:00
|
|
|
</programlisting>
|
2005-05-10 11:42:40 +00:00
|
|
|
<programlisting>
|
|
|
|
|
<![CDATA[
|
2005-05-27 16:25:02 +00:00
|
|
|
{foreach name=outer item=contact from=$contacts}
|
|
|
|
|
<hr />
|
|
|
|
|
{foreach key=key item=item from=$contact}
|
|
|
|
|
{$key}: {$item}<br />
|
|
|
|
|
{/foreach}
|
|
|
|
|
{/foreach}
|
|
|
|
|
]]>
|
2005-05-10 11:42:40 +00:00
|
|
|
</programlisting>
|
|
|
|
|
<para>
|
|
|
|
|
The above example will output:
|
|
|
|
|
</para>
|
|
|
|
|
<screen>
|
|
|
|
|
<![CDATA[
|
2005-05-27 16:25:02 +00:00
|
|
|
<hr />
|
|
|
|
|
phone: 1<br />
|
|
|
|
|
fax: 2<br />
|
|
|
|
|
cell: 3<br />
|
|
|
|
|
<hr />
|
|
|
|
|
phone: 555-4444<br />
|
|
|
|
|
fax: 555-3333<br />
|
|
|
|
|
cell: 760-1234<br />
|
2005-05-10 11:42:40 +00:00
|
|
|
]]>
|
|
|
|
|
</screen>
|
|
|
|
|
</example>
|
|
|
|
|
|
2005-05-25 19:13:20 +00:00
|
|
|
<example>
|
|
|
|
|
<title>{foreach} - database example (eg PEAR or ADODB)</title>
|
|
|
|
|
<programlisting role="php">
|
|
|
|
|
<![CDATA[
|
|
|
|
|
<?php
|
|
|
|
|
$sql = 'select contact_id, name, nick from contacts order by contact';
|
|
|
|
|
$smarty->assign("contacts", $db->getAssoc($sql));
|
|
|
|
|
?>
|
|
|
|
|
]]>
|
2005-05-27 16:25:02 +00:00
|
|
|
</programlisting>
|
|
|
|
|
<programlisting>
|
2005-05-25 19:13:20 +00:00
|
|
|
<![CDATA[
|
|
|
|
|
{foreach key=cid item=con from=$contacts}
|
2005-05-27 16:25:02 +00:00
|
|
|
<a href="contact.php?contact_id={$cid}">{$con.name} - {$con.nick}</a><br />
|
2005-05-25 19:13:20 +00:00
|
|
|
{/foreach}
|
|
|
|
|
]]>
|
2005-05-27 16:25:02 +00:00
|
|
|
</programlisting>
|
2005-05-25 19:13:20 +00:00
|
|
|
|
|
|
|
|
</example>
|
|
|
|
|
|
2005-05-10 11:42:40 +00:00
|
|
|
<para>
|
2005-05-25 19:13:20 +00:00
|
|
|
{foreach} loops also have their own variables that handle {foreach} properties.
|
2005-05-24 14:01:49 +00:00
|
|
|
These are indicated like so:
|
|
|
|
|
<link linkend="language.variables.smarty.loops">{$smarty.foreach.foreachname.varname}</link>
|
|
|
|
|
with
|
2005-05-10 11:42:40 +00:00
|
|
|
foreachname being the name specified as the <emphasis>name</emphasis>
|
|
|
|
|
attribute of foreach
|
|
|
|
|
</para>
|
|
|
|
|
|
2005-05-27 16:25:02 +00:00
|
|
|
<para>See <link linkend="section.property.index">{section}</link>
|
|
|
|
|
for examples of the properties below as they are identical
|
|
|
|
|
</para>
|
|
|
|
|
|
2005-05-10 11:42:40 +00:00
|
|
|
<sect2 id="foreach.property.iteration">
|
|
|
|
|
<title>iteration</title>
|
|
|
|
|
<para>
|
2005-05-25 19:13:20 +00:00
|
|
|
iteration is used to display the current loop iteration.Iteration always
|
|
|
|
|
starts with 1 and is incremented by one on each iteration.
|
2005-05-10 11:42:40 +00:00
|
|
|
</para>
|
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
|
|
<sect2 id="foreach.property.first">
|
|
|
|
|
<title>first</title>
|
|
|
|
|
<para>
|
|
|
|
|
<emphasis>first</emphasis> is set to true if the current foreach iteration is the first
|
|
|
|
|
one.
|
|
|
|
|
</para>
|
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
|
|
<sect2 id="foreach.property.last">
|
|
|
|
|
<title>last</title>
|
|
|
|
|
<para>
|
|
|
|
|
<emphasis>last</emphasis> is set to true if the current foreach iteration is the last
|
|
|
|
|
one.
|
|
|
|
|
</para>
|
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
|
|
<sect2 id="foreach.property.show">
|
|
|
|
|
<title>show</title>
|
|
|
|
|
<para>
|
|
|
|
|
<emphasis>show</emphasis> is used as a parameter to foreach.
|
|
|
|
|
<emphasis>show</emphasis> 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.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
</sect2>
|
|
|
|
|
<sect2 id="foreach.property.total">
|
|
|
|
|
<title>total</title>
|
|
|
|
|
<para>
|
|
|
|
|
<emphasis>total</emphasis> is used to display the number of iterations that this foreach
|
|
|
|
|
will loop. This can be used inside or after the foreach.
|
|
|
|
|
</para>
|
2005-05-10 16:01:12 +00:00
|
|
|
<para>
|
|
|
|
|
See also <link linkend="language.function.section">{section}</link> and
|
|
|
|
|
<link linkend="language.variables.smarty.loops">$smarty.foreach</link>.
|
|
|
|
|
</para>
|
2005-05-10 11:42:40 +00:00
|
|
|
</sect2>
|
2004-04-13 08:46:28 +00:00
|
|
|
</sect1>
|
2005-05-27 16:25:02 +00:00
|
|
|
|
2004-04-13 08:46:28 +00:00
|
|
|
<!-- Keep this comment at the end of the file
|
2005-05-10 11:42:40 +00:00
|
|
|
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
|
2005-05-10 16:01:12 +00:00
|
|
|
-->
|