Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to multiply PHP variables in this for loop

I’m getting values from a form and trying to multiply them with values on the left hand side incremented in 5s. I added in a picture of the results too.

Code:

<?php

if ($_SERVER["REQUEST_METHOD"] == "GET") {
    $costOne = trim($_GET["c1"]);
    $costTwo = trim($_GET["c2"]);
    $costThree = trim($_GET["c3"]);
    $costFour = trim($_GET["c4"]);
    $costFive = trim($_GET["c5"]);
    $minimum = trim($_GET["min"]);
    $maximum = trim($_GET["max"]);
}

echo '<table>';
echo '<tr>';
echo '<th>Cost per person<br>Party size</th>';
echo "<th>$costOne</th>";
echo "<th>$costTwo</th>";
echo "<th>$costThree</th>";
echo "<th>$costFour</th>";
echo "<th>$costFive</th>";
echo '</tr>';


for ($col=$minimum; $col <= $maximum; $col+=5) {
    echo "<tr>";
    echo "<td>$col</td>";
    echo "<td>$col*$costOne</td>";
    echo "<td>$col*$costTwo</td>";
    echo "<td>$col*$costThree</td>";
    echo "<td>$col*$costFour</td>";
    echo "<td>$col*$costFive</td>";
    echo "</tr>";
}
echo '</table>';
?>

Result

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this:

for ($col=$minimum; $col <= $maximum; $col+=5) {
echo "<tr>";
echo "<td>" .$col. "</td>";
echo "<td>" .$col*$costOne. "</td>";
echo "<td>" .$col*$costTwo. "</td>";
echo "<td>" .$col*$costThree. "</td>";
echo "<td>" .$col*$costFour. "</td>";
echo "<td>" .$col*$costFive. "</td>";
echo "</tr>";
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading