[英]PHP to display rows from MySQL query based on id and group by datewise
The select query outputs values for each basestation_id on a particular date wise sorted by the avg. 选择查询在特定日期输出按平均值对每个basestation_id的值。 I would like to display the table in php from Mysql query result as shown below.
我想从MySQL查询结果中以php显示表格,如下所示。
basestation_id 12-Jun-18 13-Jun-18 14-Jun-18 15-Jun-18 16-Jun-18
5 value1 value2 value3 value4 value5
3 value1 value2 value3 value4 value5
4 value1 value2 value3 value4 value5
1 value1 value2 value3 value4 value5
2 value1 value2 value3 value4 value5
But the code display results as 但是代码显示为
date 12-Jun-18 12-Jun-18 12-Jun-18 12-Jun-18 13-Jun-18 ...
basetstaion_id 5 3 4 1 2 ...
values value1 value1 value1 value1 value2 ...
PHP:- PHP:-
<?
foreach($result as $key => $row) {
foreach($row as $field => $value) {
$recNew[$field][] = $value;
}
}
//This creates a new array composed/transposed with the field names as keys
//and the "rowed" values as sub-arrays.
echo "<table>\n";
foreach ($recNew as $key => $values) // For every field name (basestation_id,
// , dates, values)
{
echo "<tr>\n"; // start the row
echo "\t<td>" . $key . "</td>\n" ; // create a table cell with the field
name
foreach ($values as $cell) // for every sub-array iterate through all
values
{
echo "\t<td>" . $cell . "</td>\n"; // write cells next to each other
}
echo "</tr>\n"; // end row
}
echo "</table>";
?>
Sample Data:- 样本数据:-
delivery_date col_head basestation_id receptions daily_avg_stats
16-Jun-18 Sat 16 Jun 152 0 under_avg
16-Jun-18 Sat 16 Jun 13 0 under_avg
16-Jun-18 Sat 16 Jun 140 0 under_avg
16-Jun-18 Sat 16 Jun 12 1093 under_avg
16-Jun-18 Sat 16 Jun 9 0 under_avg
16-Jun-18 Sat 16 Jun 1 50 under_avg
16-Jun-18 Sat 16 Jun 5 250 under_avg
. . . .
15-Jun-18 Fri 15 Jun 152 0 under_avg
15-Jun-18 Fri 15 Jun 13 0 under_avg
15-Jun-18 Fri 15 Jun 140 0 under_avg
15-Jun-18 Fri 15 Jun 12 1734 under_avg
15-Jun-18 Fri 15 Jun 9 0 under_avg
15-Jun-18 Fri 15 Jun 1 0 under_avg
SQL Query to display the results with "under avg" values listed first:- SQL查询以首先显示“低于平均”值的结果显示:
SELECT
*
FROM
(SELECT
DATE(r.ad_date) AS delivery_date,
DATE_FORMAT(r.ad_date, '%a %d %b') AS col_head,
r.basestation_id,
r.receptions,
(CASE
WHEN r.receptions > FLOOR(avg.monthly_receptions_avg * 1.3) THEN 'over_avg'
WHEN r.receptions < FLOOR(avg.monthly_receptions_avg * 0.7) THEN 'under_avg'
END) AS daily_avg_stats
FROM
receptions r
JOIN receptions_avg avg ON r.basestation_id = avg.basestation_id
WHERE
r.ad_date > DATE_SUB(CURDATE(), INTERVAL 30 DAY)
AND avg.ad_date = CURDATE() - INTERVAL 1 DAY
ORDER BY basestation_id , delivery_date) abc
ORDER BY delivery_date DESC , FIELD(abc.daily_avg_stats, 'under_avg') DESC
Use This code 使用此代码
<?
foreach($result as $key => $row) {
foreach($row as $field => $value) {
$recNew[$field][] = $value;
}
}
//This creates a new array composed/transposed with the field names as keys
//and the "rowed" values as sub-arrays.
echo "<table>";
foreach ($recNew as $key => $values) // For every field name (basestation_id,
// , dates, values)
{
echo "<tr>"; // start the row
echo "<td>" . $key . "</td>" ; // create a table cell with the field
name
foreach ($values as $cell) // for every sub-array iterate through all
values
{
echo "<td>" . $cell . "</td>"; // write cells next to each other
}
echo "</tr>"; // end row
}
echo "</table>";
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.