2014-11-20 67 views
0

我有一個while循環,它將所有鏈接的數字分隔到表中的不同部分。這是聲明:while循環 - 從循環結果中分離標題 - 循環,然後標題爲每個結果集

$result2 = mysqli_query($con,"SELECT a.*, b.*, c.* FROM b_statement_main a 
INNER JOIN b_crm_company b ON a.COMPANY_ID = b.ID INNER JOIN b_statement c 
ON a.LINK = c.LINK 
WHERE a.COMPANY_ID = '$companyID2' AND a.COMMENCE BETWEEN '$from2' AND '$to2' "); 

$linked = -1; 
while($row = mysqli_fetch_array($result2)) 
{ 

    $companyName = $row['TITLE']; 
    $arDates = strtotime($row['COMMENCE']); 
    $remaining = $row['REMAINING_HOURS']; 
    $linking = $row['LINK']; 
    $arNumber = $row['REF_NUMBER']; 
    $billable = $row['BILLABLE']; 


    if($linked != $row['LINK']) 
    { 
    print "<tr><td><strong>$companyName</strong></td><td></td><td><strong>".date('d/m/Y',$arDates)."</strong></td><td></td><td></td><td><strong>$remaining</strong></td></tr>"; 

    $linked = $row['LINK']; 
    } 

    print "<tr><td></td><td></td><td></td><td>$arNumber</td><td>$billable</td><td></td></tr>"; 

} 

print "</table>"; 

所以這是什麼返回是每個部分的標題,然後它循環下面的查詢結果。一旦它返回了所有相應的結果,它將再次顯示標題部分並再次循環相關結果。它會重複此操作直到顯示所有內容。

但是我想要的是標題部分低於循環結果。因此,它會循環播放結果,然後一旦它將所有這些特定部分循環播放,將標題放在下面。然後,它將再次進入並循環結果,並將標題置於下方,並繼續執行,直至完成。

任何人有任何想法我可以做到這一點。顯然,如果我在,如果之前移動打印,然後把結果,標題,結果,標題等

所以這是它看起來像此刻(不準確的,但只顯示標題和結果) :

| Company | Date  | Hours | 
| Title 1 | 20/11/2014 |  | 
| Result 1 | 10/11/2014 | 44 | 
| Result 2 | 08/11/2014 | 22 | 

當我需要它像:

| Company | Date  | Hours | 
| Result 1 | 10/11/2014 | 44 | 
| Result 2 | 08/11/2014 | 22 | 
| Title 1 | 20/11/2014 |  | 

回答

0

如何創建基於您在表中所需條目的新數組。所以你的代碼會像(注意$outputarray被添加):

編輯:這假定每個數據集只有一個標題。如果您希望每個記錄集有多個標題,則需要在記錄中設置標識集的內容。在這種情況下,最簡單的方法是根據它是否是標題(按照您的標準)製作結果的多維數組。

$linked = -1; 
while($row = mysqli_fetch_array($result2)){  
    $companyName = $row['TITLE']; 
    $arDates = strtotime($row['COMMENCE']); 
    $remaining = $row['REMAINING_HOURS']; 
    $linking = $row['LINK']; 
    $arNumber = $row['REF_NUMBER']; 
    $billable = $row['BILLABLE']; 

    if($linked != $row['LINK']){ 
     $outputarray['title'][] ="<tr><td><strong>$companyName</strong></td> 
      <td></td><td><strong>".date('d/m/Y',$arDates)."</strong></td><td></td><td> 
      </td><td><strong>$remaining</strong></td></tr>"; 
     $linked = $row['LINK']; 
    } else { 
     $outputarray['row'][]= "<tr><td></td><td></td><td></td><td>$arNumber</td> 
      <td>$billable</td><td></td></tr>"; 
    } 
    } 
    print"<table> 
     <thead>"; // etc... have the titles go here. 
    foreach($outputarray as $key=> $value){ 
     if($key=='title'){ 
      $thistitle=$value; 
     } else { 
      print $value; 
     } 
    } 
    print $thistitle; 

    print "</table>"; 
+0

顯示結果與我現在的結果完全相同 – andy 2014-11-20 17:28:19

+0

什麼是鏈接有多個標題?你會只顯示一個標題?你可以分割兩個數組(例如'outputarrayTitle'和'$ outputarrayContent'),並將它們分開打印,但是來自查詢的數據可能與你的視覺不符。 – Sablefoste 2014-11-20 17:34:26

+0

標題總是會分隔結果,我編輯了我的問題向你展示我在底部的意思 – andy 2014-11-20 18:31:07