7-14 ThinkPHP5循环标签:模板大量数据输出工具

7-14 ThinkPHP5循环标签:模板大量数据输出工具

挥衡棘访千拟峰熄陌核纷灯伸

7-14 ThinkPHP5循环标签:模板大量数据输出工具第1张

1.volist

7-14 ThinkPHP5循环标签:模板大量数据输出工具第2张

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:

<?php
namespace app\index\controller;
class Index extends \think\Controller
{
    public function index()
    {
        $user = [];
        for ($key = 0; $key < 10; $key++)
        {
            $user[]=[
                'name'=>'萌面人'.$key."号",
                'sex'=>$key ? "男":"女",
                'age'=>rand(15,40),
                'salary'=>rand(3200,6800)
            ];
        }
        //注意:user是二维数组
        return $this->view->fetch('',['user'=>$user]);
    }
}

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:

<table border="1" cellspacing="0" cellpadding="2" width="40%"align="center">
    <caption style="font-size: 22px;font-weight: bold">员工信息表</caption>
    <tr style="background: lightskyblue">
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>工资</th>
    </tr>
    <!--volist循环-->
    {volist name="user" id="vo"}
    <tr>
        <td>{$key}</td>
        <td>{$vo.name}</td>
        <td>{$vo.sex}</td>
        <td>{$vo.age}</td>
        <td>{$vo.salary}</td>
    </tr>
    {/volist}
</table>

执行:

7-14 ThinkPHP5循环标签:模板大量数据输出工具第3张

volist的$key属性,key是数组下标从0开始,自定义后从1开始

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:

<table border="1" cellspacing="0" cellpadding="2" width="40%"align="center">
    <caption style="font-size: 22px;font-weight: bold">员工信息表</caption>
    <tr style="background: lightskyblue">
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>工资</th>
    </tr>
    <!--volist循环-->
    {volist name="user" id="vo" key="k"}
    <tr>
        <td>{$k}</td>
        <td>{$vo.name}</td>
        <td>{$vo.sex}</td>
        <td>{$vo.age}</td>
        <td>{$vo.salary}</td>
    </tr>
    {/volist}
</table>

执行:

7-14 ThinkPHP5循环标签:模板大量数据输出工具第4张

offset:索引 length :数量

从索引为2的数组下标开始输出,输出5条数据

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:

<table border="1" cellspacing="0" cellpadding="2" width="40%"align="center">
    <caption style="font-size: 22px;font-weight: bold">员工信息表</caption>
    <tr style="background: lightskyblue">
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>工资</th>
    </tr>
    <!--volist循环-->
    {volist name="user" id="vo" key="k" offset="2" length="5"}
    <tr>
        <td>{$k}</td>
        <td>{$vo.name}</td>
        <td>{$vo.sex}</td>
        <td>{$vo.age}</td>
        <td>{$vo.salary}</td>
    </tr>
    {/volist}
</table>

执行:

7-14 ThinkPHP5循环标签:模板大量数据输出工具第5张

删除key属性,从原始数据查看偏移效果,确实是从索引为2开始输出的,输出了5条数据

<table border="1" cellspacing="0" cellpadding="2" width="40%"align="center">
    <caption style="font-size: 22px;font-weight: bold">员工信息表</caption>
    <tr style="background: lightskyblue">
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>工资</th>
    </tr>
    <!--volist循环-->
    {volist name="user" id="vo" offset="2" length="5"}
    <tr>
        <td>{$key}</td>
        <td>{$vo.name}</td>
        <td>{$vo.sex}</td>
        <td>{$vo.age}</td>
        <td>{$vo.salary}</td>
    </tr>
    {/volist}
</table>

执行:

7-14 ThinkPHP5循环标签:模板大量数据输出工具第6张

empty属性,表中没有数据时输出的内容

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:

<table border="1" cellspacing="0" cellpadding="2" width="40%"align="center">
    <caption style="font-size: 22px;font-weight: bold">员工信息表</caption>
    <tr style="background: lightskyblue">
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>工资</th>
    </tr>
    <!--volist循环-->
    {volist name="user" id="vo" offset="2" length="5" empty="没有数据"}
    <tr>
        <td>{$key}</td>
        <td>{$vo.name}</td>
        <td>{$vo.sex}</td>
        <td>{$vo.age}</td>
        <td>{$vo.salary}</td>
    </tr>
    {/volist}
</table>

修改数据,使其为空数组

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:

<?php
namespace app\index\controller;
class Index extends \think\Controller
{
    public function index()
    {
        $user = [];
        for ($key = 0; $key < 10; $key++)
        {
            $user[]=[
                'name'=>'萌面人'.$key."号",
                'sex'=>$key ? "男":"女",
                'age'=>rand(15,40),
                'salary'=>rand(3200,6800)
            ];
        }
        //注意:user是二维数组
        return $this->view->fetch('',['user'=>[]]);
    }
}

执行:

7-14 ThinkPHP5循环标签:模板大量数据输出工具第7张


将控制器中将user变量值由空数组恢复为之前的值

foreach循环:

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:

<?php
namespace app\index\controller;
class Index extends \think\Controller
{
    public function index()
    {
        $user = [];
        for ($key = 0; $key < 10; $key++)
        {
            $user[]=[
                'name'=>'萌面人'.$key."号",
                'sex'=>$key ? "男":"女",
                'age'=>rand(15,40),
                'salary'=>rand(3200,6800)
            ];
        }
        //注意:user是二维数组
        return $this->view->fetch('',['user'=>$user]);
    }
}

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:

<table border="1" cellspacing="0" cellpadding="2" width="40%"align="center">
    <caption style="font-size: 22px;font-weight: bold">员工信息表</caption>
    <tr style="background: lightskyblue">
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>工资</th>
    </tr>
    <!--foreach循环-->
    {foreach name="user" item="value"}
    <tr>
        <td>{$key}</td>
        <td>{$value.name}</td>
        <td>{$value.sex}</td>
        <td>{$value.age}</td>
        <td>{$value.salary}</td>
    </tr>
    {/foreach}
</table>

执行:

7-14 ThinkPHP5循环标签:模板大量数据输出工具第8张


for循环生成空表格

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:

<table border="1" cellspacing="0" cellpadding="2" width="40%"align="center">
    <caption style="font-size: 22px;font-weight: bold">员工信息表</caption>
    <tr style="background: lightskyblue">
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>工资</th>
    </tr>
    <!--for循环-->
    <!--for循环与原生php中基本一致-->
    {for start='0' end='10' comparsion='lt' name='i' step='1'}
        <tr>
            {for start='0' end='5' comparsion='lt' name='j' step='1'}
                <td>{$j*5+$i}</td>
            {/for}
        </tr>
    {/for}
</table>

执行:

7-14 ThinkPHP5循环标签:模板大量数据输出工具第9张


7-14 ThinkPHP5循环标签:模板大量数据输出工具第10张

概冀惶静伸闷丸撅骏盯把享擞