php - 合并表中相同 ID 的数据。

标签 php mysql sql

我需要帮助将来自 MySQL 的数据与 PHP 结合起来并在同一个表中显示。

现在我明白了:

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 28      | Salad   |    4    | 10.99|
--------------------------------------

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 28      | Pizza   |    1    | 15   |
--------------------------------------

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 26      | Fish   |    3     | 12   |
--------------------------------------

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 22      | Pizza   |    1     | 15  |
--------------------------------------

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 22      | Salad   |    1    | 10.99|
--------------------------------------

我想这样显示:

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 28      | Salad   |    4    | 10.99|
| 28      | Pizza   |    1    | 15   |
--------------------------------------

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 26      | Fish   |    3     | 12   |
--------------------------------------

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 22      | Pizza   |    1     | 15  |
| 22      | Salad   |    1    | 10.99|
--------------------------------------

我的 PHP 代码:

$username2= $_SESSION['Username'];
$result = mysql_query("SELECT * FROM order_detail, products WHERE order_detail.user =   '$username2' AND order_detail.productid = products.serial ORDER BY orderid DESC");


while($row = mysql_fetch_array($result))
{
echo "<table class='curvedEdges'>
<tr>
<th>Order ID</th>
<th>Product</th>
<th>Quantity</th>
<th>Cost</th>
</tr>";

echo "<tr>";
echo "<td>" . $row['orderid'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['price'] . " LT</td>";
echo "</tr>";
}
echo "</table>";
}

在 PHP 中执行此操作的最简单方法是什么?也许有人可以告诉我代码? :) 谢谢!

最佳答案

像这样的东西应该可以工作:

$orderID = null;
$tableShown = false;

while($row = mysql_fetch_array($result)) {
    if ($orderID != $row['orderid']) {
        if ($tableShown)
            echo "</table>";

        echo "<table class='curvedEdges'>
        <tr>
        <th>Order ID</th>
        <th>Product</th>
        <th>Quantity</th>
        <th>Cost</th>
        </tr>";
        $tableShown = true;
    }

    echo "<tr>";
    echo "<td>" . $row['orderid'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['quantity'] . "</td>";
    echo "<td>" . $row['price'] . " LT</td>";
    echo "</tr>";

    $orderID = $row['orderid'];
}

if ($tableShown)
    echo "</table>";

这会导致 HTML table 重新创建,其中包含订单 ID 中每次更改的 header 。

关于php - 合并表中相同 ID 的数据。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16654990/

相关文章:

sql - 使用 "select *"时可以格式化列吗?

php - 我应该如何在 Laravel 中正确构建我的单元测试

php - 数据库未使用 PDO 语句更新?

c# 使用 MySQL 数据库中的后台工作程序每 5 秒更新一次数据 GridView

mysql - SQL查询: How to Get tweet from following?

java - 通过 hibernate 获取不同的值

php - 在函数中的方法之外访问 php 变量

php - mysql wordpress php 语法错误

php - PHP require、require_once 或 include 是否会在页面加载时自动读取文件?

MySQl 我需要使一列仅对于另一列的某些值唯一