0

我目前正在使用以下内容显示一个充满结果的表格:

$result = mysqli_query($con,"SELECT * FROM b_tasks_po");

while($row = mysqli_fetch_array($result))

{
echo "<tr>";
echo "<td class='tdclass' width='100'>" . $row['TASK_ID'] . "</td>";
echo "<td class='tdclass' width='100'>" . $row['QTY'] . "</td>";
echo "<td class='tdclass' width='100'>" . $row['CODE'] . "</td>";
echo "<td class='tdclass' width='500'>" . $row['PRODUCT_DESCRIPTION'] . "</td>";
echo "</tr>";
}                   
?>

</table>

这带来了以下内容:

| Task ID  |  Qty  |  Code  |  Description
| 1        |  200  |  AB1   |  Frame 1
| 1        |  350  |  AB2   |  Frame 2
| 2        |  100  |  AB3   |  Frame 3
| 2        |  120  |  AB4   |  Frame 4
| 2        |  300  |  AB5   |  Frame 5

这就是我想要的,我不确定我该怎么做。我需要为每组 ID 创建一个新表。例如:

TABLE ONE

| Task ID  |  Qty  |  Code  |  Description
| 1        |  200  |  AB1   |  Frame 1
| 1        |  350  |  AB2   |  Frame 2

TABLE TWO

| Task ID  |  Qty  |  Code  |  Description
| 2        |  100  |  AB3   |  Frame 3
| 2        |  120  |  AB4   |  Frame 4
| 2        |  300  |  AB5   |  Frame 5

我考虑过按 task_id 对结果进行分组,但并不高兴,而且我还考虑过使用 if 语句,但我一生都无法弄清楚如何去做。

任何帮助,将不胜感激。

4

2 回答 2

1

当 task_id 发生变化时,您需要对结果进行排序并重新打印标题。例子:

<?
$result = mysqli_query($con,"SELECT * FROM b_tasks_po ORDER BY TASK_ID");

$current_task_id = -1;
while($row = mysqli_fetch_array($result))
{
    if($current_task_id != $row['TASK_ID'])
    {
        echo "</table><table>";
        echo "<tr>";
        echo "<th width='100'>Task ID</th>";
        echo "<th width='100'>Qty</th>";
        echo "<th width='100'>Code</th>";
        echo "<th width='500'>Description</th>";
        echo "</tr>";
        $current_task_id = $row['TASK_ID'];
    }
    echo "<tr>";
    echo "<td class='tdclass' width='100'>" . $row['TASK_ID'] . "</td>";
    echo "<td class='tdclass' width='100'>" . $row['QTY'] . "</td>";
    echo "<td class='tdclass' width='100'>" . $row['CODE'] . "</td>";
    echo "<td class='tdclass' width='500'>" . $row['PRODUCT_DESCRIPTION'] . "</td>";
    echo "</tr>";
}

?>

</table>
于 2013-10-29T14:40:40.007 回答
0

我认为你不需要两张桌子。我认为您需要两个结果集。您正在寻找的是一个where子句:

SELECT *
FROM b_tasks_po
WHERE id = 1;

如果你确实想要创建两个表(一般来说这似乎是个坏主意),那么请查看create table asMySQL 中的功能。

于 2013-10-29T14:36:37.027 回答