如何在表中每10列自动添加一个新行


How can I add a new row in a table every 10 columns automatically?

我有两个查询,从数据库中提取两个不同的数据集第一个包含表的标题,所以如果总结果是10,那么我们有10个表的标题。

第二个将有记录,每个记录每列有一个值。因此,如果我有5条记录,这意味着在第二个数据集中有5 x 10(总标题)=50条记录。

那50条记录我想把它显示在表中。

我的方法是一次显示一条记录,但在每10条记录之后关闭并为下一行打开一条新记录。

我不确定这是否是解决这个问题的最佳方法,但我愿意接受更好的想法。

假设我的方法是一个好方法,我如何在每10条记录之后在表中创建一个新行。

我试图通过在PHP中使用Mod操作来简化这一点,但这对我来说不起作用

这是我当前显示数据的代码,但它没有在正确的时间/地点添加。

我的问题是如何添加修复此代码以正确显示结果?

    //count of headers  
    $total_th = count($headers);
    //generate the headers
    $report_rows = '<thead><tr><th>Company Code</th>';
    foreach($headers AS $head){
        $report_rows .= '<th>'.$head['title'].'</th>';
    }   
    $report_rows .= '</tr></thead>';

    //count of the the actual results
    $total_results = count($results);
    //create the table body
    $report_rows .= '<tbody>';
    //loop all of the records
    for($i=0; $i< $total_results; ++$i){
    $row = $results[$i];
    //start new row "Add this only once per row
        if($i == 0 ||  $i % $total_th == 0){
        $report_rows .= '<tr>';
        $report_rows .= '<td>'.$row['company_code'].'</td>';
        }
    //display all answers
    $report_rows .= '<td>'.$row['answer'].'</td>';
    //close row if the $total_th is reached 
        if( $i % $total_th == 0){
        $report_rows .= '</tr>';
        }
    }
    //close tbody and table
    $report_rows .= '</tbody>';
echo '<table class="common2">';
echo $report_rows;
echo '</table>';

您可以使用模数运算。

$i = 1;
foreach($records as $record){
echo $record;
if ($i % 10 == 0)
   echo '<hr />';
$i++;
}