Usage of foreach in smarty

Source: Internet
Author: User
Tags php foreach

{Foreach}, {foreachelse}

{Foreach} is used to cyclically access an associated array like a digital index array. It is different from the {Section} That can only access the numeric index array, the syntax of {foreach} is much simpler than that of {Section}, but it can only be used as a compromise for a single array. Each {foreach} tag must be paired with the close tag {/foreach.

Attribute

1,FromType: array necessity: Yes required default value: N/A description: cyclically accessed Array

2,ItemType: String string necessity: Yes required default value: N/A description: variable name of the current element

3,KeyType: String string necessity: No optional default value: N/A description: variable name of the current key name

4,NameType: String string necessity: No (optional) Default Value: N/A description: name of the foreach loop used to access the foreach attribute

From and item are required attributes
The {foreach} loop name can be a combination of any letters, arrays, and underscores. For more information, see PHP variables.
{Foreach} loops can be nested. The nested {foreach} names should be different.
The from attribute is usually a value array and is used to determine the number of {foreach} cycles.
If no value exists in the from variable, {foreachelse} is executed }.
The {foreach} loop also has its own attribute variables, which can be accessed through {$ smarty. foreach. Name. Property}, where "name" is the name attribute.
Note: The name attribute is valid only when you need to access the {foreach} attribute. It is different from {Section. Accessing the {foreach} attribute of an undefined name will not throw an error, but it will lead to unpredictable results.
{Foreach} attributes include index, iteration, first, last, show, total.

Example:

Example 1. Item attributes
<? PHP
$ Arr = array (1000,100 1, 1002 );
$ Smarty-> assign ('myarray', $ ARR );
?>

Output $ myarray in unordered list using a template

<Ul>
{Foreach from = $ myarray item = Foo}
<Li >{$ Foo} </LI>
{/Foreach}
</Ul>

The above example will output:

<Ul>
<Li> 1000 </LI>
<Li> 1001 </LI>
<Li> 1002 </LI>
</Ul>

Example 2: demonstrate the item and key attributes

<? PHP
$ Arr = array (9 => 'tennis ', 3 => 'canonicalization', 8 => 'coding ');
$ Smarty-> assign ('myarray', $ ARR );
?>

Output $ myarray in the form of template key name/key-value pair, similar to PhP foreach.

<Ul>
{Foreach from = $ myarray key = K item = V}
<Li >{$ K }:{$ v} </LI>
{/Foreach}
</Ul>

The above example will output:

<Ul>
<Li> 9: Tennis </LI>
<Li> 3: padding Ming </LI>
<Li> 8: Coding </LI>
</Ul>

Example 3. The item attribute of {foreach} is an associated array.

<? PHP
$ Items_list = array (23 => array ('no' => 2456, 'label' => 'salad '),
96 => array ('no' => 4889, 'label' => 'cream ')
);
$ Smarty-> assign ('items ', $ items_list );
?>

In the template, the URL outputs $ items through $ myid

<Ul>
{Foreach from = $ items key = myid item = I}
<Li> <a href = "item. php? Id = {$ myid} ">{$ I. No }:{$ I. Label} </LI>
{/Foreach}
</Ul>

The above example will output:

<Ul>
<Li> <a href = "item. php? Id = 23 "> 2456: salad </LI>
<Li> <a href = "item. php? Id = 96 "& gt; 4889: Cream </LI>
</Ul>

Example 4. {foreach} Use nested item and key

Set an array to smarty. Each cyclic value corresponding to each key name includes a key.

<? PHP
$ Smarty-> assign ('contacts', array (
Array ('phone' => '1 ',
'Fax' => '2 ',
'Cell '=> '3 '),
Array ('phone' => '2014-555 ',
'Fax' => '2017-555 ',
'Cell '=> '2017-760 ')
));
?>

The template used to output $ contact.

{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 5. Database example using {foreachelse}

Example of a search script for a database (such as pear or ADODB,

<? 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 outputs the words "None found" when no result is displayed using the {foreachelse} tag.

{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}

. Index contains the current array index, starting from scratch.

Example 6. Index example

{* Output the header block once per five lines *}
<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 contains the number of current cycles. Different from index, the number of cycles increases by 1 from 1.

Example 7: iteration and Index

{* This will output 0 | 1, 1 | 2, 2 | 3,... etc *}
{* This example outputs 0 | 1, 1 | 2, 2 | 3,... and so on *}
{Foreach from = $ myarray item = I name = Foo}
{$ Smarty. foreach. Foo. Index} | {$ smarty. foreach. Foo. Iteration },
{/Foreach}

The value of first in the initial position of the current {foreach} loop is true.

Example 8. first attribute example

{* Display latest instead of ID for the first entry *}
<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>

The value of last in the final position of the current {foreach} loop is true.

Example 9. Last attribute example

{* Add a horizontal flag at the end of the 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}

Show is the {foreach} parameter. Show is a Boolean value. If the value is false, {foreach} is not displayed. If there is a corresponding {foreachelse}, it will be displayed.

Total includes the number of {foreach} cycles, which can be used in {foreach} or later.

Example 10. Total attribute example

{* Display the number of rows at the end position *}
{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}

Problem

{Foreach from = $ custid item = curr_id key = num}
{If num is odd}
What
{Else}
Other
{/If}
{/Foreach}

What is the difference between odd and enen?
{Foreach from = $ custid item = curr_id key = num}
{If num % 2 = 0}
What
{Else}
Other
{/If}
{/Foreach}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.