Kill lui lei Dog---Smarty no:10 foreach array traversal

Source: Internet
Author: User
Tags html sample

Function: The main implementation of the logarithmic group traversal output

Basic syntax:

Foreach,foreachelse

{foreach from= array key= key name= name item= content}

{Foreachelse}

{/foreach}

From: An array to traverse the output

Item: The result of the traversal is automatically placed in the item content each time it is traversed

Key: Keys, the system will put the traversed key value in the key when each traversal

Name:foreach name, named foreach

Foreachelse: Executes this sentence when the array is empty

Demo4.html Sample Code

<!DOCTYPE HTML><HTML><Head><MetaCharSet= ' utf-8′><title></title></Head><Body>{* One-dimensional array *}{foreach from= $lamp item= ' val '} {$val}<HR/>{/foreach}{* Two-dimensional array *}{foreach from= $persons item= ' Row '} {$row [' name ']}–{$row [' Age ']}–{$row [' Sex ']}<HR/>{/foreach}{* Other parameters are used *}{foreach from= $lamp item= ' val ' key= ' K '} {$k}:{$val}<HR/>{/foreach}{* attached property *}{foreach from= $persons item= ' row ' name= ' ps '} {$smarty. foreach.ps.index}:{$ Smarty.foreach.ps.iteration}:{[email protected]}:{[email protected]}{$row [' name ']}-{$row [' Age ']}-{$row [' Sex ']} <HR/>{ /foreach} has a total of {$smarty. Foreach.ps.total} Records before</Body></HTML>

Demo4.php code Example

<?PHPrequire"Smarty/smarty.class.php ";$smarty=NewSmarty ();$lamp=Array(' php ', 'MySQL', ' Apache ',' Linux ');$persons=Array(Array(' name ' = ' Lisi ', ' age ' = ' ', ' sex ' = ' nan '),Array(' name ' = ' Zhangsam ', ' age ' = ' ", ' sex ' = ' nv '),Array(' name ' = ' = ' Wangwu ', ' age ' = ' + ', ' sex ' = ' yao '),Array(' name ' = ' JJ ', ' age ' = ' + ', ' sex ' =' Nan '));$smarty-Assign (' Lamp ',$lamp);$smartyAssign (' persons ',$persons);$smartyDisplay ("demo4.html");

Example 4:foreach attached property

$smarty. Foreach.name.index @index: Circular index (default starting from 0)

$smarty. Foreach.name.iteration @iteration: Loop iteration (currently the first cycle)

$smarty. Foreach.name.first @first: This value is true when the first loop

$smarty. Foreach.name.last @last: This value is true when the last loop

$smarty. Foreach.name.total @total: Counting the current number of cycles

{* Additional property *}{foreach from= $persons item= ' row ' name= ' ps '} {$smarty. foreach.ps.index}:{$smarty. Foreach.ps.iteration} : {[email protected]}:{[email protected]}{$row [' name ']}-{$row [' Age ']}-{$row [' Sex ']}<HR /> { /foreach} has a total of {$smarty. Foreach.ps.total} Records before

Example code:

Kill lui lei Dog---Smarty no:10 foreach array traversal

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.