计算出的数组值不正确,需要帮助

时间:2015-03-09 09:43:30

标签: php arrays multidimensional-array

在我的订单表中,我有一个这样的数组:

// For Order 1:
Array
(
    [MOD-000001] => Array
        (
            [0] => 37.5
            [1] => 28.5
            [2] => 24
            [3] => 19.5
        )

)

Array
(
    [37.5] => Cancelled
    [28.5] => Due
    [24] => Due
    [19.5] => Due
)

// For Order 2
Array
(
    [MOD-000002] => 67.5
    [MOD-000001] => Array
        (
            [0] => 52.5
            [1] => 27
            [2] => 24
        )

)

Array
(
    [67.5] => Due
    [52.5] => Due
    [27] => Due
    [24] => Due
)

我要做的是,每当MOD-000001MOD-000002从他的浏览器登录时,他都会在MyAccount页面上看到他的佣金金额:

MOD-000001

OrderNo.    DueComm    CancelComm    TotalComm
   1         72.00        0.00         72.00
   2        103.50        0.00        103.50

同样适用于MOD-000002

我得到的是所有版主的所有额外金额。例如,在订单2中,有2个版主,因此如果MOD-000002登录,他会看到该订单上所有佣金的总和,如下所示:

OrderNo.    DueComm    CancelComm    TotalComm
   2         171.00       0.00        171.00

我如何完成它?我从5天开始摸不着头脑,但没有成功。

这是我到目前为止尝试过的php代码:

<?php
session_start();

if (!isset($_SESSION['Moderator']['email'])) {
    header("Location: //www.example.com/nbs/Moderators/");
    exit();
}

require_once '../Classes/class.Validation.php';
$validate = new Validation();

$modCode = "";

$q = "SELECT * FROM moderators WHERE ModEmail = '".$_SESSION['Moderator']['email']."'";
$validate->Query($q);

if ($validate->NumRows() > 0) {
    while ($row = $validate->FetchAllDatas()) {
        $modCode = $row['ModCode'];
        $modName = $row['ModFullName'];
    }
}
$totalPaidCommission = $totalDueCommission = $totalCancelledCommission = 0;


$commiss = "";
$table = "";

$sta = array('Due', 'Cancelled', 'Paid');

$qu = "SELECT o.*, mcp.*, com.* FROM orders o, moderators_commission_payable mcp, commission com WHERE mcp.CommId = com.commId AND com.OrderCode = o.OrderCode";
$validate->Query($qu);
if ($validate->NumRows() >= 1) {
    while ($rows = $validate->FetchAllDatas()) {
        $totPaidComm = $totDueComm = $totCancellComm = 0;

        $commId = $rows["CommId"];
        $orderCode = $rows["OrderCode"];
        $orderDate = $rows["OrderDate"];

        $prdCodes = explode(', ', $rows["ProdCode"] );

        $c = explode(', ', $rows["ModCode"]);

        if (in_array($modCode, $c)) {
            $sqr = "SELECT * FROM moderators WHERE ModCode = '".$modCode."'";
            $validate->Query($sqr);

            echo $validate->FetchAssoc('ModFullName');

            $arrCommissionStatus = explode(', ', $rows["MCP_PaymentStatus"]);
            $arrAffiCommAmount = explode(', ', $rows["ModCommAmount"]);
            $prdCodeAndCommStatus = array_combine($prdCodes, $arrCommissionStatus);
            $arrCommAndStatus = array_combine($arrAffiCommAmount, $arrCommissionStatus);

            $table .= "<tr>";
            $table .= "<td>".$orderCode."</td>";
            $table .= "<td>".$orderDate."</td>";
            $table .= "<td>".count($prdCodes)."</td>";

            foreach ($rows as $key => $value) {
                if ($key === "ModCommAmount") {
                    $comAmt = explode(', ', $rows["ModCommAmount"]);
                }
                if ($key === "MCP_PaymentStatus") {
                    $coStat = explode(', ', $rows["MCP_PaymentStatus"]);
                }
            }

            $ts = array_combine( $comAmt, $coStat);

            foreach ($ts as $k => $v) {
                if (in_array($v, $sta) && $v == 'Due') {
                    $totDueComm += $k;
                }

                if (in_array($v, $sta) && $v == 'Paid') {
                    $totPaidComm += $k;
                }

                if (in_array($v, $sta) && $v == 'Cancelled') {
                    $totCancellComm += $k;
                }
            }

            $table .= "<td>".number_format($totPaidComm, 2)."</td>";
            $table .= "<td>".number_format($totCancellComm, 2)."</td>";
            $table .= "<td>".number_format($totDueComm, 2)."</td>";
            $table .= "<td>".number_format( ($totPaidComm + $totCancellComm + $totDueComm), 2)."</td>";
            $table .= "<td><a href='//www.example.com/nbs/Moderators/Commission.php?ord=".$orderCode."&id=".$commId."'>View</a></td>";
            $table .= "</tr>";
        }

    }
}

