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