thinkphp自动生成表格

目的:了解全局变量和静态变量的使用

步骤1:写一个表单(table1.html)

步骤二:点击提交后对应的方法中处理

<?php
namespace Home\Controller;
use Think\Controller;

//用户相关信息及设置
class DemoController extends Controller {
    public function table1(){
        if(IS_POST){
            global $num;global $rows;
            $num = 0;$rows=0;
            echo "<table border='1' style='border-collapse:collapse;margin:0 auto;text-align:center;'>";
            $this->test(I('post.max_num'),I('post.cols_num'),I('post.incre'));
            if($num%I('post.cols_num')!=0){
                for ($i=0; $i <I('post.cols_num')-$num%I('post.cols_num'); $i++) {
                echo "<td >空</td>";
                }
            }
            echo "<tr/></table>";
        }else{
            $this->display();
        }
    }

    /*
    **$num 有实际数据的表格单元格数
    **$rows 表格有多少行
    **$max 最大值
    **$cols 表格的列数
    **$plus 表格值的递增数
    */
    public function test($max,$cols,$plus){
        static $a = 1, $b=1;
        global $num ,$rows;
        if($a<$max){
            if($b==1){
                echo "<tr>";
            }
            echo "<td style=\"min-width:80px;\">".$a."</td>";
            $a+=$plus;
            $b++;$num++;
            if($b>$cols){
                echo "</tr>";
                $b = 1;
                $rows++;
            }
            $this->test($max,$cols,$plus);            
        }
    }
    
}

效果如下图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值