gpt4 book ai didi

php - 使用 php 构建 html 表

转载 作者:太空宇宙 更新时间:2023-11-04 14:50:37 25 4
gpt4 key购买 nike

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

如果变量为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com