duanke8011
duanke8011
2015-06-17 18:41

PHP(简单):循环问题

已采纳

I'm very new to programming, I am trying to take create a converter from Celsius to Fahrenheit to Kelvin. The user inputs the values (in celsius) they want converted in the 2 input boxes and it creates a table using loops. The first set of data where it outputs the amounts in celsius looks great however the second data stream (fahrenheit) will only output one value of celsius which is the converted value of the final nuber in the celsius loop.

<form name="calculator" action="" method="post">
    From: <input class="inputbox" type="number" name="one" value="" /><br />
                <p>to</p><br>
    To: <input class="inputbox" type="number" name="two" value="" /><br />
        <input type="submit" class="submit" name="submit" value="Get Conversions!" />
    </form>
    <br>

    <table border='1' cellpadding='5px'>
            <tr>
                <th>Degrees Celsius</th>

                <?php 
                    if ($_POST['submit']) {
                        $one = $_POST['one'];
                        $two = $_POST['two'];
                    }

                        if ($two < $one) {
                            echo "<p>Please put the lowest number in the first input box.</p>";
                        } else if ($one < (-273) OR $two < (-273)) {
                            echo "<p>   Tempature cant go below -273 Celsius (0 kelvin), please enter higher values.</p>";
                        } else {
                            $c = $one - 1;

                                do {
                                    $c++;
                                    echo "<td>" . $c . "</td>";
                                } while ($c < $two);
                        }

                ?>
            </tr>
            <tr>
                <th>Degrees Fahrenheit</th>

                <?php 

                    $f = (1.8 * $c) + 32;

                                do {
                                    $c++;
                                    echo "<td>" . $f . "</td>";
                                } while ($c < $two);

                                $k = $x - 273;
                ?>
            </tr>
    </table>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongshang5862 dongshang5862 6年前

    Your have 2 problems in your code.

    One problem is that you are only assigning c once, when you are running the first while, it's counting C up and then when you get to the second do{}while() it's allready at the maximum.

    you should "reset" C after The first while loop like here:

    <th>Degrees Fahrenheit</th>
    
                    <?php 
      $c = $one - 1;
    

    Secondly your only counting your f variable once, you should either make a function for it (might be overkill in this case) or move your calculation of f down inside the while loop, the last part of your code would be something like this Degrees Fahrenheit

                <?php 
                    $c = $one - 1;
                                do {
                                    $f = (1.8 * $c) + 32;
                                    $c++;
                                    echo "<td>" . $f . "</td>";
                                } while ($c < $two);
    
                                $k = $x - 273;
                ?>
            </tr>
    
    点赞 评论 复制链接分享