无法添加数组值,有什么不对? [英] Cannot add array values, what's wrong?

查看:75
本文介绍了无法添加数组值,有什么不对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在委托有一个电子商务Web应用程序的功能之一,我的工作就可以了。

I am making an eCommerce web application having commission as one of the feature and I am working on it.

Orders表:

CREATE TABLE `orders` (
 `OrderId` int(11) NOT NULL AUTO_INCREMENT,
 `OrderCode` varchar(20) NOT NULL,
 `CustEmailAdd` varchar(80) NOT NULL,
 `CustDelAddId` varchar(255) NOT NULL,
 `ProdCode` varchar(255) NOT NULL,
 `Quantity` varchar(100) NOT NULL,
 `PaytMethod` varchar(255) NOT NULL,
 `ShippingCharges` float NOT NULL,
 `TaxedAmount` float NOT NULL,
 `AppliedCredits` int(11) NOT NULL,
 `PayableAmount` varchar(255) NOT NULL,
 `OrderDate` datetime NOT NULL,
 `OrderModified` datetime NOT NULL,
 `OrderStatus` varchar(255) NOT NULL,
 `OrderIPAddress` varchar(20) NOT NULL,
 PRIMARY KEY (`OrderId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

佣金表:

CREATE TABLE `commission` (
 `CommId` int(11) NOT NULL AUTO_INCREMENT,
 `OrderCode` varchar(20) NOT NULL,
 `ProdCode` varchar(255) NOT NULL,
 `ModCode` varchar(255) NOT NULL,
 `AffiCode` varchar(255) NOT NULL,
 `BenCode` varchar(255) NOT NULL,
 `ModCommAmount` varchar(255) NOT NULL,
 `AffiCommAmount` varchar(255) NOT NULL,
 `BenCommAmount` varchar(255) NOT NULL,
 PRIMARY KEY (`CommId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

该affiliate_commission_payable表

The affiliate_commission_payable table

CREATE TABLE `affiliate_commission_payable` (
 `ACP_Id` int(11) NOT NULL AUTO_INCREMENT,
 `CommId` int(11) NOT NULL,
 `ACP_PaymentStatus` varchar(255) NOT NULL,
 `ACP_PaymentDate` varchar(255) NOT NULL,
 `ACP_PaymentDetails` text NOT NULL,
 PRIMARY KEY (`ACP_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

我有一个数组,像这样:

I have an array like so:

// For Order 000001    
    Array
    (
        [0] => ORD-000001
    )
    Array
    (
        [Paid] => 26.25
        [Due] => 42.75
    )

// For Order 000002
    Array
    (
        [0] => ORD-000002
    )
    Array
    (
        [Cancelled] => 33.75
        [Due] => 13.5
    )
// For Order 000003
    Array
    (
        [0] => ORD-000003
    )
    Array
    (
        [Paid] => 13.50
        [Paid] => 14.25
    )

我所试图做的是显示的顺序佣金汇总给用户。它是在像这样的表格式为:

What I am trying to do is showing the order commission summary to the user. It is in the table format like so:

OrderCode    Paid Comm    Cancelled Comm    Due Comm    View Details

ORD-000001   addAllPaid   addAllCancelled   addAllDue       View

ORD-000002   addAllPaid   addAllCancelled   addAllDue       View

ORD-000003   addAllPaid   addAllCancelled   addAllDue       View

如果有阵列为佣金中没有值,用0代替它,否​​则它们添加以及基于所述顺序code显示它。我如何做到这一点?

If there is no value in the array for commission, replace it with 0, else add them and display it based on the order code. How do I achieve this ?

在code,我到目前为止已经试过: - >

The code that I have tried so far:->

<?php
$qu = "SELECT
           c.*, o.*, acp.*, .*, a.*
       FROM
           customers c, orders o, affiliate_commission_payable acp, commission com, affiliates a
       WHERE
           a.AffiCode = '".$affiCode."'
               AND
           c.AffiCode = '".$affiCode."'
               AND
           c.CustEmailAdd = o.CustEmailAdd
               AND
           acp.CommId = com.commId
               AND
           com.OrderCode = o.OrderCode
               AND
           com.AffiCode = c.AffiCode";
$validate->Query($qu);
if ($validate->NumRows() >= 1) {
    while ($rows = $validate->FetchAllDatas()) {
        $commId = $rows["CommId"];
        $orderCode = $rows["OrderCode"];
        $orderDate = $rows["OrderDate"];
        $arrCommissionStatus = explode(', ', $rows["ACP_PaymentStatus"]);
        $prdCodes = explode(', ', $rows["ProdCode"] );
        $arrAffiCommAmount = explode(', ', $rows["AffiCommAmount"]);

        $ord = explode(', ', $rows["OrderCode"]);

        $prdCodeAndCommStatus = array_combine($prdCodes, $arrCommissionStatus);
        $arrCommAndStatus = array_combine($arrCommissionStatus, $arrAffiCommAmount);

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

        foreach ($arrCommAndStatus as $key => $value) {
            if ($value == 'Due') {
                $totDueComm += $key;
            } else {
                $totDueComm = 0;
            }
        }
        foreach ($arrCommAndStatus as $key => $value) {
            if ($value == 'Cancelled') {
                $totCancellComm += $key;
            } else {
                $totCancellComm = 0;
            }
        }
        foreach ($arrCommAndStatus as $key => $value) {
            if ($value == 'Paid') {
                $totPaidComm += $key;
            } else {
                $totPaidComm = 0;
            }
        }

        $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(array_sum($arrAffiCommAmount), 2)."</td>";
        $table .= "<td><a href='//www.example.com/nbs/Affiliates/Commission.php?ord=".$orderCode."&id=".$commId."'>View</a></td>";
        $table .= "</tr>";

    }   
}

更新1

我莫名其妙地试图把它做..这里是code,我不得不上面while循环中改变:

I have somehow tried to get it done.. Here's the code that I had to change inside the while loop above:

$strQ = "SELECT com.*, acp.* FROM commission com, affiliate_commission_payable acp WHERE com.AffiCode = '".$affiCode."' AND com.OrderCode = '".$orderCode."' AND com.CommId = acp.CommId";
$validate->Query($strQ);
if ($validate->NumRows() >= 1) {
    while ($rows_strQ = $validate->FetchAllDatas()) {
        $comAmt = explode(', ', $rows_strQ["AffiCommAmount"]);
        $coStat = explode(', ', $rows_strQ["ACP_PaymentStatus"]);
        $ts = array_combine( $comAmt, $coStat);
        foreach ($ts as $key => $value) {
            if (in_array($value, $sta) && $value == 'Due') {
                $totDueComm += $key;
                $totalDueCommission += $totDueComm;
            }                       
        }
    }
}

但更新刚刚做它一阶,而不是所有的订单。我在哪里出了错?请帮助我。谢谢你。

But the update has just done it for 1st order and not all the orders. Where have I gone wrong ? Kindly help me out. Thanks.

更新2

我也总算是完成它。但有这一数额是越来越添加到previous的订单金额,然后显示量。例如,说我有一个卢比问题。 300订购1 和卢比。在订购2 ..因此,对于订购1 ,它会显示 RS 200。 300 订购2 ,显示的金额为卢比。 500 。我想让它显示卢比。 200 ,而不是只500卢比我要如何解决这个问题?请帮我..再次感谢。

I have somehow managed it to get it done.. But there is a problem that the amount is getting added to the previous order amount and then the amount is displayed.. For example, Say I have Rs. 300 in Order 1 and Rs. 200 in Order 2.. So for Order 1, it will display Rs. 300 and for Order 2, the amount shown is Rs. 500. I want it to show Rs. 200 only and not Rs 500. How do I solve this ? Please help me.. Thanks once again.

下面是我迄今使用code:

Here's the code that I have used so far:

推荐答案

根据聊天,这里的解决方案:的http:// ideone.com/tXoWDk

Based on chat, here the solution: http://ideone.com/tXoWDk

我不知道理解你的数据库结构中的所有领域,但如果像你说的,要总结佣金每笔订单在数据库中,这里的如何做到这一点的例子:

I'm not sure of understanding all fields in your database structure, but if, as you said, want to sum commissions for each order in your database, here a sample of how to do that:

select 
    o.code, sum(c.amount) as total
from orders o
inner join com c on c.orderId = o.id
group by o.code

问题的关键是,这样做,在PHP也没用,因为MySQL能够处理它也没有任何进一步的处理,所以如果我简化结构是这样的:

The point is that doing that in PHP is useless as MySQL can handle it as well without any further processing, so if I simplify the structure like this:

mysql> select * from orders;
+----+------------+
| id | code       |
+----+------------+
|  1 | ORDER-0001 |
|  2 | ORDER-0002 |
+----+------------+

mysql> select * from com;
+----+--------+---------+-----------+
| id | amount | orderId | status    |
+----+--------+---------+-----------+
|  1 |     15 |       1 | DUE       |
|  2 |     12 |       1 | DUE       |
|  3 |      2 |       2 | CANCELLED |
|  4 |     23 |       2 | PAID      |
|  5 |      3 |       1 | CANCELLED |
|  6 |      5 |       1 | PAID      |
|  7 |     12 |       2 | CANCELLED |
+----+--------+---------+-----------+


mysql> select
    -> o.code, c.status, sum(c.amount) as total
    -> from orders o
    -> inner join com c on c.orderId = o.id
    -> group by c.status, o.code
    -> order by o.code, c.status, c.amount;
+------------+-----------+-------+
| code       | status    | total |
+------------+-----------+-------+
| ORDER-0001 | CANCELLED |     3 |
| ORDER-0001 | DUE       |    27 |
| ORDER-0001 | PAID      |     5 |
| ORDER-0002 | CANCELLED |    14 |
| ORDER-0002 | PAID      |    23 |
+------------+-----------+-------+

和的总和,只适用于以下到你的结果是:

and to sum the total, just apply the following to your result:

$ordersTotalCommissions = [];

// fetch your data
foreach ($result as $line) {

    // initialize the 'order' group by putting 0 as a commission
    if (!isset($ordersTotalCommissions[$line['code']])) {
        $ordersTotalCommissions[$line['code']] = 0;
    }

    // sum each commission
    $ordersTotalCommissions[$line['code']] += $line['total'];
}

print_r($ordersTotalCommissions);

告诉我,如果可以帮助您

Tell me if that helps you

这篇关于无法添加数组值,有什么不对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