PHP查询数据然后自动行跨度表td如果相同的值数据


PHP Query Data then Auto rowspan table td if same value data

如何在查询数据结果时自动行跨<td>表。

示例:

SHP NO | SN
05 | M001
05 | M002
05 | M003
06 | M006
06 | M007
06 | M008

将:

SHP NO | SN
       | M001
05     | M002
       | M003
-------------
       | M006
06     | M007
       | M008

因此,如果数据值相同,它将自动在td表中进行跨行。

这是我的查询代码:

$q1 = "SELECT * FROM DIGI_SHIPMENT_SCAN WHERE TO_CHAR(SHIPMENT_DATE,'YYYY-MM-DD') BETWEEN '$start_date' AND '$end_date'";
$result_q1 = oci_parse($c1, $q1);
oci_execute($result_q1);
while($data = oci_fetch_array)
{
?>
<td></td>
<?php
}

请帮忙。

  1. 首先,按shp_no对结果进行排序
  2. shp_no不变时,保存要在buffer中打印的
  3. 如果遇到不同的shp_no,请打印buffer并将其重置

方法如下:

// Order the results by shp_no
$q1 = "SELECT * FROM DIGI_SHIPMENT_SCAN WHERE TO_CHAR(SHIPMENT_DATE,'YYYY-MM-DD') BETWEEN '$start_date' AND '$end_date' ORDER BY shp_no ASC";
$result_q1 = oci_parse($c1, $q1);
oci_execute($result_q1);
$curr_shpno = NULL; // The current shp_no
$buffer = "";
$count = 0;  // Number of rows of data (used to set the rowspan)
while($data = oci_fetch_array($result_q1))
{
    // If the shp_no has changed, print the rows and reset the buffer
    if($curr_shpno != $data['shp_no']) {
        // Print the rows
        if($count > 0) { // Handle the first shp_no
            // Print the starting <TR> and the <TD> containing the shp_no
            echo "<tr><td rowspan=$count>$curr_shpno</td>";
            echo $buffer;
        }
        $curr_shpno = $data['shp_no'];
        // Reset the buffer, and save the new row (Value of shp_no
        // and the starting <TR> will be added later)
        $buffer = "<td>" . $data['sn'] . "</td></tr>";
        $count = 1;
    }
    else {
        // Save the new row
        $buffer += "<tr><td>" . $data['sn'] . "</td></tr>";
        $count++;
    }
}