php - 具有动态循环的表

标签 php loops for-loop

我创建了动态循环表(如果我有 100 条记录,则一张表中应该只显示 15 条记录)

有了这个,我已经完成了创建两个表的 20 条记录,但问题是列动态未获取并且显示相同的记录,用于在一个表中显示 15 条记录,在其他表中显示 5 条记录:

my table

这是我的代码:

<?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 - 具有动态循环的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50560834/

相关文章:

PHP/MYSQL上传,导入.csv文件到mysql-process-table design

javascript - 有没有更优雅的方式来编写这个循环?

bash - 在命令行上解析 shell 脚本的输入

python - 为什么在循环变量使用相同名称时列表会被最后一项覆盖?

php - 如何使用 php 分解 mysql 数据库中已经存在的 json 数据?

php - Laravel 4 的 Ajax

php - 如何使用旧数据填充新的和修改后的 MySQL 数据库

r - 将revgeocode应用于经纬度坐标列表

python - For 循环 vs Numpy 向量化计算时间

python - 如何使用一个 for 循环来使用两个不同的 .format()