2017-10-08 47 views
0

我正在通過OCI連接器查詢帶有SQL查詢的oracle數據庫。我期待根據一列中的值拆分我返回的數組。根據值從OCI拆分表

enter image description here

因此,在這個例子中,基於「位置」列中的值,我想分開的表。我已經能夠通過使用if((floorid == $ row ['LOCATION'])並且將floor ID設置爲其中一個值來分隔其中一個位置,但是我無法複製這些以用於所有可能性。複製標題任何幫助,將不勝感激

$stid = oci_parse($conn, $query); 
oci_bind_by_name($stid, ":getparam", $safeparam1); 

$r = oci_execute($stid); 

print '<hr>'; 
print '<table class="style1">'; 
Print '<tr><th>Name</th><th>Location</th><th>Colour</th></tr>'; 


while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS + OCI_ASSOC)) { 
    print '<tr>'; 
    foreach($row as $item) { 
     print '<td>' . ($item !== null ? htmlentities($item, ENT_QUOTES) : 
'') . '</td>'; 
    } 

    print '</tr>'; 
} 

print '</div>'; 

oci_free_statement($stid); 
oci_close($conn); 

回答

0

修改SQL命令由LOCATION場結果:

order by location 

然後,在你while循環,檢測時LOCATION變化值和開始新表:

$stid = oci_parse($conn, $query); 
oci_bind_by_name($stid, ":getparam", $safeparam1); 

$r = oci_execute($stid); 

print '<hr>'; 
print '<table class="style1">'; 
print '<tr><th>Name</th><th>Location</th><th>Colour</th></tr>'; 

$location = -1; // keeps track of the location change. 

while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS + OCI_ASSOC)) { 
    if ($location == -1) { 
     // seems to be the first row; set $location so we don't end up with an empty first table. 
     $location = $row['LOCATION']; 
    } 

    if ($row['LOCATION'] != $location) { 
     $location = $row['LOCATION']; 
     // close previous table; start new table. 
     print '</table><table class="style1">'; 
     print '<tr><th>Name</th><th>Location</th><th>Colour</th></tr>'; 
    } 

    print '<tr>'; 
    foreach ($row as $item) { 
     print '<td>' . ($item !== null ? htmlentities($item, ENT_QUOTES) : '') . '</td>'; 
    } 

    print '</tr>'; 
} 

print '</table>'; 

oci_free_statement($stid); 
oci_close($conn); 
+0

@JamesPavett對此有何反饋? – timclutton