创建一个合适的数组摘要

时间:2016-01-29 19:54:37

标签: php arrays

我正在编写一个脚本来创建项目摘要。唯一似乎没有正常工作的是项目汇总金额。也就是说这三行显示价格的最后一个值而不是总和:

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;

2 个答案:

答案 0 :(得分:0)

每次在现有$description中遇到新的$category时,都会从当前行重新初始化$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;
}

答案 1 :(得分:0)

谢谢Barmar。我不得不做一些修改,这是我的最终代码:

   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 (!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;
    $data['description']     = $description;
    $data['year']            = $year;
    $data['id']              = $id;



    $categoryTotals[$category]['counterExtended'] += $counterExtended;
    $categoryTotals[$category]['onlineExtended']  += $onlineExtended;
    $categoryTotals[$category]['voucherExtended'] += $voucherExtended;


        // First time hitting an item with this description


    }

    // 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+=$summary['counterExtended'];
        $onlineGrand+=$summary['onlineExtended'];
        $voucherGrand+=$summary['voucherExtended'];

    }