使用MySql php计算循环字段

时间:2017-07-11 07:18:05

标签: php html mysql sql database

我有一个从MySql获取数据的HTML表。

existingbankproducts表如下:

enter image description here

我有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> &nbsp; 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。

2 个答案:

答案 0 :(得分:2)

您只需要遍历数据库结果一次:

<?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();
$totalBalance = 0;
$totalMonthlyComm = 0;
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)) {
    $totalBalance += $row['Balance'];
    $totalMonthlyComm +=$row['MonthlyCommitment'];

?>
    <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
}
?><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>
</table>
<?php
} else {
?>
    <div class="">
        <div class="alert alert-warning">
            <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ...
        </div>
    </div>
<?php
} ?>

答案 1 :(得分:1)

从以下位置更改行:

<?php
  $totalBalance = 0;
  $totalMonthlyComm = 0;    
  while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
   $totalBalance += $row['Balance'];
   $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>