zoukankan      html  css  js  c++  java
  • 夺命雷公狗---Smarty NO:12 section函数

    功能:实现对数组的遍历操作

    注:其只能遍历索引下标从0开始且连续的索引型数组

    基本语法:

    {section name=名称 loop=循环数组(次数) start=开始(0) step=步阶(1) max=最大循环次数}
    {sectionelse}

    {/section}

    loop:要遍历的数组

    name:section名称,每次遍历时,系统会自动将数组的索引小标放入name中

    start:从哪个索引开始遍历

    step:步阶,默认为1

    max:循环最大次数

    sectionelse :当数组为空时,系统自动执行此句

    例1:遍历一维数组

    demo5.html代码示例

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=’utf-8′>
    <title></title>
    </head>
    <body>
    {*遍历一堆数组*}
    {section loop=$lamp name=’sec1′}
    {$lamp[sec1]}
    {/section}
    </body>
    </html>

    demo5.php代码示例

    <?php
    require “smarty/Smarty.class.php”;
    $smarty = new Smarty();
    $lamp = array(‘php’,’mysql’,’apache’,’linux’);
    $smarty -> assign(‘lamp’,$lamp);
    $smarty -> display(“demo5.html”);

    在php中有两种数组的遍历方式

    for($i=0;$i<count($arr);$i++) {

    echo $arr[$i];

    }

    foreach($arr as $row) {

    echo $row;

    }

    foreach(foreach)是真正完成了对数组的遍历,而for(section)循环只是对数组循环输出而已。

    例2:遍历二维数组

     
    {*遍历二维数组*}
    {section loop=$persons name=’sec2′}
    {$persons[sec2][‘name’]}—{$persons[sec2][‘age’]}—{$persons[sec2][‘sex’]}<hr/>
    {/section}

    例3:sectionelse的使用

    {*sectionelse*}
    {section loop=$var name=’sec3′}
    {$var[sec3]}
    {sectionelse}
    未查询到相关记录
    {/section}

    例4:其他属性的使用

    {*其他属性的使用*}
    {section loop=$lamp name=’sec4′ start=1 step=2 max=1}
    {$lamp[sec4]}<hr/>
    {/section}

    例5:附加属性

    {$smarty.section.name.index} :循环索引,默认从0开始

    {$smarty.section.name.index_prev} :上一次循环索引,默认从-1开始

    {$smarty.section.name.index_next} :下一次循环索引

    {$smarty.section.name.iteration} :当前是第几次循环

    {$smarty.section.name.first} :当第一次运行时条件为真

    {$smarty.section.name.last} :当最后一次循环条件为真

    {$smarty.section.name.total} :循环的总次数

    示例代码:

    {*附加属性的使用*}
    {section loop=$lamp name=’sec5′}
    {$smarty.section.sec5.iteration},{$lamp[sec5]}<hr/>
    {/section}
    当前总共有{$smarty.section.sec5.total}条记录

    section的总代码如下

    demo5.html代码示例

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=’utf-8′>
    <title></title>
    </head>
    <body>
    {*遍历一堆数组*}
    {section loop=$lamp name=’sec1′}
    {$lamp[sec1]}<hr/>
    {/section}
    {*遍历二维数组*}
    {section loop=$persons name=’sec2′}
    {$persons[sec2][‘name’]}—{$persons[sec2][‘age’]}—{$persons[sec2][‘sex’]}<hr/>
    {/section}
    {*sectionelse*}
    {section loop=$var name=’sec3′}
    {$var[sec3]}
    {sectionelse}
    未查询到相关记录
    {/section}
    <hr/>
    {*其他属性的使用*}
    {section loop=$lamp name=’sec4′ start=1 step=2 max=1}
    {$lamp[sec4]}<hr/>
    {/section}
    {*附加属性的使用*}
    {section loop=$lamp name=’sec5′}
    {$smarty.section.sec5.iteration},{$lamp[sec5]}<hr/>
    {/section}
    当前总共有{$smarty.section.sec5.total}条记录
    </body>
    </html>

    demo5.php代码示例

    <?php
    require “smarty/Smarty.class.php”;
    $smarty = new Smarty();
    $lamp = array(‘php’,’mysql’,’apache’,’linux’);
    $persons = array(
    array(‘name’=>’lisi’,’age’=>’66’,’sex’=>’nan’),
    array(‘name’=>’xiaohong’,’age’=>’99’,’sex’=>’nv’),
    array(‘name’=>’xiaoqing’,’age’=>’88’,’sex’=>’nv’),
    array(‘name’=>’zhangfei’,’age’=>’22’,’sex’=>’nan’),
    );
    $var = ”;
    $smarty -> assign(‘lamp’,$lamp);
    $smarty -> assign(‘persons’,$persons);
    $smarty -> assign(‘var’,$var);
    $smarty -> display(“demo5.html”);
  • 相关阅读:
    python requests用法总结
    Linux统计某文件夹下文件的个数
    PM2使用及介绍
    如何区分USB 2.0 和USB 3.0插口
    npm突然找不到npm-cli.js的解决方法
    mRemoteNG
    js中几种实用的跨域方法原理详解
    Tornado异步阻塞解决方案
    [阅读笔记]EfficientDet
    iOS 保存视频AVAssetWriter
  • 原文地址:https://www.cnblogs.com/leigood/p/5033334.html
Copyright © 2011-2022 走看看