我需要帮助从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
重新创建,并为Order ID中的每个更改提供标题。
我认为你需要两个循环来实现这个-
$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>";
}