This program will take the user\'s input as a temperature in degrees Fahrenheit.
ID: 3591029 • Letter: T
Question
This program will take the user's input as a temperature in degrees Fahrenheit. The user will then click a radio button indicating whether a table of temperatures, starting with the entered value, will be displayed in increments of 5 degrees F or 10 degrees F. When a submit button is clicked your code will display a HTML table of temperature values. The first column will be temperatures in degrees Fahrenheit, given in increments of 5 or 10 degrees F, depending on the radio button clicked. The second column of the table will be the Celsius equivalent of the Fahrenheit temperature.
degC = (degF - 32) / 1.8;
The table will include a 100 degree F range. So, if the user enters 50 and clicks the 10 degree increment radio button, the table will include 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, and 150 degrees Fahrenheit values and their Celsius equivalents. If the user enters 50 and clicks the 5 degree increment radio button, the same temperature range will be displayed for degrees F, but in increments of 5 (50, 55, 60, 65, ..., 140, 145, 150).
To get full credit:
Except for the opening and closing table tags, your HTML table rows must be generated using a loop, where the starting temperature comes from the user's textbox entry, and number of iterations is based on which radio button was clicked.
For this program you do not need to validate the user's input; assume the user enters a reasonable number for the temperature and clicks one of the radio buttons before clicking submit.
The Celsius temperature should be rounded to 1 decimal place. use round($x, 2) if you want.
Your HTML form will contain a textbox asking for a starting temperature value in degrees Fahrenheit, and 2 radio buttons. One radio button will have a value property of 5, the other one a value property of 10. As mentioned, the radio buttons will determine the increment value for the table. There will also be a submit button to submit the form.
Your project folder needs to contain 1 file, and only one file, which posts back to itself. This technique is discussed in the lecture notes and videos on the course website.
When the user first requests a page a HTML form is displayed asking for the user's input. When they submit the form, the table of temperatures displays.
Here is a possible example. Your form needs to include these element, but the style and layout are up to you. There are a very wide variety of HTML backgrounds in this class; I will not force a particular HTML standard. So you may do the table any reasonable way in terms of style, heading, and so on.
First, the form that displays when the user first requests the page. Example values are shown.
Here is one possible output based on the above (start at 20 degrees, increment of 10 degrees).
This code displays a table of values and that value squared, starting at 5, and going up until 50 more than 5, in increments of 5.
$x = 5;
echo "<table>";
for($i = $x; $i < $x + 50; $i = $i + 5) {
echo "<tr>";
$y = $i * $i;
echo "<td>" . $i . "td> <td>" . $y . "td>";
echo "</tr>";
}
echo "<table>";
Explanation / Answer
<?php
if( $_POST["temperature"] || $_POST["increment"] ) {
//get he increament value and temperature from post request
$increment = $_POST['increment'];
$temperature = $_POST['temperature']
//html table
echo "<table>";
// Create Table header
echo "<tr>";
echo "<th>Temperature(F)</td>";
echo "<th>Temperature(C)</th>";
echo "</tr>";
// For loop to increament the temperature and print the table row
for($temp_in_fahrenheit = $temperature; $temp_in_fahrenheit < $temperature + 100; $temp_in_fahrenheit = $temp_in_fahrenheit + $increment) {
echo "<tr>";
//calculate temperature in celcius
$temp_in_celcius = ($temp_in_fahrenheit - 32) / 1.8;
//tempereature in fahrnheit
echo "<td>" . $i . "</td>";
//temperature in celcius
echo "<td>" . $temp_in_celcius . "</td>";
echo "</tr>";
}
echo "<table>";
exit();
}
?>
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "POST">
<!--Input text box-->
Temperature: <input type = "number" name = "temperature" placeholder="Enter Temperature in Fahrenheit" />
<!--Radio Buton to choose increment-->
<input type="radio" name="increment" value="5" checked> 5<br>
<input type="radio" name="increment" value="10`"> 10`<br>
<!--Submit Button-->
<input type = "submit" />
</form>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.