php - 动态循环表
问题描述
我创建了带有动态循环的表(如果我有 100 条记录,它应该在一张表中只显示 15 条记录)
有了这个,我有 20 条记录,创建了两个表,但问题是列动态没有得到并且正在显示相同的记录,用于在一个表中显示 15 条记录和 5 条记录在另一个表中:
这是我的代码:
<?php
$total = $totalQues / 15;
for ($row = 0; $row < $total; $row++)
{
echo '<table aria-describedby="example2_info" id="example2" class="table table-bordered table-hover dataTable custom-table pull-left col-xs-offset-1 col-lg-offset-2 col-sm-offset-2 col-md-offset-2">';
echo '<tbody aria-relevant="all" aria-live="polite" role="alert">';
echo '<tr class="odd black">';
echo '<td class=" text-center">Q</td>';
echo '<td class="text-center">C</td>';
echo '<td class="text-center">U</td>';
echo '</tr>';
$j = 0;
$k = 0;
$l = 0;
for ($i = 0; $i < 15; $i++)
{
$j = $i + $totalQues;
$k = $i + 30;
$l = $i + 45;
$m = $i + 1;
$n = $k + 1;
$o = $l + 1;
if (empty($r[1][$i]->corr_ans))
{
echo '<tr style = "color:green;">
<td>' . $m . '</td>
<td></td>
<td></td>
</tr>';
}
else
{
$uans = strtolower($r[1][$i]->corr_ans);
$cans = strtolower($r[1][$i]->user_ans);
$u = ord($uans);
$cr = ord($cans);
// echo "u=".$u."cr=".$cr;
if ($u == $cr):
echo '<tr style = "color:green;">
<td>' . $m . '</td>
<td>' . strtoupper($r[1][$i]->corr_ans) . '</td>
<td>' . strtoupper($r[1][$i]->user_ans) . '</td>
</tr>';
else:
echo '<tr style = "color:red;">
<td>' . $m . '</td>
<td>' . strtoupper($r[1][$i]->corr_ans) . '</td>
<td>' . strtoupper($r[1][$i]->user_ans) . '</td>
</tr>';
endif;
}
}
echo '</tbody>';
echo '</table>';
}
?>
解决方案
我相信以下代码应该为您完成这项工作:
$numberOfTables = $totalQues/15;
$maxRows = 15
for($table = 0; $table < $numberOfTables; $table ++){
echo '<table aria-describedby="example2_info" id="example2" class="table table-bordered table-hover dataTable custom-table pull-left col-xs-offset-1 col-lg-offset-2 col-sm-offset-2 col-md-offset-2">';
echo '<tbody aria-relevant="all" aria-live="polite" role="alert">';
echo '<tr class="odd black">';
echo '<td class=" text-center">Q</td>';
echo '<td class="text-center">C</td>';
echo '<td class="text-center">U</td>';
echo '</tr>';
$tableMax = ($table < $numberOfTables-1 ? $maxRows*($table+1) : $totalQues)
for($row = $table*$maxRows; $row < $tableMax; $row++){
if (!empty($r[1][$row]->corr_ans)) {
$uans = strtolower($r[1][$row]->corr_ans);
$cans = strtolower($r[1][$row]->user_ans);
$u = ord($uans);
$cr = ord($cans);
//echo "u=".$u."cr=".$cr;
$color = ($u == $cr ? 'green' : 'red');
echo '<tr style = "color:'.$color.'">
<td>' . $row . '</td>
<td>' . strtoupper($r[1][$row]->corr_ans) . '</td>
<td>' . strtoupper($r[1][$row]->user_ans) . '</td>
</tr>';
}
}
echo '</tbody>';
echo'</table>';
}
?>
推荐阅读
- php - 使用 Image header() 和 readfile() 在 PHP 页面上显示 Base64 图像
- pandas - 这些都是在数据框中删除一行的有效方法吗?
- php - 填充数组,无论是否有空检查
- go - 如何在不指定精度的情况下从 big.Rat 转换为字符串?
- sql - SQL 查找 LIKE 记录的重复项
- c# - 更改 MapPolyline 的颜色
- php - jQuery函数在数据库中更新时不更新数据
- r - 如何使用 lapply 替换矩阵?
- python - 如何在 sklearn 中进行小批量梯度下降?
- rust - 是否可以在 rust 中写入值大于 127 的文字字节字符串?