?>

请帮助我..非常感谢任何帮助。

P.S。:所有值都来自数据库。因此,在下达的订单中,可以有任意数量的主持人和主持人依赖于产品。所以,你可以说x number of products in order = y number of moderators in order

更新1 : 这是$validate->FetchAllDatas()

的结果
Array
(
    [OrderId] => 1
    [OrderCode] => ORD-000001
    [CustEmailAdd] => mehulbawadia@gmail.com
    [CustDelAddId] => 1
    [ProdCode] => DC-0001-0004, DC-0001-0009, DC-0001-0015, DC-0001-001
    [Quantity] => 1, 1, 1, 1
    [PaytMethod] => 2
    [ShippingCharges] => 1
    [TaxedAmount] => 131.4
    [AppliedCredits] => 0
    [PayableAmount] => 1227.4
    [OrderDate] => 2015-03-05 12:14:42
    [OrderModified] => 0000-00-00 00:00:00
    [OrderStatus] => In Process, In Process, In Process, In Process
    [OrderIPAddress] => 60.243.64.45
    [MCP_Id] => 1
    [CommId] => 1
    [MCP_PaymentStatus] => Cancelled, Due, Due, Due
    [MCP_PaymentDate] => 
    [MCP_PaymentDetails] => 
    [ModCode] => MOD-000001, MOD-000001, MOD-000001, MOD-000001
    [AffiCode] => AFFI-000001
    [BenCode] => BEN-000001, BEN-000001, BEN-000001, BEN-000001
    [ModCommAmount] => 37.5, 28.5, 24, 19.5
    [AffiCommAmount] => 18.75, 14.25, 12, 9.75
    [BenCommAmount] => 187.5, 142.5, 120, 97.5
)

Array
(
    [OrderId] => 2
    [OrderCode] => ORD-000002
    [CustEmailAdd] => mehulbawadia@gmail.com
    [CustDelAddId] => 2
    [ProdCode] => DC-0001-0001, DC-0001-0002, DC-0001-0010, DC-0001-001
    [Quantity] => 1, 1, 1, 1
    [PaytMethod] => 2
    [ShippingCharges] => 1
    [TaxedAmount] => 205.2
    [AppliedCredits] => 0
    [PayableAmount] => 1916.2
    [OrderDate] => 2015-03-05 12:16:09
    [OrderModified] => 0000-00-00 00:00:00
    [OrderStatus] => In Process, In Process, In Process, In Process
    [OrderIPAddress] => 60.243.64.45
    [MCP_Id] => 2
    [CommId] => 2
    [MCP_PaymentStatus] => Due, Due, Due, Due
    [MCP_PaymentDate] => 
    [MCP_PaymentDetails] => 
    [ModCode] => MOD-000002, MOD-000001, MOD-000001, MOD-000001
    [AffiCode] => AFFI-000001
    [BenCode] => BEN-000002, BEN-000001, BEN-000001, BEN-000001
    [ModCommAmount] => 67.5, 52.5, 27, 24
    [AffiCommAmount] => 33.75, 26.25, 13.5, 12
    [BenCommAmount] => 337.5, 262.5, 135, 120
)

1 个答案:

答案 0 :(得分:1)

好的,找到了你的问题。

我不明白为什么这有必要?

