3 回答
TA贡献1826条经验 获得超6个赞
你有一些奇怪的疑问。单引号绝对不能用于列别名,为什么要按 system_id 分组?通过使用联接,您的计算将大大简化,如果我是您的老师,我希望看到联接。
这将立即解决分配问题:
SELECT s.system_id
, sum(amount_paid) as sumitem_cost
, sum(fee) as total
FROM students s
JOIN payments p on s.system_id = p.payment_by
JOIN invoices i on p.invoice_id = i.id
TA贡献1757条经验 获得超8个赞
$total_price每次与新学生打交道时,您都需要归零(init) 。您没有明确地这样做,因此第一次使用$total_price它时会被创建为零,但随后您会进一步迭代并不断添加它,最终得到累积值。所以只需添加
$total_price = 0;
对于每个while循环迭代:
while($row = mysqli_fetch_assoc($students)){
$total_price = 0;
...
TA贡献1993条经验 获得超5个赞
你可以试试这个代码
<?php
$students = mysqli_query($conn, "SELECT * FROM students GROUP BY system_id");
$grandBalanceForAllStudent = 0 ;
while($row = mysqli_fetch_assoc($students)){
$user_uid = $row['system_id'];
$exam = $row['exam_number'];
$total_price = 0 ;
//getting payments balances
//gettingshopping cart details
$Balance_query = mysqli_query($conn, "SELECT SUM(amount_paid) AS 'sumitem_cost' FROM payments WHERE payment_by='$user_uid' ");
$balance_data = mysqli_fetch_array($Balance_query);
$balance_price = $balance_data['sumitem_cost'];
$py = mysqli_query($conn, "SELECT * FROM payments WHERE payment_by='$user_uid' AND status!='rejected' GROUP BY invoice_id");
while($rowpy = mysqli_fetch_assoc($py)){
$paidAmout = $rowpy['amount'];
$invoiceId = mysqli_real_escape_string($conn, $rowpy['invoice_id']);
$PaymentStatus = mysqli_real_escape_string($conn, $rowpy['status']);
//Getting invoice
$Invoice = mysqli_query($conn, "SELECT * FROM invoices WHERE id='$invoiceId'");
while($rowInv = mysqli_fetch_assoc($Invoice)){
$NewFeeId = $rowInv['id'];
$sql = mysqli_query($conn,"SELECT SUM(fee) as total FROM invoices WHERE id='$invoiceId'");
$row = mysqli_fetch_array($sql);
$sum = $row['total'];
$total_price += $row[‘fee’];
}}
$BalanceToPay = $total_price - $balance_price;
// you can keep for your data
$grandBalanceForAllStudent += $BalanceToPay ;
echo'<br>'.$exam.':' . $BalanceToPay ;
}
?>
- 3 回答
- 0 关注
- 107 浏览
添加回答
举报