Home > Backend Development > PHP Tutorial > Smarty Foreach Instructions_PHP Tutorial

Smarty Foreach Instructions_PHP Tutorial

WBOY
Release: 2016-07-21 15:40:03
Original
941 people have browsed it

foreach is another way to handle loops besides section (choose different options according to different needs). foreach is used to process simple arrays (the elements in the array are of the same type). Its format is much simpler than section. The disadvantage is that it can only process simple arrays.
foreach must be used in pairs with /foreach, and the from and item attributes must be specified.
Traverse the data in the array specified by the from attribute into the variable specified by the item attribute.
Reference foreach (array_expression as $key => $value)
from <=> array_expression; item <=> $value; key <=> $key.
The name attribute can be specified arbitrarily (a combination of letters, numbers and underscores).
Foreach can be nested, but the foreach name in the nest must be unique. The
from attribute (usually an array) determines the number of loops.
The foreachelse statement is executed when the from attribute has no value. (When the value specified by the from attribute is empty, you can use the foreachelse statement to specify it - otherwise - what to do)
The foreach loop has its own variable name, which can be used to access the loop. The usage method is {$smarty.foreach. foreachname.varname}, where foreachname is the name attribute specified in foreach.

foreach demonstration
{* This example will output the values ​​​​of all elements in the array $custid*}

Copy code The code is as follows :

{foreach from=$custid item=curr_id}
id: {$curr_id}

{/foreach}

Output Result:
id: 1000
id: 1001
id: 1002
Demonstration of foreach key and nested demonstration
{*
The array is defined as follows:
Copy code The code is as follows:

$smarty->assign("contacts", array(array("phone" => "1" , "fax" => "2", "cell" => "3"),
array("phone" => "555-4444", "fax" => "555-3333" , "cell" => "760-1234")));
*}
{* The key is the subscript of the array, please refer to the explanation of the array*}
{foreach name=outer item =contact from=$contacts}
{foreach key=key item=item from=$contact}
{$key}: {$item}

{/foreach}
{ /foreach}

Output result:
phone: 1
fax: 2
cell: 3
phone: 555-4444
fax: 555-3333
cell: 760-1234

.index
index contains the current array index, starting from "0"
For example:
Copy code The code is as follows:


{foreach from=$items key=myId item=i name=foo}
{if $smarty.foreach.foo .index % 5 == 0} {* $smarty.foreach.foo.index Find remainder of 5*}

{/ if}

{/foreach}
Title
{$i.label}


.iteration
iteration contains the number of executions of the current loop, which always starts from 1 and increments by 1 each time it is executed.
For example:
Copy code The code is as follows:

{* 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 set to true when the current foreach loop is executed for the first time.
For example:
Copy code The code is as follows:

{* Display LATEST when the loop is executed for the first time, o Otherwise display id *}

{foreach from=$items key=myId item=i name=foo}




{/foreach}
{if $smarty. foreach.foo.first}LATEST{else}{$myId}{/if} {$i.label}


.last
last is set to true when the current foreach loop is executed for the last time.
For example:
Copy code The code is as follows:

{* Add a horizontal line at the end of the list*}
{foreach from=$items key=part_id item=prod name=products}
{$prod}{if $smarty.foreach.products.last}
{else },{/if}
{foreachelse}
... content ...
{/foreach}

.total
total is used to display the number of loop executions and can be called during or after the loop execution.
For example:
Copy code The code is as follows:

{* Display the row number at the end*}
{foreach from=$items key=part_id item=prod name=foo}
{$prod.label }

{if $smarty.foreach.foo.last}
{$smarty.foreach.foo.total} items

{/if}
{foreachelse}
... something else ...
{/foreach}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321472.htmlTechArticleforeach is another solution for processing loops in addition to section (choose different solutions according to different needs). foreach is used to process simple arrays (the elements in the array are of the same type), its format...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template