我有 2 个 db 调用,它们运行一个循环来获取所有结果。
首先,我得到了相对于当年的结果。
其次,我得到了相对于上一年的结果(-1)。
如何将第二个循环的输出中的月份与第一个循环的同一个月对齐?
public function cargovolumes_cy() {
global $wpdb;
$cargodb = new $wpdb('root', 'devpw', 'exdb', 'localhost');
$currentmonthtxt = date('M');
$currentyear = date('Y');
$cargovolume_cy = array();
foreach($cargodb->get_results(
"
SELECT *, SUM(tonneCount)
FROM volumes
WHERE year = $currentyear
GROUP BY terminal, year, month
ORDER BY month desc, year desc
"
) as $key => $row) {
$tonnages = $row->tonneCount;
$terminal = $row->terminal;
$year = $row->year;
$month = $row->month;
$cargovolume_cy[] =
'<h4 class="cv-terminal-title">'.$terminal.' </h4>'.
'<div class="cargovolumes_cy">'.
'<div class="cargovolumes_cy-dates cvrow-'.$month.'-'.$year.'">'.
'<span class="cy-month"> '.$month.' </span>'.
'<span class="cy-year"> '.$year.' </span></div>'.
'<div class="cy-year-bar"><div class="cy-tonnage-bar"> </div>'.
'<span class="cy-tonnage" value="'.$tonnages.'"> '.$tonnages.' </span></div>'.
'</div>';
};
return $cargovolume_cy;
}
public function cargovolumes_ly() {
global $wpdb;
$cargodb = new $wpdb('root', 'devpw', 'exdb', 'localhost');
// $stable = $shipdb->get_results("SELECT * FROM volumes", ARRAY_A);
$currentmonthtxt = date('M');
$currentyear = date('Y');
$cargovolume_ly = array();
foreach($cargodb->get_results(
"
SELECT *, SUM(tonneCount)
FROM volumes
WHERE year = $currentyear -1
GROUP BY terminal, year, month
ORDER BY month desc, year desc
"
) as $key => $row) {
$tonnages = $row->tonneCount;
$terminal = $row->terminal;
$year = $row->year;
$month = $row->month;
$cargovolume_ly[] =
'<div class="cargovolumes_ly">'.
'<div class="cargovolumes_ly-dates">'.
'<span class="ly-month"> '.$month.' </span>'.
'<span class="ly-year"> '.$year.' </span></div>'.
'<div class="ly-year-bar"><div class="ly-tonnage-bar"> </div>'.
'<span class="ly-tonnage" value="'.$tonnages.'"> '.$tonnages.' </span></div>'.
'</div>';
};
return $cargovolume_ly;
}
结果应输出 2 个具有匹配月份和年份的元素。相反,我在第一个输出中获得了最近一个月,在第二个输出中与一年中的最后一个月配对。
下面是容器元素:
@for ($i = 0; $i < min(count($cargovolumes_cy), count($cargovolumes_ly)); $i++)
<div class="page_cargovolumes-info_grid-cy_item">
{!! $cargovolumes_cy[$i] !!}
{!! $cargovolumes_ly[$i] !!}
</div>
@endfor