1

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'

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.

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.

*************EDIT*************

<!--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 updated my code and cleaned it up. Thank you for all the help. I'm still having an issue with this though. 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 calculation to be 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 this:

http://tinypic.com/r/2uj5axk/8

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • I've tried removing the column tags, I've tried removing the percent string. I've watched a Youtube video and searched around with no luck. I keep getting the same error and I don't understand. –  Sep 03 '15 at 03:20
  • Your strings aren't concatenated properly. You need another `.` before `$percent`. Try not to mix which quotes you use to wrap strings (unless necessary) and use one or the other. Also, when wrapping strings with double quotes you can simply type the variable name in there and PHP will interpolate the value. This is not true with single quotes which will not interpolate or do any escaping. (e.g. `"\n"` and `'\n'` are two different things. – Crackertastic Sep 03 '15 at 03:27

2 Answers2

1

When you're doing string concatenation in PHP you need to use . (dot) operator.

echo "<td>"$percent.'%'"</td>";

must be written as

echo "<td>".$percent."%</td>"; or echo "<td>$percent%</td>";

also for loop consists of 3 part

for (init counter; test counter; increment counter) {
  code to be executed; 
}

if you want only test counter, you can use it as

for (;$percent <= 10;) {
 echo "<td>$percent%</td>";
 $percent++
}

or

for ($percent = 1 ; $percent <= 10; $percent++) {
 echo "<td>$percent%</td>";
}

HTML table roughly consists of 3 parts

<table>
  <tr>
    <td>
    </td>
  <tr>
</table>

You have missed the <tr>

K T
  • 11
  • 1
  • 3
  • The only issue I'm having with that is that I cant get the next column to appear beside the first. Rather it shows under it. Am I going about this entirely wrong in order to have three separate columns with three separate for loops? "; } ?>
    $percent%
    –  Sep 03 '15 at 04:00
1

the syntax should look like

<table border="1">
    <?php
        $percent = 1;
        for($percent = 1; $percent < 10; $percent++)
        {
            echo "<tr><td> $percent % </td></tr>";
        }
    ?>
</table>

or if u want to use your

for ($percent <= 10) {
        echo "<td>"$percent.'%'"</td>";
        $percent++;
      }

use while instead

<?php
$percent = 1;
        while($percent <= 10)
        {
            echo "<tr><td> $percent % </td></tr>";
            $percent++;
        }
?>
Syafiq Azwan
  • 178
  • 1
  • 2
  • 10