IndexController.class.php
- <?php
- namespace Home\Controller;
- use Think\Controller;
- class IndexController extends Controller {
- //查询全部数据
- public function index(){
- //实例化model
- $User=D('Address');
- $arr = $User->sel_all();
- //print_r($arr);die;
- $this->assign('arr',$arr);
- $this->display();
- }
- }
- ?>
AddressModel.class.php
- <?php
- namespace Home\Model;
- use Think\Model;
- class AddressModel extends Model{
- //查询address表中所有数据
- public function sel_all(){
- $arr = $this->Table('address')->select();
- return $this->list_level($arr,$pid=0,$level=0);
- }
- //递归遍历数据
- public function list_level($arr,$pid=0,$level=0){
- //定义一个静态数组
- static $data = array();
- foreach($arr as $k => $v){
- if($v['pid'] == $pid){
- $v['level'] = $level;
- $data[] = $v;
- $this->list_level($arr,$v['id'],$level+1);
- }
- }
- return $data;
- }
- }
View
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="UTF-8">
- <title></title>
- </head>
- <body>
- <table border='1'>
- <th>编号</th>
- <th>地区</th>
- <th>层级</th>
- <foreach name='arr' item='v'>
- <tr>
- <td>{$v.id}</td>
- <!--str_repeat('字符串','重复的次数');重复一个字符串-->
- <td><?php echo str_repeat('----',$v['level']);?>{$v.name}</td>
- <td>
- <if condition="$v['level'] eq 0">
- 一级分类
- <elseif condition="$v['level'] eq 1"/>
- 二级分类
- <else/>
- 三级分类
- </if>
- </td>
- </tr>
- </foreach>
- </table>
- </body>
- </html>