Smarty Foreach Instructions for use _php tutorial

Source: Internet
Author: User
foreach is another scenario for processing loops other than section (choose a different scenario depending on your needs). foreach is used to work with simple arrays (the same type of elements in an array), which is much simpler to format than the section, with the drawback that only simple arrays can be processed.
foreach must be paired with/foreach and must specify the From and item properties.
The data traversal in the array specified by the From property is processed into the variable specified by the Item property.
Refer to foreach (array_expression as $key = $value)
From <=> Array_expression;item <=> $value; key <=> $key.
The Name property can be arbitrarily specified (a combination of letters, numbers, and underscores).
foreach can be nested, but you must ensure that the foreach names in the nested are unique.
The From property (usually an array) determines the number of cycles.
The Foreachelse statement is executed when the From property has no value. (When the value specified by the From property is null, the FOREACHELSE statement can be used to specify--otherwise--what)
The Foreach loop has its own variable name, which can be accessed by using the variable name. Use the method {$smarty. Foreach.foreachname.varname}, where Foreachname is the name attribute specified in foreach.

foreach Demo
{* This example outputs the value of all elements in the array $custid *}
Copy CodeThe code is as follows:
{foreach from= $custid item=curr_id}
ID: {$curr _id}

{/foreach}

Output Result:
id:1000
id:1001
id:1002
Presentation of the Foreach Key and nested demo
{*
The array is defined as follows:
Copy CodeThe code is as follows:
$smarty->assign ("Contacts", Array ("Phone" = "1", "fax" = "2", "Cell" = "3"),
Array ("Phone" = "555-4444", "Fax" = "555-3333", "cell" = "760-1234"));
*}
The {* Key is the subscript for the array, see 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 with "0"
For example:
Copy CodeThe code is as follows:







{foreach from= $items key=myid item=i Name=foo} {if $smarty. foreach.foo.index% 5 = = 0} {* $smarty. foreach.foo.index to 5 Remainder *} {/if} {/foreach}
Title
{$i. Label}


. iteration
The iteration contains the number of executions of the current loop, always starting at 1, and each time the self-added 1 is executed.
For example:
Copy CodeThe code is as follows:
{* Output 0|1, 1|2, 2|3, ... Wait *}
{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 executes.
For example:
Copy CodeThe code is as follows:
{* Display LATEST when the loop first executes, 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 executes to the final pass.
For example:
Copy CodeThe code is as follows:
{* Add 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 show the number of times a loop executes, which can be called in a loop or after a loop executes.
For example:
Copy CodeThe code is as follows:
{* Show line number at last *}
{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}

http://www.bkjia.com/PHPjc/321472.html www.bkjia.com true http://www.bkjia.com/PHPjc/321472.html techarticle foreach is another scenario for processing loops other than section (choose a different scenario depending on your needs). foreach is used to work with simple arrays (the type of elements in an array is consistent), its lattice ...

  • 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.