根据值从OCI拆分表

时间:2017-10-08 16:46:58

标签: php html oracle oci

我通过OCI连接器查询带有SQL查询的oracle数据库。我希望根据一列中的值拆分我返回的数组。

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);

1 个答案:

答案 0 :(得分: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);