index/index/index
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { $user = []; $name = ['peter','luo','yang','wang','li','xia','zho']; //定义一个数组 for ($key=0;$key<10;$key++){ $user []=[ 'name'=>$name[rand(0,6)], //在 $name 中6个随机取值 'sex'=>($key % 2) ? '男':'女', //取余数 0->男 1->女 'age'=>rand(15,30), 'salary'=>rand(2800,5000), 'level'=>rand(1,4), 'home'=>rand(1,3) ]; } return $this->view->fetch('',['user'=>$user]); } }
view/index/index
<table border="1" cellpadding="0" cellspacing="0" width="60%"> <caption style="font-weight: bold">员工信息表</caption> <tr> <th>编号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>是否成年</th> <th>工资</th> <th>工资级别</th> <th>级别</th> <th>籍贯</th> </tr> {volist name="user" id="vo" empty="数据为空"} <tr> <td>{$key}</td> <td>{$vo.name}</td> <td>{$vo.sex}</td> <td>{$vo.age}</td> {compare type="egt" name="vo.age" value="18"} //如果大于18成年否则未成年 <td>成年</td> {else /} <td>未成年</td> {/compare} <td>{$vo.salary}</td> {between name="$vo.salary" value="2000,3800"} //如果工资在2000-3800中等收入,否则高等收入 <td>中等收入</td> {else /} <td>高收入</td> {/between} {switch name="$vo.level"} //switch 分支语句 如果是1=钻石,2=金牌,3=等于银牌,4=铜牌 {case value="1"}<td>钻石</td>{/case} {case value="2"}<td>金牌</td>{/case} {case value="3"}<td>银牌</td>{/case} {case value="4"}<td>铜牌</td>{/case} {/switch} {if condition="$vo.home==1"} //如果 home==1->安徽, home==2->江苏, home==3->外国人 {elseif condition="$vo.home==2"} <td>江苏</td> {else /} <td>外国人</td> {/if} </tr> {/volist} </table>