这段代码有什么不同的工作方式

时间:2016-10-10 16:36:10

标签: php mysql

以下是我撰写的个人预算计划的一部分。

此代码从多个表中提取行项信息并将其写入数组,然后通过transid =>显示信息。 family => category =>了LineItem。一切正常,我得到了我想要的结果。我的问题是,是否有更有效的方法来完成这项任务?

由于这是个人计划,我只是要求我提高编码能力。

<?php

    include ('../cfg/connect.php');

    $s = " : ";
    $br = "<br>";
    $ul = "<ul>";
    $li = "<li>";
    $_ul = "</ul>";
    $_li = "</li>";

    $data = [];
    $itemCount = 0;
    $arrayItemCount = 0;


    $categoryQry = "SELECT a.itemQty, b.transDate, b.transID, b.amount, a.itemPrice, a.itemCategory, c.catFamily, a.itemName, a.itemSource FROM budget.lineItems AS a JOIN budget.quickEntry AS b ON a.transID = b.transID JOIN budget.categories AS c ON a.itemCategory = c.catName WHERE b.processed = 'y' ORDER BY c.catFamily, c.catName, b.transDate";
    $categories = $conn->prepare ($categoryQry);
    $categories->execute ();
    $categories->store_result ();
    $categories->bind_result ($itemQty, $transDate, $transID, $totalPrice, $itemPrice, $category, $family, $itemName, $source);

    while ($categories->fetch ()) {

        if (!isset($data[$transID]['amount'])) {
            $data[$transID]['amount'] = 0;
        }
        if (!isset($data[$transID]['line'])) {
            $data[$transID]['line'] = '';
        }
        if (!isset($data[$transID]['line'][$family]['amount'])) {
            $data[$transID]['line'][$family]['amount'] = 0;
        }
        if (!isset($data[$transID]['line'][$family]['line'])) {
            $data[$transID]['line'][$family]['line'] = '';
        }
        if (!isset($data[$transID]['line'][$family]['line'][$category]['amount'])) {
            $data[$transID]['line'][$family]['line'][$category]['amount'] = 0;
        }
        if (!isset($data[$transID]['line'][$family]['line'][$category]['line'])) {
            $data[$transID]['line'][$family]['line'][$category]['line'] = '';
        }

        $itemCount++;

        $qtyPrice = $itemPrice * $itemQty;
        $data[$transID]['amount'] += $qtyPrice;
        $data[$transID]['transDate'] = $transDate;
        $data[$transID]['source'] = $source;
        $data[$transID]['line'][$family]['amount'] += $qtyPrice;
        $data[$transID]['line'][$family]['line'][$category]['amount'] += $qtyPrice;
        $data[$transID]['line'][$family]['line'][$category]['line'][$itemName] = ['itemQty' => $itemQty, 'itemPrice' => $itemPrice];
    }


    foreach ($data as $transID => $transValue) {
        echo $transID .$s.$transValue['transDate'].$s.$transValue['source'].$s.$transValue['amount']. $ul;
        foreach ($transValue['line'] as $category => $categoryValue) {
            echo $li . $category .$s.$categoryValue['amount']. $ul;
            foreach ($categoryValue['line'] as $line => $lineValue) {
                echo $li . $line .$s.$lineValue['amount']. $ul;
                foreach ($lineValue['line'] as $item => $details) {
                    echo $li . $item .$s . $details['itemQty'] . $s . $details['itemPrice'] . $_li;
                }
                echo $_ul . $_li;
            }
            echo $_ul . $_li;
        }
        echo $_ul . $br;
    }

0 个答案:

没有答案