Home Backend Development PHP Tutorial Smarty foreach控制循环次数的一些方法_php实例

Smarty foreach控制循环次数的一些方法_php实例

Jun 07, 2016 pm 05:12 PM
foreach smarty

1、在 smarty 中数组是经常会用到的,循环遍历数组用 section 或者 foreach ,如何得到数组长度或者判断一个数组个数呢?可以用{$array| count} 来试试。

2.

复制代码 代码如下:

{foreach from=$variable key=key name=name iteam=value}

    {$variable|@count}     // 获取数组长度

    {$smarty.foreach.loop.index}    // 获取当前循环数组元素下标,以0开始

    {$smarty.foreach.loop.iteration}    // 获取当前循环次数,以1开始

    {$smarty.foreach.loop.first}    // 为true时,标记循环第一次执行

    {$smarty.foreach.loop.last}    // 为true时,标记循环最后一次执行
 {$smarty.foreach.name.last}    // 为true时,标记循环最后一次执行

    {$smarty.foreach.loop.show}    // 当前是否显示

    {$smarty.foreach.loop.total}    // 循环次数

{/foreach}


3、控制循环次数
复制代码 代码如下:

{foreach name=infolist from=$VIDEO_INFO_LIST item=infolist}
{assign var=floor value=$smarty.foreach.infolist.index}
{if $floor  {*或者 if $smarty.foreach.infolist.iteration  
  • {$infolist%}

  • {/if}
    {/foreach}
    Statement of this Website
    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

    Hot Article Tags

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How to determine the number of foreach loop in php How to determine the number of foreach loop in php Jul 10, 2023 pm 02:18 PM

    How to determine the number of foreach loop in php

    What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? Apr 27, 2023 pm 03:40 PM

    What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList?

    PHP returns an array with key values ​​flipped PHP returns an array with key values ​​flipped Mar 21, 2024 pm 02:10 PM

    PHP returns an array with key values ​​flipped

    PHP returns the current element in an array PHP returns the current element in an array Mar 21, 2024 pm 12:36 PM

    PHP returns the current element in an array

    How to iterate through the properties of an object using the forEach function? How to iterate through the properties of an object using the forEach function? Nov 18, 2023 pm 06:10 PM

    How to iterate through the properties of an object using the forEach function?

    How to use PHP and Smarty to achieve front-end and back-end separation development How to use PHP and Smarty to achieve front-end and back-end separation development Jun 25, 2023 pm 01:46 PM

    How to use PHP and Smarty to achieve front-end and back-end separation development

    A preliminary study on the template engine Smarty in PHP A preliminary study on the template engine Smarty in PHP May 11, 2023 pm 05:15 PM

    A preliminary study on the template engine Smarty in PHP

    What is the difference between foreach and for loop What is the difference between foreach and for loop Jan 05, 2023 pm 04:26 PM

    What is the difference between foreach and for loop

    See all articles