我正在编写脚本以创建项目摘要。 唯一看起来没有正常工作的是项目汇总金额。 也就是说,这三行显示价格的最后一个值,而不是总和:
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;我会注意到QTY加起来很好,只是不是$ 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;I am working on a script to create a summary of items. The only thing that doesn't appear to be working correctly is the item summary amounts. That is to say that these three lines display the last value for the price instead of a sum:
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>";Even though I have them being added here:
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;I'll note that QTY adds up just fine, just not $counterExtended, $onlineExtended, $voucherExtended. They only display the last value instead of adding up all the values for each item, per month.
Here is the SQL loop:
$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;最满意答案
每当您在现有的$category遇到新的$description时,都会从当前行重新初始化$categoryTotals[$category] ,从而消除该类别的运行总数。 您应该只在首次看到该类别时初始化此条目。
while ($s->fetch()) { if (!isset($categoryTotals[$category])) { $categoryTotals[$category] = array('counterExtended' => 0, 'onlineExtended' => 0, 'voucherExtended' => 0); $lineItems[$category] = array(); } if (!isset($lineItems[$category][$description])) { $lineItems[$category][$description] = array('quantity' => 0, 'counterExtended' => 0, 'onlineExtended' => 0, 'voucherExtended' => 0); } $data =& $lineItems[$category][$description]; // Use reference so we don't make a copy that we have to assign back $data['quantity'] += $qty; $data['counterExtended'] += $counterExtended; $data['onlineExtended'] += $onlineExtended; $data['voucherExtended'] += $voucherExtended; $categoryTotals[$category]['counterExtended'] += $counterExtended; $categoryTotals[$category]['onlineExtended'] += $onlineExtended; $categoryTotals[$category]['voucherExtended'] += $voucherExtended; }Every time you encounter a new $description in an existing $category, you reinitialize $categoryTotals[$category] from the current row, which wipes out the running total for that category. You should only initialize this entry when you see the category for the first time.
while ($s->fetch()) { if (!isset($categoryTotals[$category])) { $categoryTotals[$category] = array('counterExtended' => 0, 'onlineExtended' => 0, 'voucherExtended' => 0); $lineItems[$category] = array(); } if (!isset($lineItems[$category][$description])) { $lineItems[$category][$description] = array('quantity' => 0, 'counterExtended' => 0, 'onlineExtended' => 0, 'voucherExtended' => 0); } $data =& $lineItems[$category][$description]; // Use reference so we don't make a copy that we have to assign back $data['quantity'] += $qty; $data['counterExtended'] += $counterExtended; $data['onlineExtended'] += $onlineExtended; $data['voucherExtended'] += $voucherExtended; $categoryTotals[$category]['counterExtended'] += $counterExtended; $categoryTotals[$category]['onlineExtended'] += $onlineExtended; $categoryTotals[$category]['voucherExtended'] += $voucherExtended; }更多推荐
发布评论