1.11M Members

Temperature Loop Table

 
0
 

Hey guys, soo i'm new to PHP & I'm doing a course in school. I really really really need help. I have to get three input's;start temperature, stop temperature, & how much the users wants to increment it. then validate it for these...
that the user entered numbers in each field
that the start temperature is less than the end temperature
that the temperature increment is a positive (non-zero) number

Then outputs the temperature in a table...celsius in left coloum, fahrenheit in the right coloum, and each row increments.

So i have some code here to start off with. but I have absolutely no idea where to go from here. Can you guys please help me & explain everything to so in a way that a n00b can understand.!!! Thank you in Advance.

I'm really desperate and I need this asap!

The issues i'm having is with validation & outputting the result. it is supposed to be a self-ferring form aswell... I put the how the form should be output and the end of the code. I hope you guys understand it.

<p>Please enter Start & Stop Temperatures. Temperatures entered are in &deg;Celsius. </p>
<p>Then enter the amount in which you want the temperature to increment.</p>

<?php
//empty out error and result regardless of method that got you here
$error = "";
$result == FALSE;

if($_SERVER["REQUEST_METHOD"] == "GET"){
	//default mode when the page loads the first time
	//can be used to make decisions and initialize variables
	$start = "";
	$stop = "";
	$incr = "";
}else if($_SERVER["REQUEST_METHOD"] == "POST"){
	//the page got here from submitting the form, let's try to process
	$num = trim($_POST["inputted_number"]); //the name of the input box on the form, white-space removed
	
	//let's do some data validation
	if(!isset($num) || $num == ""){
		//means the user did not enter anything
		$error = "You must enter something into the text box.";
	}else if(!is_numeric($num)){
		//means the user entered something, but not a number
		//give them a detailed message
		$error = "The value entered <u>MUST</u> be a number, you entered: " . $num;
		//empty out the invalid data
		$num = "";
	}
	
	if($error == ""){  //if error is an empty string
		//no errors, do the math
		//$result = $num . " squared is " . ($num * $num);
		$result == TRUE;
}
	}else{
		//there were problems, concatentate the TRY AGAIN message
		$error .= "<br/>Please Try Again";
		
	}

//NOTE: 
//the first two echos below show the errors or the result (these are empty the first time the page loads)
//the third of the following echo'es makes this page self-referring
//the name of the current file is outputted placed in the action of the form
//and the fourth of the following echo'es is what makes the form sticky, the
//number previously entered on the form, is automatically displayed in the value of the text input box
?>
<div id="form6">
<form action="<?php echo $_SERVER['PHP_SELF'];  ?>" method="POST" >
	<p>Start Temperature: <input type="text" name="start_inputted" value="<?php echo $start; ?>" size="5" />
    Stop Temperature: <input type="text" name="stop_inputted" value="<?php echo $stop; ?>" size="5" />
    Increment By: <input type="text" name="incr_inputted" value="<?php echo $increment; ?>" size="5" /></p>
	<p><br/><input type="submit" value="I'm Feeling Lucky!" /></p>
</form>
<!-- Table markup-->
<?php
$intFConverter;
echo "<table id='pattern-style-a'>";
echo "<thead>";
echo "<tr align='center'>";
echo "<th scope='col'>Celsius</th>";
echo "<th scope='col'>Fahrenheit</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
for($celsius=-40;$celsius<=110;$celsius+=10)
{
echo "<tr align='center'>";
echo "<td>$celsius&deg;</td>";
echo "<td>".converter($celsius)."&deg;</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>"

?>
<?php
function converter($celsius)
{
	
   $intFConverter=(9.0/5.0)*$celsius + 32;
    return $intFConverter;
}
?>
<h2><?php echo $result; ?></h2>
<h3><?php echo $error; ?></h3>
</div>
 
0
 
<?php
//default variables
$output = "";  	//don't change
$error="";	//don't change

$inc = 1;
$start = 25;
$end = 50;

//this works off the submit button
if(isset($_POST['sub'])){
	//trim all inputs
	$p = array_map('trim',$_POST);
	//check all inputs are numeric
	if(!is_numeric($p['inc']) || !is_numeric($p['start']) || !is_numeric($p['stop'])){
		$error .= "You need all three inputs to be numeric";
	}else{
		//check incrementer is positive and stop temp greater than start temp
		if(($p['inc'] > 0 && $p['start'] >= $p['stop'])){
			$error .= "Increment must be positive and the start temp must be less than the end temp.";
		}else{
			//set start position for 'counter'
			$x = $p['start'];
			//set stop loop variable
			$finish = false;
			//loop - many ways you could do this
			while(!$finish){
				//calculate fahrenheit
				$f = $x * 9/5 + 32;
				//build output row 
				$output .= "<tr><td>$x</td><td>$f</td></tr>";	
				//enable stop variable once target temp reached
				if($x>=$p['stop'])$finish = true;
				//increment the 'counter' 
				$x += $p['inc'];
			}
			//build table if output has data in it
			if($output)$output = "<table border=\"1\"><thead><tr><th>&degC</th><th>&degF</th></tr></thead><tbody>$output</tbody></table>";
			//place new values into form display variables
			$inc = $p['inc']; $end = $p['stop']; $start = $p['start'];
		}
	}
}
?>
<form method="post">
	Increment: <input name="inc" value="<?php echo $inc;?>" />
	Start: <input name="start" value="<?php echo $start;?>" />
	Stop: <input name="stop" value="<?php echo $end;?>" />
	<input type="submit" value="Go" name="sub" />
</form>
<?php echo $error;echo $output;?>

THis isn't the best code ever, but just to hive you an idea. I tested it quickly and it seems to work.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: