做while循环而不在php

时间:2018-11-16 05:01:47

标签: php html

我一直在工作一个递增重复的循环,同时在表中显示开尔文和华氏度。我正在使用do while循环来执行此操作,但是该函数不会循环,并且不会从表单中的正确数字开始。

到目前为止,我完成的代码是:

<body>

<form action="" method="post">

Start temperature in degrees:<input type="text" name="start"></input><br />
End temperature in degrees:<input type="text" name="end"></input><br />
How should the list be incremented?:<input type="text" name="inc"></input><br />

<input type="submit" name="sub" value="Submit"></input><br />

</form>

<?php

$a = 1;

if ($_POST['sub']) {

$a = 0;
$start = $_POST['start'];
$end = $_POST['end'];
$inc = $_POST['inc'];

$x = $start;

do {

    $x = $x + $inc;
    $y = $x + 273;
    $z = (1.8 * $x) + 32;

} while ($x >= $end);

}
?>

<?php
if ($a != 1) {
?>


<table>

<tr>
    <th>Celsius</th>
    <th>Kelvin</th>
    <th>Fahrenheit</th>
</tr>

<tr>
    <th>
    <?php
    echo "$x degrees <br />"; 
    ?>
    </th>

    <th>
    <?php
    echo "$y degrees <br />";
    ?>
    </th>

    <th>
    <?php
    echo "$z degrees <br />";
    ?>
    </th>
</tr>

</table>


<?php
}
?>


</body>

我是否必须在表的回显部分包括while循环才能使其循环?以及如何使循环以与表格相同的编号开始?

1 个答案:

答案 0 :(得分:0)

您需要吗?

<body>
<form action="" method="post">
    Start temperature in degrees:<input type="text" name="start"></input><br />
    End temperature in degrees:<input type="text" name="end"></input><br />
    How should the list be incremented?:<input type="text" name="inc"></input><br />
    <input type="submit" name="sub" value="Submit"></input><br />
</form>
<table >
    <tr>
        <th>Celsius</th>
        <th>Kelvin</th>
        <th>Fahrenheit</th>
    </tr>
    <?php
    $a = 1;
    if (isset($_POST['sub']) && $_POST['sub']) {
        $a = 0;
        $start = $_POST['start'];
        $end = $_POST['end'];
        $inc = $_POST['inc'];
        $x = $start;
        do {
            $x = $x + $inc;
            $y = $x + 273;
            $z = (1.8 * $x) + 32;
            ?>
            <?php
            if ($a != 1) {
                ?>
                <tr>
                    <th><?php echo "$x degrees <br />"; ?></th>
                    <th><?php echo "$y degrees <br />"; ?></th>
                    <th><?php echo "$z degrees <br />";?></th>
                </tr>
                <?php
            }
        } while ($x >= $end);
    }
    ?>
    </table>
</body>