foreach ($rows as $key => $value) {
    if ($key === "ModCommAmount") {
        $comAmt = explode(', ', $rows["ModCommAmount"]);
    }
    if ($key === "MCP_PaymentStatus") {
        $coStat = explode(', ', $rows["MCP_PaymentStatus"]);
    }
}

目前,您正在添加ModCommAmount的所有号码。因此,您需要检查此特定金额是否适用于当前版主。

完整代码:

<?php
session_start();

if (!isset($_SESSION['Moderator']['email'])) {
    header("Location: //www.example.com/nbs/Moderators/");
    exit();
}

require_once '../Classes/class.Validation.php';
$validate = new Validation();

$modCode = "";

$q = "SELECT * FROM moderators WHERE ModEmail = '".$_SESSION['Moderator']['email']."'";
$validate->Query($q);

if ($validate->NumRows() > 0) {
    while ($row = $validate->FetchAllDatas()) {
        $modCode = $row['ModCode'];
        $modName = $row['ModFullName'];
    }
}
$totalPaidCommission = $totalDueCommission = $totalCancelledCommission = 0;


$commiss = "";
$table = "";

$sta = array('Due', 'Cancelled', 'Paid');

$qu = "SELECT o.*, mcp.*, com.* FROM orders o, moderators_commission_payable mcp, commission com WHERE mcp.CommId = com.commId AND com.OrderCode = o.OrderCode";
$validate->Query($qu);
if ($validate->NumRows() >= 1) {
    while ($rows = $validate->FetchAllDatas()) {
        $totPaidComm = $totDueComm = $totCancellComm = 0;

        $commId = $rows["CommId"];
        $orderCode = $rows["OrderCode"];
        $orderDate = $rows["OrderDate"];

        $prdCodes = explode(', ', $rows["ProdCode"] );

        $c = explode(', ', $rows["ModCode"]);

        if (in_array($modCode, $c)) {
            $sqr = "SELECT * FROM moderators WHERE ModCode = '".$modCode."'";
            $validate->Query($sqr);

            echo $validate->FetchAssoc('ModFullName');

            $arrCommissionStatus = explode(', ', $rows["MCP_PaymentStatus"]);
            $arrAffiCommAmount = explode(', ', $rows["ModCommAmount"]);
            $prdCodeAndCommStatus = array_combine($prdCodes, $arrCommissionStatus);
            $arrCommAndStatus = array_combine($arrAffiCommAmount, $arrCommissionStatus);

            $table .= "<tr>";
            $table .= "<td>".$orderCode."</td>";
            $table .= "<td>".$orderDate."</td>";
            $table .= "<td>".count($prdCodes)."</td>";

            /* EDITED CODE */
            /*
            foreach ($rows as $key => $value) {
                if ($key === "ModCommAmount") {
                    $comAmt = explode(', ', $rows["ModCommAmount"]);
                }
                if ($key === "MCP_PaymentStatus") {
                    $coStat = explode(', ', $rows["MCP_PaymentStatus"]);
                }
            }
            */

            $comAmt = explode(', ', $rows["ModCommAmount"]);
            $coStat = explode(', ', $rows["MCP_PaymentStatus"]);

            $ts = array_combine( $comAmt, $coStat);

            $i = 0;
            foreach ($ts as $k => $v) {

                // Check if this is current moderator sum
                if( $c[$i] == $modCode ){
                    if (in_array($v, $sta) && $v == 'Due') {
                        $totDueComm += $k;
                    }

                    if (in_array($v, $sta) && $v == 'Paid') {
                        $totPaidComm += $k;
                    }

                    if (in_array($v, $sta) && $v == 'Cancelled') {
                        $totCancellComm += $k;
                    }
                }
                $i++;
            }
            /* EDITED CODE END */

            $table .= "<td>".number_format($totPaidComm, 2)."</td>";
            $table .= "<td>".number_format($totCancellComm, 2)."</td>";
            $table .= "<td>".number_format($totDueComm, 2)."</td>";
            $table .= "<td>".number_format( ($totPaidComm + $totCancellComm + $totDueComm), 2)."</td>";
            $table .= "<td><a href='//www.example.com/nbs/Moderators/Commission.php?ord=".$orderCode."&id=".$commId."'>View</a></td>";
            $table .= "</tr>";
        }

    }
}

?>