【发布时间】:2017-07-11 07:18:05
【问题描述】:
我有一个从 MySql 获取数据的 HTML 表。
现有银行产品表如下:
我有 2 个计算字段需要总结我得到的数字。需要计算的字段是 Balance 和 MonthlyCommitment。
我用来从 MySql 获取结果的代码如下:
<?php
$stmt2 = $DB_con->prepare("SELECT * FROM applicantpersonaldetails apd "
. "INNER JOIN existingbankproducts ext ON apd.ApplicantID = ext.ApplicantID "
. "WHERE apd.AccountID =:accountId AND apd.applicantType ='main';");
$stmt2->bindParam(':accountId', $accountId, PDO::PARAM_INT);
//if account id data type is varchar change the last parameter to `PDO::PARAM_str`
$stmt2->execute();
if ($stmt2->rowCount() > 0) {
?>
<table>
<tr>
<th>Financial Institution</th>
<th>Product Type</th>
<th>Balance</th>
<th>Monthly Commitment</th>
</tr>
<?php
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
?>
<tr>
<td><input type = "text" name = "finanIns1" id = "finanIns1" value="<?php echo $row['FinanicalInstituion']; ?>" readonly></td>
<td>
<input list = "proTypeList" name = "proType1" id = "proType1" value="<?php echo $row['ProductType']; ?>"readonly >
</td>
<td id = "balance"><input type = "number" name = "balance1" id = "balance1" value="<?php echo $row['Balance']; ?>"readonly></td>
<td id = "MonthyComm"><input type = "number" name = "monthlyComm1" id = "monthlyComm1" value="<?php echo $row['MonthlyCommitment']; ?>"readonly></td>
</tr>
<?php
}
} else {
?>
<div class="">
<div class="alert alert-warning">
<span class="glyphicon glyphicon-info-sign"></span> No Data Found ...
</div>
</div>
<?php
}
?>
<?php
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
$totalBalance = 0;
$totalBalance += $row['Balance'];
$totalMonthlyComm = 0;
$totalMonthlyComm +=$row['MonthlyCommitment'];
?>
<tr>
<td>Total:</td>
<td><input readonly></td>
<td id="balance"><input type="number" name="totalBalance" id="totalBalance" value="<?php echo $totalBalance; ?>" min="0" readonly></td>
<td id="MonthyComm"><input type="number" name="totalMonthlyComm" id="totalMonthlyComm" value="<?php echo $totalMonthlyComm; ?>" min="0" readonly></td>
</tr>
<?php
}
?>
</table>
我能够从 MySql 中检索数据。但是,它无法总结我表格末尾的 totalBalance 和 totalMonthlyComm。
【问题讨论】:
-
你必须转移 $totalBalance = 0;和 $totalMonthlyComm = 0;在循环开始之前。
-
你能告诉我结果数组吗?
标签: php html mysql sql database