2014-11-20 131 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