• PHP生成表格


    <?php
    /*
    
    DROP TABLE IF EXISTS `art`;
    
    CREATE TABLE `art` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `pid` int(11) NOT NULL DEFAULT '0',
      `cate` char(10) NOT NULL,
      `path` char(10) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    LOCK TABLES `art` WRITE;
    
    INSERT INTO `art` (`id`, `pid`, `cate`, `path`)
    VALUES
    (1,0,'a1','4'),
    	(2,1,'b1','2'),
    	(3,1,'b2','2'),
    	(4,3,'c3',''),
    	(5,3,'c2',''),
    	(6,2,'c1',''),
    	(7,2,'c4',''),
    	(8,0,'A2','5'),
    	(9,8,'A2-B1','3'),
    	(10,9,'A2-B1-C1',''),
    	(11,9,'A2-B1-C2',''),
    	(13,9,'A2-B1-C3',''),
    	(14,8,'A2-B2','1'),
    	(15,14,'A2-B2-C1',''),
    	(16,8,'A2-B3','1'),
    	(17,16,'A2-B3-C1',''),
    	(18,0,'A3','1'),
    	(19,18,'A3-B1','1'),
    	(20,19,'A3-B1-C1','');
    
        UNLOCK TABLES;
    
    
     */
    header('Content-type:text/html;charset=utf-8');
    $connect = mysqli_connect('localhost', 'root', 'newpass', 'security');
    $connect->query('set names utf8');
    
    $query = $connect->prepare('select * from art');
    
    $query->execute();
    $query->bind_result($id, $pid, $cate, $path);
    
    
    $arr = [];
    while ($query->fetch()) {
        // printf("%s %s
    ", $id, $text);
        // echo "id: {$id} - text: {$text} <br />";
        // echo "id: {$id} - text: " . _escape_string($text) . " <br /> 
    ";
        // echo "id: {$id} - text: " . $cate . " <br /> 
    ";
        $arr[] = [
            'id'   => $id,
            'pid'  => $pid,
            'cate' => $cate,
            'path' => $path // 每一个大分类中考核总数
        ];
    }
    
    
    
    function tree($pid = 0, $arr) {
        $tree = '';
        foreach($arr as $cate) {
            if ($pid == $cate['pid']) {
                $cate['son'] = tree($cate['id'], $arr);
                $tree[] = $cate;
            }
        }
    
        return $tree;
    }
    
    $data = tree(0, $arr);
    
    
    echo "<table border='1'>
    ";
    
    
    foreach($data as $row) {
         //var_dump($row);continue;
        // 一级分类、二级分类第一行以及三级分类第一行
        echo "<tr>
    ";
        echo "<td rowspan='{$row['path']}'>{$row['cate']}</td>
    ";
        echo "<td rowspan='{$row['path']}'>10</td>
    ";
        echo "<td rowspan='{$row['path']}'>编辑 删除 增加</td>
    ";
        echo "<td rowspan='{$row['son'][0]['path']}'>{$row['son'][0]['cate']}</td>
    ";
        echo "<td rowspan='{$row['son'][0]['path']}'>mf</td>
    ";
        echo "<td rowspan='{$row['son'][0]['path']}'>编辑 删除 增加</td>
    ";
        echo "<td >{$row['son'][0]['son'][0]['cate']}</td>
    ";
        echo "<td >sf</td>
    ";
        echo "<td >编辑 删除 增加</td>
    ";
        echo "</tr>
    ";
    
        // 补全二级分类第一行的所有下级分类
        for($i = 1; $i <= count($row['son'][0]['son']) - 1; $i++) {
            echo "<tr>
    ";
            echo "<td >{$row['son'][0]['son'][$i]['cate']}</td>
    ";
            echo "<td >sf</td>
    ";
            echo "<td >编辑 删除 增加2</td>
    ";
            echo "</tr>
    ";
        }
    
        // 补全二级分类
        // for($j = 1; $j <= count($row['son']) - 1; $j++) {
        for($j = 1; $j < count($row['son']); $j++) {
            echo "<tr>
    ";
            echo "<td rowspan='{$row['son'][$j]['path']}'>{$row['son'][$j]['cate']}</td>
    ";
            echo "<td rowspan='{$row['son'][$j]['path']}'>mf</td>
    ";
            echo "<td rowspan='{$row['son'][$j]['path']}'>编辑 删除 增加3</td>
    ";
            echo "<td >{$row['son'][$j]['son'][0]['cate']}</td>
    ";
            echo "<td >sf</td>
    ";
            echo "<td >编辑 删除 增加</td>
    ";
            echo "</tr>
    ";
        }
    
        // 二级分类的下级行
        for($k = 1; $k < count($row['son']); $k++) {
            for( $l = 1; $l < count($row['son'][$k]['son']); $l++) {
                echo "<tr>
    ";
                echo "<td >{$row['son'][$k]['son'][$l]['cate']}</td>
    ";
                echo "<td >sf</td>
    ";
                echo "<td >编辑 删除 增加2</td>
    ";
                echo "</tr>
    ";
            }
        }
    }
    
    echo "</table>
    ";
    

      

  • 相关阅读:
    手把手编写PHP框架 深入了解MVC运行流程
    利用Nginx+Mono+Fastcgi代替IIS对Asp.Net进行反向代理
    用一句SQL取出第 m 条到第 n 条记录的方法
    项目管理理论与实践(6)——利用Excel制作项目文档的设计技巧
    项目管理理论与实践(7)——软件开发报价的计算方法
    Windows版Nginx启动失败之1113: No mapping for the Unicode character exists in the target multi-byte code page
    redis安装之zmalloc.h:55:2: error: #error "Newer version of jemalloc required"错误
    Vmware虚拟机设置静态IP地址
    Centos查看系统位数方法
    Invalid property 'driverClassName' of bean class [com.mchange.v2.c3p0.ComboPooledDataSource]
  • 原文地址:https://www.cnblogs.com/adtuu/p/4936216.html
Copyright © 2020-2023  润新知