Home >Backend Development >PHP Tutorial >Detailed description of smarty foreach_PHP tutorial

Detailed description of smarty foreach_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:58:451390browse

Detailed description of smarty foreach

Regarding the knowledge of smarty foreach, here is a detailed explanation of its function and usage.

smarty {foreach} is used to loop through an associative array like a numeric index array. Unlike {section}, which can only access numeric index arrays, the syntax of {foreach} is much simpler than that of {section}. , but as a trade-off only works with a single array. Each {foreach} tag must be paired with a closing tag {/foreach}.

smarty foreach has the following attributes:

属性名称 Type类型 Required必要 Default默认值 Description描述
from array数组 Yes必要 n/a 循环访问的数组
 
item string字符串 Yes必要 n/a 当前元素的变量名
 
key string字符串 No可选 n/a 当前键名的变量名
 
name string字符 No可选 n/a 用于访问foreach属性的foreach循环的名称
 

foreach is another solution for processing loops besides section (choose different solutions according to different needs).

foreach is used to process simple arrays (the elements in the array have 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.

The name attribute can be specified arbitrarily (a combination of letters, numbers and underscores).

The name of the {foreach} loop can be any combination of letters, arrays, and underscores. Please refer to PHP variables.

{foreach} loops can be nested, and the names of nested {foreach} should be different from each other.

The from attribute is usually an array of values ​​and is used to determine the number of loops of {foreach}.

When there is no value in the from variable, {foreachelse} will be executed.

The {foreach} loop also has variables with its own properties, which can be accessed through {$smarty.foreach.name.property}, where "name" is the name property.

Note: The name attribute is only valid when accessing the {foreach} attribute, which is different from {section}. Accessing the {foreach} attribute of undefined name will not throw an error, but will lead to unpredictable results.

smarty {foreach} In addition to the above attributes that are parameters, there are several non-parametric attributes: index, iteration, first, last, show, total. The following explains them one by one:

Attribute name Description
index Used to access the index value of the current foreach, index always starts from 0
iteration
属性名称 Description描述
index 用于访问当前foreach的索引值,index总是从0开始
iteration

iteration 用于显示当前循环的执行次数,iteration 总是从 1 开始,每执行一次增加 1

first 当前 foreach 循环第一次执行时 first 被设置成 true
 
last 当前 foreach 循环执行到最后一遍时 last 被设置成 true
 
show show 是 foreach 的一个参数. 取值为布尔值 true 或 false. 如果指定为 false 该循环不显示,如果循环指定了 foreachelse 子句,该子句显示与否也取决于 show 的取值
total total 用于显示循环执行的次数,可以在循环中或循环执行后调用
 
Iteration is used to display the number of executions of the current loop. Iteration always starts from 1 and increases by 1 for each execution
first When the current foreach loop is executed for the first time, first is set to true
last When the current foreach loop executes to the last pass, last is set to true
show show is a parameter of foreach. The value is Boolean true or false. If false is specified, the loop will not display. If the loop specifies foreachelse clause, whether this clause is displayed or not also depends on the value of show
total total is used to display the number of loop executions and can be called during or after the loop execution

Let’s look at a few examples to illustrate the usage of each attribute of smarty foreach:

(1) Demonstrate item and key attributes

$arr = array(9 => 'Tennis', 3 => 'Swimming', 8 => 'Coding');
$smarty->assign('myArray', $arr);
?>

Use template key name/key value pairs to output $myArray, similar to PHP's foreach.


    {foreach from=$myArray key=k item=v}
  • {$k}: {$v}

  • {/foreach}

       
The above example will output:


  • 9: Tennis

  • 3: Swimming

  • 8: Coding

(2) When the item attribute of foreach is an associative array

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



The above example will output:

(3) foreach uses nested items and keys

Set an array to Smarty that includes the key for each loop value corresponding to each key name.

$smarty->assign('contacts', array(
array('phone' => '1',
'fax' => '2',
'cell' => '3'),
array('phone' => '555-4444',
'fax' => '555-3333',
'cell' => '760-1234')
));
?>

Template for outputting $contact.

{foreach name=outer item=contact from=$contacts}



{foreach key=key item=item from=$contact}
{$key}: {$item}

{/foreach}
{/foreach}

The above example will output:



phone: 1

fax: 2

cell: 3



phone: 555-4444

fax: 555-3333

cell: 760-1234


(4) smart foreach index attribute usage

{* Output header block every five lines *}


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

{/if}

{/foreach}
Title
{$i.label}

(5) smart foreach iteration attribute usage

{* This example will output 0|1, 1|2, 2|3, ... etc. *}
{foreach from=$myArray item=i name=foo}
{$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},
{/foreach}

(6) smart foreach first attribute usage

{* Show LATEST instead of id for first entry *}


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




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

(7) smart foreach last attribute usage

{* Add a horizontal marker 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}

(8) smart foreach show attribute usage

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

(9) smart foreach total attribute usage

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



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

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

ok, that’s all about smarty foreach. For more knowledge about smarty, please refer to this site: smarty knowledge topic

Articles you may be interested in

  • Comparison of the efficiency of array_walk and foreach, for, PHP performance optimization
  • About the performance comparison when using in_array() foreach array_search() to find whether the array contains
  • Extension plug-in for for loop in smarty template
  • smarty include file Method of using variables
  • php finds whether a value exists in an array (in_array(), array_search(), array_key_exists())
  • smarty template How to use php functions in smarty templates and how to use multiple functions for one variable in smarty templates
  • The difference and usage of select into from and insert into select
  • Fatal error Class 'ZipArchive' not found... ... solution

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/976850.htmlTechArticlesmarty foreach details the knowledge about smart foreach. Here is a detailed explanation of its role and usage. smarty {foreach} is used like iterating over a numerically indexed array...
Statement:
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