2017-10-08 8 views
0

OCI 커넥터를 통해 SQL 쿼리로 Oracle 데이터베이스를 쿼리하고 있습니다. 하나의 열에있는 값을 기반으로 반환하는 배열을 분할하려고합니다. 값을 기준으로 OCI에서 테이블 분할

enter image description here

그래서이 예에서 "위치"열에있는 값을 기준으로, 나는 분리 된 테이블을 싶습니다. if (($ floorid == $ row [ 'LOCATION'])을 사용하여 분리 된 위치 중 하나를 가져올 수 있었고 바닥 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

LOCATION 필드에 의한 결과를 정렬하기 위해 SQL을 수정합니다.

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