php - 使用 php 构建 html 表

标签 php html

我希望有一个使用 php 构建的表,但有一些变化。我只想将其限制为 10 行。如果变量大于 10,则添加新列。 IE:

如果变量为 9,则表将有 1 列和 9 行。如果数字是 19,那么表格将包含 10 行和 2 列,第二列包含数字 11 - 19。依此类推,类似于下面

+----+-------+-------+----+-------+-------+----+-------+-------+
| Q  | Tally | Total | Q  | Tally | Total | Q  | Tally | Total |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 1  |       |       | 11 |       |       | 21 |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 2  |       |       | 12 |       |       | 22 |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 3  |       |       | 13 |       |       | 23 |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 4  |       |       | 14 |       |       | 24 |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 5  |       |       | 15 |       |       | 25 |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 6  |       |       | 16 |       |       |    |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 7  |       |       | 17 |       |       |    |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 8  |       |       | 18 |       |       |    |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 9  |       |       | 19 |       |       |    |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+
| 10 |       |       | 20 |       |       |    |       |       |
+----+-------+-------+----+-------+-------+----+-------+-------+

知道如何实现这一目标吗?

我已经走到这一步了

$table_string = 'Table Name 1:45,Table Name 2:20,Table Name 3:9';

function foo()
{
    $tables = explode(',', $table_string);
    $output = '';

    foreach ($tables as $table)
    {
        $table_name = explode(':', $table)[0];
        $table_rows = explode(':',$table)[1];
        $table_columns = ceil($table_rows/10);

        $output .= "<br>

        <table class='table table-bordered table-small'>
            <tr>
                <th scope='col' colspan='99'>{$table_name}</th>
            </tr>";

        for ($x = 1; $x <=10; $x++)
        {
            $output .= "<tr>";

            for ($y = 1; $y <= $table_columns * 3; $y++)
            {
                if ($y % 3 == 1) {
                    $output .= "<td scope=\"col\">Q</td>";
                } else if ($y % 3 == 2) {
                    $output .= "<td scope=\"col\">Tally</td>";
                } else  {
                    $output .= "<td scope=\"col\">Total</td>";
                }
            }

            $output .= "</tr>";
            $output .= "<tr>";

            for ($y = 1; $y <= $table_columns * 3; $y++)
            {
                if ($y == 1 || $y % 4 == 0) {
                    $z = ceil($y / 4);
                    $output .= "<td scope=\"row\">{$z}</td>";
                } else {
                    $output .= "<td></td>";
                }
            }

            $output .= "</tr>";
        }

        $output .= "</table>";
    }

    return $output;
}

为了帮助回答更多问题:这是我得到的当前输出:

+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| Q | Tally | Total | Q | Tally | Total | Q | Tally | Total | Q | Tally | Total | Q | Tally | Total |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+
| 1 |       |       | 1 |       |       |   | 2     |       |   |       | 3     |   |       |       |
+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+---+-------+-------+

最佳答案

试试这个代码:

    //no. of ques
    $total_ques = 45;
    //creating array for que no
    $que_nos = range(1,(int)$total_ques);
    $part = 10;

    //splitting array in chunks
    $cols = array_chunk($que_nos,$part);

    echo '<table border="1" cellpadding="5">';
    echo '<tr>';
    foreach($cols as $col) {
        //Generating heading columns
        echo "<td>Q</td>";
        echo "<td>Tally</td>";
        echo "<td>Total</td>";
    }
    echo '</tr>';

    //data for each row
    $row_data = [];
    for ($i=0; $i < $part; $i++) {
        //temporary variable containing values for each row
        $temp_row_data = [];
        foreach($cols as $k1 => $col) {
            //getting first value of array
            $value = reset($col);
            $temp_row_data[] = $value ?: '';
            if ($value !== false) {
                //unset value as it is already processed
                unset($cols[$k1][array_search($value,$col)]);
            }
        }
        //storing temporary array in main row array
        $row_data[] = $temp_row_data;
    }

    foreach ($row_data as $key => $cd) {
        echo '<tr>';
        foreach ($cd as $c) {
            echo "<td>{$c}</td>";
            echo "<td></td>";
            echo "<td></td>";
        }
        echo '</tr>';
    }

    echo '</table>';

Demo

输出将如下图所示 enter image description here

关于php - 使用 php 构建 html 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53434223/

相关文章:

php - 获取任意 mysql select 语句的计数(在 PHP 中),而不获取整个结果

javascript - 如何在javascript中向按钮添加点击事件?

php - SQL 根据表中最新的 150 个条目计算元素

php - 根据 IP 地址国家代码重定向访客

javascript - Dojo Chart X 轴名称的命名标签问题

javascript - 改进这个 "next","previous"脚本,使其可以上升到至少 3

javascript - 动态 HTML 页面内容

javascript - 将字符串从页面 URL 传递到 HTML <P> 标记

html - 如何使 contenteditable ='false' div 具有与 contenteditable ='true' 相同的高度?

php - 如何将表中的数据与当前年份和上一年进行比较