Aaron Thole
Aaron Thole

Reputation: 21

How to combine foreach loops in php

In trying to understand php and loop so Im creating an application that increases the saving by multiplying the rate each year. In the application the user puts in a set value and the rate from a radio button. After the submit button is clicked a table is shown that displays the the next ten years counted and the money saved each year until it hits ten years. Currently I have it show year one ten times, then two ten times until the table shows 10 years ten times. How do I fix my loops to display just ten years and the correct savings for each year.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
    table, th, td {
        border: 1px solid black;
    }
    </style>
</head>
<body>
    <form action="savings.php" method="POST">
        <?php
        if($_SERVER["REQUEST_METHOD"]=="GET"){
            echo'<p>Principle:<input type="text" id="Principle" name="Principle"></p>';
            echo'Interest Rate:<input type="radio" name="MyRadio" value="1"><label>1%</label>';
            //
            echo'<input type="radio" name="MyRadio" value="5"><label>5%</label>';
            //
            echo'<input type="radio" name="MyRadio" value="10"><label>10%</label>';
            //
            echo'<p><input type="submit" value="Submit"></p>';
        }
        else{
            if($_SERVER["REQUEST_METHOD"]=="GET"){
                
            }
            else
            $Principle = (int)$_REQUEST["Principle"];
            $radioVal = $_POST["MyRadio"];
            echo'<table>';
            echo'<tr><th>Year</th>';
            echo'<th>Savings</th>';
            $Principle= $Principle + ($Principle * .05);
            foreach (range(1,10,1) as $Year){
                foreach (range(1,10,1) as $Rate){

                echo "<tr><td>$Year</td>
                <td>$$Principle</td></tr>";
            }
        }
            echo'</table>';
        }
            
        ?>
    </form>
</body>
</html>

Upvotes: 2

Views: 63

Answers (1)

Kien Nguyen
Kien Nguyen

Reputation: 2691

  1. Only 1 for loop is needed.
  2. You are using the fixed value of 0.05 for calculating expression so it is not correct, it should be: $Principle = $Principle + ($Principle * $radioVal / 100);.
  3. You have to put that expression inside the loop.
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    table, th, td {
      border: 1px solid black;
    }
  </style>
</head>
<body>
<form action="savings.php" method="POST">
  <?php
  if($_SERVER["REQUEST_METHOD"]=="GET"){
    echo'<p>Principle:<input type="text" id="Principle" name="Principle"></p>';
    echo'Interest Rate:<input type="radio" name="MyRadio" value="1"><label>1%</label>';
    //
    echo'<input type="radio" name="MyRadio" value="5"><label>5%</label>';
    //
    echo'<input type="radio" name="MyRadio" value="10"><label>10%</label>';
    //
    echo'<p><input type="submit" value="Submit"></p>';
  }
  else{
    if($_SERVER["REQUEST_METHOD"]=="GET"){

    }
    else
    $Principle = (int)$_REQUEST["Principle"];
    $radioVal = $_POST["MyRadio"];
    echo'<table>';
    echo'<tr><th>Year</th>';
    echo'<th>Savings</th>';
    foreach (range(1,10,1) as $Year){
        $Principle = $Principle + ($Principle * $radioVal / 100);
        echo "<tr><td>$Year</td>
                <td>$$Principle</td></tr>";
    }
    echo'</table>';
  }

  ?>
</form>
</body>
</html>

Upvotes: 1

Related Questions