Smarty Foreach Instructions_PHP Tutorial
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*}
{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:
$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:
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:
{* 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:
{* Display LATEST when the loop is executed for the first time, o Otherwise display id *}
{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:
{* 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:
{* Display the row number at the end*}
{foreach from=$items key=part_id item=prod name=foo}
{$prod.label }
{if $smarty.foreach.foo.last}
{/if}
{foreachelse}
... something else ...
{/foreach}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

In the process of PHP development, dealing with special characters is a common problem, especially in string processing, special characters are often escaped. Among them, converting special characters into single quotes is a relatively common requirement, because in PHP, single quotes are a common way to wrap strings. In this article, we will explain how to handle special character conversion single quotes in PHP and provide specific code examples. In PHP, special characters include but are not limited to single quotes ('), double quotes ("), backslash (), etc. In strings

Glodon Software is a software company focusing on the field of building informatization. Its products are widely used in all aspects of architectural design, construction, and operation. Due to the complex functions and large data volume of Glodon software, it requires high computer configuration. This article will elaborate on the computer configuration recommendations of Glodon Software from many aspects to help readers choose a suitable computer configuration processor. Glodon Software requires a large amount of data calculation and processing when performing architectural design, simulation and other operations. Therefore, the requirements for the processor are higher. It is recommended to choose a multi-core, high-frequency processor, such as Intel i7 series or AMD Ryzen series. These processors have strong computing power and multi-thread processing capabilities, and can better meet the needs of Glodon software. Memory Memory is affecting computing

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values swapped. $original_array=[

Lambda expression breaks out of the loop, specific code examples are needed. In programming, the loop structure is an important syntax that is often used. However, in certain circumstances, we may want to break out of the entire loop when a certain condition is met within the loop body, rather than just terminating the current loop iteration. At this time, the characteristics of lambda expressions can help us achieve the goal of jumping out of the loop. Lambda expression is a way to declare an anonymous function, which can define simple function logic internally. It is different from an ordinary function declaration,

Which version of the graphics card driver is best to use? 1. There is no absolute best version. It is most important to choose the version that suits your computer; 2. Because the applicability and stability of the graphics card driver version are related to the computer hardware environment and system configuration; 3. You can check the detailed information of the computer and graphics card on the official website, select the appropriate driver version based on the information, or refer to the reviews of other users. It is recommended to back up the system before installing the driver to avoid unexpected situations. Graphics card driver version 472.19 series is an excellent choice. Currently, the driver compatibility of version 472 is the best. Installing version 472 of the driver can also maximize the performance of the graphics card. The NVIDIA graphics card driver Win7 installation version, numbered 2, 472.19, is a product with remarkable quality.
