Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I achieve a sum of each individual column ie. Due Principal, Interest, and Total Monthly Payment using a for loop. I want the result to be output in the (-----------) section below the table. Any help?

HTML
<pre><h1>Loan Repayment Schedule</h1>
<p>Amount Taken: 1,000,000</p>
<p>Period: 12 Months</p>
<p>Interest: 2%</p>
<table border="1">
  <thead>
    <tr>
      <th>Month</th>
      <th>Due Date</th>
      <th>Description</th>
      <th>Due Principal</th>
      <th>Interest</th>
      <th>Total Monthly Payment</th>
      <th>Principal Balance</th>
    </tr>
  </thead>
  <tbody>
    <?php
      $balance = 1000000;
      $period = 12;
      $emi = $balance/$period;
      $payment_date = date('2022-09-28');

      for ($i=1; $i <=$period; $i++){
        $interest = ((2/100)*$balance);
        $principal = $emi +$interest;
        $balance = $balance - $emi;
        $payment_date = date("jS F, Y",strtotime("+1 month",strtotime($payment_date)));
        ?>
          <tr>
            <td><?php echo $i;?></td>
            <td><?php echo $payment_date;?></td>
            <td>Repayment</td>
            <td><?php echo number_format($emi);?></td>
            <td><?php echo number_format($interest);?></td>
            <td><?php echo number_format($principal);?></td>
            <td><?php echo number_format($balance);?></td>
          </tr>
        <?php
      }
    ?>
  </tbody>
  <tfoot>
    <tr>
      <th></th>
      <th></th>
      <th></th>
      <th>-----------</th>
      <th>-----------</th>
      <th>-----------</th>
      <th></th>
    </tr>
  </tfoot>
</table>


What I have tried:

<pre lang="HTML"><pre><h1>Loan Repayment Schedule</h1>
<p>Amount Taken: 1,000,000</p>
<p>Period: 12 Months</p>
<p>Interest: 2%</p>
<table border="1">
  <thead>
    <tr>
      <th>Month</th>
      <th>Due Date</th>
      <th>Description</th>
      <th>Due Principal</th>
      <th>Interest</th>
      <th>Total Monthly Payment</th>
      <th>Principal Balance</th>
    </tr>
  </thead>
  <tbody>
    <?php
      $balance = 1000000;
      $period = 12;
      $emi = $balance/$period;
      $payment_date = date('2022-09-28');

      for ($i=1; $i <=$period; $i++){
        $interest = ((2/100)*$balance);
        $principal = $emi +$interest;
        $balance = $balance - $emi;
        $payment_date = date("jS F, Y",strtotime("+1 month",strtotime($payment_date)));
        ?>
          <tr>
            <td><?php echo $i;?></td>
            <td><?php echo $payment_date;?></td>
            <td>Repayment</td>
            <td><?php echo number_format($emi);?></td>
            <td><?php echo number_format($interest);?></td>
            <td><?php echo number_format($principal);?></td>
            <td><?php echo number_format($balance);?></td>
          </tr>
        <?php
      }
    ?>
  </tbody>
  <tfoot>
    <tr>
      <th></th>
      <th></th>
      <th></th>
      <th>-----------</th>
      <th>-----------</th>
      <th>-----------</th>
      <th></th>
    </tr>
  </tfoot>
</table>
Posted
Comments
Richard MacCutchan 1-Oct-22 6:46am    
You need to loop through the amounts in each column adding them together and post the total in the row beyond the last posted item.
Norman Mugumya 1-Oct-22 14:45pm    
How can that be achieved?

Kindly reply to this with a snippet.
Thank you.
Richard MacCutchan 1-Oct-22 15:18pm    
You just need to loop through each row taking the values in the relevant column and adding them together. What is difficult about that?
Norman Mugumya 3-Oct-22 5:46am    
I have to be honest. I have tried all and failed. If you could paste the source code here, I would be much happy.

Thanks for the help.
Richard MacCutchan 3-Oct-22 6:42am    
You already have a for loop that builds the data. So you just need another one that follows it to add up all the values in the columns.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900