将表中的数据组合为相同的ID。

时间:2013-05-20 17:34:13

标签: 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中执行此操作的最简单方法是什么?也许有人可以告诉我代码? :) 谢谢!

2 个答案:

答案 0 :(得分:2)

这样的事情应该有效:

$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中每次更改的标头。

答案 1 :(得分:0)

我认为你需要进行两次循环才能实现这一目标 -

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

    while($row2 = mysql_fetch_array($result2))
    {
    $row2orderid = $row2['orderid'];

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

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

    while($row = mysql_fetch_array($result))
    {    
    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>";
    }