【问题标题】:calculate loop fields with MySql php用 MySql php 计算循环字段
【发布时间】: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> &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。

【问题讨论】:

  • 你必须转移 $totalBalance = 0;和 $totalMonthlyComm = 0;在循环开始之前。
  • 你能告诉我结果数组吗?

标签: php html mysql sql database


【解决方案1】:

你只需要循环一次数据库结果:

<?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
} ?>

【讨论】:

    【解决方案2】:

    将你的行从 while 更改为:

    <?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>
    

    【讨论】:

    • 您的解决方案不起作用,因为$stmt2-&gt;fetch() 将返回 FALSE。 (因为结果集的所有行都已被第一个while 循环所消耗)。
    猜你喜欢
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    相关资源
    最近更新 更多