This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Functions, Part I Forum

View Course

743 points
Picture
Submitted by
mohdennis
about 2 years ago

PHP & html dividing table in

I have a question which is not entirely related to codecademy, but I don't know who I could ask this besides the codecademy users.

Let me just explain what I need to do to finish this excercise.

The excercise is that I need to create a table which converts kilometers to miles. The code works perfectly, but I'm having trouble with the table. I need to make the output like this (in one table);

1                       21
2                       22
..                      ..
19                      39
20                      40

So the code runs, creates a row, goes up to 20 and than continues with new cells next to the first two, which
then runs till 40, all in one table. It must be easily done but I just can't figure out how.
If someone could explain this to me, I would be very gratefull.

    <?php
            $mile = 1.609;
            echo "<table border='1'>";
            echo "<tr><th>Miles</th><th>Kilometers</th></tr>";
            $total = 40;
            $half = $total / 2;

            for ($i = 1; $i <= $total; $i++) {
                if ($i == ($half + 1) && $i <= $total) {
                    echo "</table>";
                    echo "<table border='2'>";
                    echo "<tr><th>Miles</th><th>Kilometers</th></tr>";                    
                }
                echo "<tr>";
                echo "<td>" . $i . "</td>";
                echo "<td>" . ($i * $mile) . "</td>";
                echo "</tr>";
            }
    ?>