gpt4 book ai didi

php - 使用 GROUP BY 将时间作为类别列表 - MYSQL

转载 作者:行者123 更新时间:2023-11-29 02:23:02 26 4
gpt4 key购买 nike

我想使用 time 作为类别并且我有以下表格:

addonlist_final

enter image description here

表格插件

enter image description here

我希望输出看起来像这样:

Order at 11:17 am

Coke 1 Litre

Ham & Cheese Sandwich

Order at 11:15 am

Coke 1 Litre

Hotdog Sandwich

Tuna Sandwich

我目前所做的是这段代码,但只显示组中的第一个 item

<?php

$getaddons = mysql_query("SELECT a.addon_id, a.description, a.price, a.status, fl.faddon_id, fl.quantity, fl.timef
FROM addons AS a
LEFT JOIN addonlist_final AS fl ON a.addon_id = fl.faddon_id AND fl.ftransac_id = '96675'
GROUP BY fl.timef
LIMIT 10
");
while($rows = mysql_fetch_assoc($getaddons)){
$addonid = $rows['addon_id'];
$addondesc = $rows['description'];
$addonprice = $rows['price'];
$addonstat = $rows['status'];
$quantity = $rows['quantity'];
$timef = $rows['timef'];
$total = $addonprice * $quantity;

if($rows['faddon_id']){
echo "Ordered at $timef<br />";
echo "$addondesc<br />";
}
echo "<br />";
}

?>

输出:

Ordered at 11:15 am

Coke 1 Litre

Ordered at 11:17 am

Coke 1 Litre

最佳答案

尝试以下操作

<?php

include("conn.php");

date_default_timezone_set('Asia/Manila');
$time = date('h:i:s a', time());
$date = (date("D F d Y"));

echo $date." ".$time."<br /><br /><br />";

$getaddons = mysql_query("SELECT a.*, af.timef, af.quantity FROM addons AS a, addonlist_final AS af WHERE a.addon_id = af.faddon_id ORDER BY af.timef DESC");


$list = array();

while($rows = mysql_fetch_assoc($getaddons)){
$timef = $rows['timef'];
$list[$timef][] = $rows;
}

foreach($list as $time => $group) {
echo "Ordered at $time<br />";
foreach($group as $rows) {
$addonid = $rows['addon_id'];
$addondesc = $rows['description'];
$addonprice = $rows['price'];
$addonstat = $rows['status'];
$quantity = $rows['quantity'];
$timef = $rows['timef'];
$total = $addonprice * $quantity;

if($rows['faddon_id']){
echo "$addondesc<br />";
}
}
echo "<br />";
}

关于php - 使用 GROUP BY 将时间作为类别列表 - MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28122230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com