2013-06-28 104 views
0

我有兩個查詢,從數據庫中拉2個不同的數據集 第一個包含表頭,所以如果總結果是10,那麼我們有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>'; 
+0

循環如何進行? – hjpotter92

+0

這真的很簡單,請在來這裏之前表明你已經做了一些嘗試。 – Barmar

+0

@Barmar我只是用我的嘗試更新我的問題 – Mike

回答

4

您可以使用modulus operation

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

由於某種原因,它現在正在爲我工​​作。請檢查我的帖子,因爲我修改了它。 – Mike

相關問題