[英]How can I add a new row in a table every 10 columns automatically?
我有2個查詢,它們從數據庫中提取2個不同的數據集,第一個包含表的標題,因此,如果總結果為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++;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.