1

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でこれを行う最も簡単な方法は何ですか? 多分誰かが私にコードを見せることができますか? :) ありがとう!

4

2 に答える 2

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

これにより、注文 ID が変更されるたびに HTMLtableがヘッダー付きで再作成されます。

于 2013-05-20T17:36:55.427 に答える