2017-07-11 4 views
0

MySqlからデータを取得するHTMLテーブルがあります。MySqlでループフィールドを計算する

existingbankproductsテーブルは以下の通りです:

enter image description here

私は私が手の数字を合計する必要がある2つの計算フィールドを持っています。計算に必要なフィールドは、残高と月次コミットメントです。

私は、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を合計することはできません。

+0

あなたはtotalBalance = 0 $をシフトしなければなりません。 $ totalMonthlyComm = 0;ループ開始前。 –

+0

結果の配列を教えていただけますか? – GYaN

答えて

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
が通りながらからあなたの行を変更

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

あなたのソリューションはうまくいかず、 '$ stmt2-> fetch()'はFALSEを返します。 (結果セットのすべての行が最初の 'while'ループで消費されたためです)。 –