我正在创建一个脚本以创建项目摘要。唯一看起来没有正常工作的是项目汇总金额。也就是说,这些三线的价格,而不是和显示的最后一个值:创建正确的数组摘要
echo "<td><strong>" . number_format($summary['counterExtended'], 2) . "</strong></td>";
echo "<td><strong>" . number_format($summary['onlineExtended'], 2) . "</strong></td>";
echo "<td><strong>" . number_format($summary['voucherExtended'], 2) . "</strong></td>";
即使我已经在这里被加入其中:
if (true === isset($lineItems[$category][$description])) {
$data = $lineItems[$category][$description];
$data['quantity'] += $qty;
$data['counterExtended'] += $counterExtended;
$data['onlineExtended'] += $onlineExtended;
$data['voucherExtended'] += $voucherExtended;
$lineItems[$category][$description] = $data;
$categoryTotals[$category]['counterExtended'] += $counterExtended;
$categoryTotals[$category]['onlineExtended'] += $onlineExtended;
$categoryTotals[$category]['voucherExtended'] += $voucherExtended;
我会注意到,数量增加只要不是$ counterExtended,$ onlineExtended,$ voucherExtended。它们只显示最后一个值,而不是每个月为每个项目添加所有值。
这里是SQL循环:
$s->bind_result($id, $qty, $price, $online, $voucher, $month, $year, $description, $cat, $category);
$lineItems = array();
$categoryTotals = array();
while($s->fetch()) {
if ($online == 0 && $voucher == 0) {
$counterExtended = $qty * $price;
$onlineExtended = 0;
$voucherExtended = 0;
}
if ($online == 1) {
$counterExtended = 0;
$onlineExtended = $qty * $price;
$voucherExtended = 0;
}
if ($voucher == 1) {
$counterExtended = 0;
$onlineExtended = 0;
$voucherExtended = $qty * $price;
}
// Repeated item, increment values accordingly
if (true === isset($lineItems[$category][$description])) {
$data = $lineItems[$category][$description];
$data['quantity'] += $qty;
$data['counterExtended'] += $counterExtended;
$data['onlineExtended'] += $onlineExtended;
$data['voucherExtended'] += $voucherExtended;
$lineItems[$category][$description] = $data;
$categoryTotals[$category]['counterExtended'] += $counterExtended;
$categoryTotals[$category]['onlineExtended'] += $onlineExtended;
$categoryTotals[$category]['voucherExtended'] += $voucherExtended;
// First time hitting an item with this description
} else {
$lineItems[$category][$description] = array(
'category' => $category,
'description' => $description,
'id' => $id,
'counterExtended' => $counterExtended,
'onlineExtended' => $onlineExtended,
'voucherExtended' => $voucherExtended,
'quantity' => $qty,
'year' => $year,
);
$categoryTotals[$category] = array(
'counterExtended' => $counterExtended,
'onlineExtended' => $onlineExtended,
'voucherExtended' => $voucherExtended,
);
}
}
// Now loop through the $lineItems array and output rows.
foreach ($categoryTotals as $category => $summary) {
while ($item = array_shift($lineItems[$category])) {
echo "<tr>";
echo '<td>'.$item['year'].'</td>';
echo '<td>'.$item['id'].'</td>';
echo '<td>'.$item['quantity'].'</td>';
echo "<td></td>";
echo "<td>" . number_format($item['counterExtended'],2) . "</td>";
echo "<td>" . number_format($item['onlineExtended'],2) . "</td>";
echo "<td>" . number_format($item['voucherExtended'],2) . "</td>";
echo '<td>'.$item['description'].'</td>';
echo '<td>'.$item['category'].'</td>';
echo "</tr>\n";
}
echo "<tr>";
echo "<td colspan=4></td>";
echo "<td><strong>" . number_format($summary['counterExtended'], 2) . "</strong></td>";
echo "<td><strong>" . number_format($summary['onlineExtended'], 2) . "</strong></td>";
echo "<td><strong>" . number_format($summary['voucherExtended'], 2) . "</strong></td>";
echo "<td><strong>$category</strong>";
echo "<td></td>";
echo "</tr>";
}
$counterGrand=$counterGrand+$counterCat;
$onlineGrand=$onlineGrand+$onlineCat;
$voucherGrand=$voucherGrand+$voucherCat;
只是副手,它看起来就像是增加对'$ data',不'$ summary'。 –
@ Don'tPanic他在做两个。他在'$ categoryTotals [$ category] ['counterExtended'] + = $ counterExtended;' – Barmar
'中添加了'$ summary';我认为问题可能是您重新初始化$ categoryTotals [$ category]'$ lineItems [$ category] [$ description]'没有设置。所以,每当你得到一个新的描述,你重置整个类别的总数。 – Barmar