Jump to content

PHP using for loop to create table with three separate columns of looped info


Recommended Posts

I can only use one page and have to use php and html in order to make a table using a for loop to increment three different columns. The first column is 'Rate' the second is 'Annual Interest' and the third is 'Monthly Interest'

Yea, it's a class assignment and unfortunately the class only meets once per week and the professor doesn't really specialize in this subject matter, but a recent surge in enrollment has stretched the faculty kinda thin and she's virtually unavailable. We've only had two classes thus far, and I'm still pretty new to PHP.

PURPOSE: Practice in writing calculations and loops and in embedding HTML inside a PHP tag

PROCEDURE: In this lab you will write a php program that will calculate the annual and monthly interest on a loan. There will be no HTML page and no form. The program will use a loop to calculate the annual and monthly interest rate on 50000 dollars for every interest rate from 1 to 10 ( 1 being 1% APR (annual percentage rate). Format the results to display dollar signs and two decimal places. Display all the data in an HTML table (see provided jpeg). The code must have variables for the interest rate, annual interest and monthly interest and must define a constant named AMOUNT that will have a value of 50000.

Annual interest on a loan is calculated by multiplying the loan amount by the annual interest rate (expressed as a decimal – if the annual Interest rate is 1 percent the formula will multiply 50000 by .01.
Monthly interest can be calculated by dividing the annual interest by 12. Write a program that will calculate the annual and monthly interest for $50,000 at annual rates from 1 to 10 percent in increments of 1 percent and output the results as a table. You may use any type of loop you choose.

HINTS: The first few lines in the php tag will declare your variables. To create a table, you use echo statements and place the HTML inside of single quotes. For example, this code, inside a php tag, will create and display a paragraph element:

echo '

Hello World!

';

 

You will need to indicate the start of a table outside your loop. You will create the first row of the table outside the loop. The rest of the rows will be created inside of a loop that uses a counter going from 1 to 10. Inside each iteration the loop you will calculate the annual and monthly interest on 50000 using the counter, storing the answers in your variables and displaying the info in a row of the table. The ending tag for the table will be after the loop.

Provide a CSS file that will at a minimum will format the table.

 

<!--Indicates page is HTML5 compliant-->
<!DOCTYPE html>
<html>
<head>
<!--Titles the page at the top such as on the browser tab (w/ Chrome)-->
<title>Monthly & Yearly Interest</title>
<!--Pulls the CSS styling from the main.css page-->
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<main>
<p>Yearly and Monthly Interest for $50,000 at Varying Rates</p>
<table border="1">
<tr><td>Rate</td><td>Annual Interest</td><td>Monthly Interest</td></tr>
<?php

$Ammount = 50000;
$Percent = 1;
$Annual= 500;
$Monthly = $Ammount * $Percent/12;

for ($counter = 1; $counter <= 10; $counter++) {
echo "<td>$Percent%</td><td>$Annual</td><td>$Monthly</td>";
$Percent++;
$Annual+=500;
//$Monthly = ;
echo "<tr></tr>"; 
}

?>

</table>
</main>
</body>
</html>

I've tried using $Annual = number_format(500,2); and also modified the increment counter below the echo to do likewise, but I can only get the number formatting to appear on the first row. Also having a similar issue with the $Percent value. Technically I need the percent value to be 0.01 for the $Monthly calculations turn out correct, but I need the $Percent value to appear as a whole digit in the first column. I'm trying my best to explain this clearly. I have to use a for loop and have to follow the above instructions in order to create a page that will appear like the attached image.

To clarify, I'm not asking anyone to do my homework. I've managed most of this thus far, but am totally stuck at this point and need help.

post-179671-0-88616600-1441393321_thumb.jpg

Edited by kr4ckl3s

I've tried using $Annual = number_format(500,2); and also modified the increment counter below the echo to do likewise, 

 

Have you looked through the documentation for number_format()?

http://php.net/manual/en/function.number-format.php

 

The first example shows how to use the function with variables. You'll want to use the function in the echo statement.

 

Also note that the column tags should be surrounded by the row tags. Your script currently outputs the columns and then the row.

<!--Indicates page is HTML5 compliant-->
<!DOCTYPE html>
<html>
<head>
<!--Titles the page at the top such as on the browser tab (w/ Chrome)-->
    <title>Monthly & Yearly Interest</title>
<!--Pulls the CSS styling from the main.css page-->
    <link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
    <main>
<p>Yearly and Monthly Interest for $50,000 at Varying Rates</p>
<table border="1">
<tr><td>Rate</td><td>Annual Interest</td><td>Monthly Interest</td></tr>
    <?php


$Ammount = 50000;
$Percent = 1;
$Annual= number_format(500,2);
$Monthly = number_format($Ammount * $Percent/12,2);


 for ($counter = 1; $counter <= 10; $counter++) {
echo "<td>$Percent%</td><td>$$Annual</td><td>$$Monthly</td>"; 
$Percent++;
$Annual+=500;
//$Monthly = ;
 echo "<tr></tr>"; 
 }
  
    ?>


</table>
    </main>
</body>
</html>

The code for Monthly doesn't work right. The math only produces the same initial result the whole way down. The Annual number_format only works on the first cell in the column, the rest under it are unformatted. I've also tried foing a number_format on the increment code under the first echo with no such luck. These work, but the results are not showing up the way I need. Monthly is a logic error I'm struggling with because I need $Percent to show as an integer in the Percent column, but I need it to act as 0.01 for the math to work out correctly on the $Monthly part.

 

I tried using this:

$Annual+= number_format(500,2);

 for the increment part under the echo, and it didn't change anything. I tried it as 

 

$Annual number_format(=+500,2);

And it crashed. Didn't really expect that one to work, but figured I'd try anyway. :P

 

If I put it in the echo statement it just echo's out the actual string number_format....

Edited by kr4ckl3s

Oh, to concatenate it in and put in more double quotes. I didn't think it needed another set of quotes because I thought double quotes interpolate, which is why the $Percent and others could sit inside the whole thing unaffected. Awesome. The only other thing I'm trying to puzzle out is the monthly column. Any suggestions? 

The problem is that I need $Percent to appear as an integer like 1% 2% etc. all the way to 10% in the Percent column. Which it does. But in the $Monthly column the math I need to use is $Ammount * $Percent/12

In that case, I need the $Percent to be 0.01 because multiplying it by the integer 1 gives incorrect results. The only thing I can think of, is to create another variable for it and increase it at the bottom by 0.01 like I've done the others, but I don't know if there's a cleaner/simpler way to do it using just the one $Percent variable.

Edited by kr4ckl3s

try

<table border="1">
<tr><th>Rate</th><th>Annual Interest</th><th>Monthly Interest</th></tr>
    <?php
    $amount = 50000;
    for ($i=1; $i<=10; $i++) {
        $rate = $i/100;
        $annual = '$' . number_format($amount * $rate, 2);
        $month = '$' . number_format($amount * $rate / 12, 2);
        echo "<tr><td>{$i}%</td><td>$annual</td><td>$month</td></tr>";
    }
    ?>
</table>

Here's what I came up with to fix the percentage math issue, but now the issue is that for each iteration, the nex value of $Monthly is stored into the variable and increases it by that much each time rather than increasing by the initial amount only. The $Percentage column should display 1%, 2%, 3%, 4% etc. The $Annual Column should display $500.00, $1000.00, $1500.00, $2000.00 etc. and those two work beautifully. The final column, $Monthly is supposed to be $Ammount * $Percent/12 with $Ammount being a constant of $50,000, which it is. $Monthly is supposed to take $50,000 and multiply it by the percent of that row which it does so that on row 1 it would be $50,000 * 0.01 and then divide that by 12 (12 months in a year) to display the monthly rate, and that part works fine. But then on row 2 it's supposed to simply take the value of $Monthly and add it to itself. Then on row 3 it's supposed to take the initial value and again add to itself. Instead, it keeps re-writing the variable with the new number. I need it to do like this: (this is just an example, not the actual numerical data) 3+3=6;  6+3=9; 9+3=12 etc.    Instead, it's doing this: 3+3=6;    6+6=12;    12+12=24;     24+24=48   etc. The picture I attached to this shows how it should look with correct results and all.

 <!--Indicates page is HTML5 compliant--><!DOCTYPE html>
<html>
<head>
<!--Titles the page at the top such as on the browser tab (w/ Chrome)-->
    <title>Monthly & Yearly Interest</title>
<!--Pulls the CSS styling from the main.css page-->
    <link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
    <main>
<h2>Yearly and Monthly Interest for $50,000 at Varying Rates</h2>
<table>
<tr><td>Rate</td><td>Annual Interest</td><td>Monthly Interest</td></tr>
    <?php


// Variables
$Ammount = 50000;
$Annual= number_format(500,2); 
$Percent = 1;
$Monthly = number_format($Ammount * ($Percent/100)/12,2);


//For loop to display Percent, Annual and Monthly data in their respective columns for one row
 for ($counter = 1; $counter <= 10; $counter++) {
echo "<td>$Percent%</td><td>$" . number_format($Annual, 2) . "</td><td>$" . number_format($Monthly, 2) . "</td>"; 


//Increments 
$Percent++;
$Annual+=500;
$Monthly += $Monthly;


//Next row
 echo "<tr></tr>"; 
 }
  
    ?>


</table>
    </main>
</body>
</html>

You need to be recalculating the monthly percentage inside the for loop. So this line needs to be inside the loop (before you output $Monthly)

$Monthly = number_format($Ammount * ($Percent/100)/12,2);

You do not add $Monthly together

You need to be recalculating the monthly percentage inside the for loop. So this line needs to be inside the loop (before you output $Monthly)

$Monthly = number_format($Ammount * ($Percent/100)/12,2);
You do not add $Monthly together

 

 

As already shown in my reply ( #8 ) yesterday

Edited by Barand
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.