我做了一个查询,其中显示了登录电子邮件 ID 的宝贵购买数据。我已经成功地做到了这一点。现在我想根据日期显示数据。这里它显示每行的日期。有什么建议请!!
代码
<h3>My Orders</h3>
<table border="1">
<?php
$query="select orders.date,order_detail.quantity,order_detail.price,order_detail.color,order_detail.size,customers.name,products.product_name,products.product_image from order_detail JOIN orders on orders.serial=order_detail.orderid Join customers on customers.serial=orders.customerid Join products on products.productid=order_detail.productid where customers.email='$email'";
$sql=mysqli_query($con,$query);
while($row=mysqli_fetch_array($sql))
{
?>
<tr>
<td><?php echo $row['date'] ?></td>
<td><image width="80px" height="90px" src="images/images/<?php echo $row['product_image'] ?>"/></td>
<td><?php echo $row['product_name']. "*". $row['quantity']?></td>
<td><?php echo $row['color'] ?></td>
<td><?php echo $row['price'] ?></td>
<td><?php echo $row['size'] ?></td>
</tr>
<?php
}
?>
</table>
最佳答案
您是否尝试过根据日期对它们进行排序?通过添加“ORDER BY column ASC
”来增加值或添加“ORDER BY column DESC
”来减少值,可以轻松完成此操作!
$query="SELECT orders.date, order_detail.quantity, order_detail.price,
order_detail.color, order_detail.size,
customers.name,
products.product_name,
products.product_image
FROM order_detail
JOIN orders ON orders.serial=order_detail.orderid
JOIN customers ON customers.serial=orders.customerid
JOIN products ON products.productid=order_detail.productid
WHERE customers.email='$email'
ORDER BY orders.date";
看看manual
如果您想将相同的日期分组在一起,请尝试使用 GROUP BY 命令(如果orders.date 的类型为 DATE/DATETIME/TIMESTAMP,则此示例有效):
$query="SELECT orders.date, order_detail.quantity, order_detail.price,
order_detail.color, order_detail.size,
customers.name,
products.product_name,
products.product_image
FROM order_detail
JOIN orders ON orders.serial=order_detail.orderid
JOIN customers ON customers.serial=orders.customerid
JOIN products ON products.productid=order_detail.productid
WHERE customers.email='$email'
GROUP BY DATE( orders.date )
ORDER BY orders.date";
如果您使用 PHP 时间戳或 INT,则需要像这样转换日期:GROUP BY DATE( FROM_UNIXTIME(orders.date ) )
。
DATE(col) 将日期/日期时间/时间戳转换为单日。 GROUP BY 将具有相同 DATE 的所有行放入一行。现在您可能还想将显示的数据更改为 SUM(order_detail.price) 以获取当天的总价,依此类推。
关于php - 根据日期在不同的表中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34159257/