【问题标题】:Table with dynamic looping动态循环表
【发布时间】:2018-05-28 07:04:44
【问题描述】:

我创建了带有动态循环的表(如果我有 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>';
    }
?>

【问题讨论】:

  • 任何人请帮我解决这个问题
  • 您想要$total 的表数或行数吗?我真的无法理解。
  • 由于您的代码无法正常工作,因此我们使用您的代码来确定您正在尝试做什么并不是一个好主意。也许你可以用通俗易懂的英语解释你想要做什么。就像函数接受参数 a 和 b,a 是 ... b 是 ... 它会返回 ... 取决于 ...
  • 表格数量。但在一个 for 循环中创建相同的记录 @seethrough 你可以参考我的图片我 atteched
  • @HMR 我有 20 条记录作为 totalQues,我必须创建一个限制为 15 列的表,其余 5 列必须显示另一个表

标签: php loops for-loop


【解决方案1】:

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

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

            ?>

【讨论】:

  • @HMR 没有先生我在表上出现错误,一个表创建 15 行但另一个缺少一整列
  • @GaganTej 我想你把最后一条评论发给了另一个人,其次,我更新了我的答案,试试看
  • @GaganTej 很抱歉进行了这么多更正,我不确定您的数据结构是什么样的。如果它不再工作,请留言
  • A "have a try on this" 不足以解释您的更改。
  • @RokoC.Buljan 我已经改了好几次了,这是我第一次发布答案的时候
猜你喜欢
  • 2016-07-09
  • 2021-12-24
  • 2020-03-30
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
  • 2019-08-19
  • 2016-10-16
  • 2020-05-25
相关资源
最近更新 更多