表与动态循环

时间:2018-05-28 07:04:44

标签: 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>';
    }
?>

1 个答案:

答案 0 :(得分:2)

我相信以下代码应该为您完成这项工作:

$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>';
        }

            ?>