我正在制作一个电子商务网络应用程序,其中包含佣金作为功能之一,我正在努力。
订单表:
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表
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
我有一个像这样的数组:
// 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
)
我要做的是向用户显示订单佣金摘要。它的表格格式如下:
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,否则添加它们并根据订单代码显示它。我如何实现这一目标?
到目前为止我尝试过的代码: - >
<?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 :
我以某种方式试图完成它。这里是我必须在上面的while循环中更改的代码:
$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;
}
}
}
}
但是更新刚刚完成了第一个订单而不是所有订单。我哪里出错了?请帮助我。感谢。
更新2 :
我以某种方式管理它以完成它..但是有一个问题是金额被添加到先前的订单金额然后显示金额..例如,说我有Rs。 300 in Order 1
和卢比。 200 in Order 2
..因此,对于Order 1
,它会显示Rs. 300
,而对于Order 2
,显示的金额为Rs. 500
。我希望它只显示Rs. 200
而不是500卢比。我该如何解决这个问题?请帮帮我..再次感谢。
这是我到目前为止使用的代码:
$sta = array('Due', 'Cancelled', 'Paid');
$qu = "SELECT c.*, o.*, acp.*, com.*, 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 GROUP BY o.OrderCode";
$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"]);
$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 === "AffiCommAmount") {
$comAmt = explode(', ', $rows["AffiCommAmount"]);
}
if ($key === "ACP_PaymentStatus") {
$coStat = explode(', ', $rows["ACP_PaymentStatus"]);
}
}
$ts = array_combine( $comAmt, $coStat);
foreach ($ts as $k => $v) {
if (in_array($v, $sta) && $v == 'Due') {
$totDueComm += $k;
$totalDueCommission = $totDueComm;
}
}
$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>";
}
}
?&GT;
答案 0 :(得分:0)
根据聊天情况,这里有解决方案:http://ideone.com/tXoWDk
我不确定是否了解数据库结构中的所有字段,但如果如您所说,想要汇总数据库中每个订单的佣金,请参阅以下示例:
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也可以处理它而无需进一步处理,所以如果我简化这样的结构:
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 |
+------------+-----------+-------+
并总计总和,只需将以下内容应用于您的结果:
$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);
告诉我这是否有助于你