Variable not passing / updating in PHP loop
So, basically I am creating a php script that prints the table and updates and calculates the values ββdepending on what is entered into the form.
So, I have a separate HTML file that contains a form that passes 3 variables:
$tempStart
$tempEnd
$windSpeed
then I have a created function that is used in each field of the table like this:
function windChillCalc(&$Twc, $Temp, $Wind)
{
$Twc = 13.12 + 0.6215*$Temp - (11.37*(pow($Wind, 0.16))) + (0.3965*$Temp*(pow($Wind, 0.16)));
}
The complete script looks like this:
<?php
function windChillCalc(&$Twc, $Temp, $Wind)
{
$Twc = 13.12 + 0.6215*$Temp - (11.37*(pow($Wind, 0.16))) + (0.3965*$Temp*(pow($Wind, 0.16)));
}
?>
<html>
<head>
<title>Wind Chill Temperature Table</title>
</head>
<?php
extract($_REQUEST);
print "<h1>Wind Chill Temperature Table</h1>";
if(!empty($tempStart) && !empty($tempEnd) && !empty($windSpeed))
{
print "<h3>Air Temperature from: ".$tempStart."°C to ".$tempEnd."°C</h3>";
print "<h3>For Wind Speed from 7 km/h to ".$windSpeed." km/h</h3>";
}
else
print "<h2>Air Temperature START is not numeric</h2><br />";
$tablecolor="white";
$headercolor="#00ffff";
$windcolor="red";
$tempcolor="yellow";
$cTemp=$tempStart;
$cWindSpeed="7";
windChillCalc($Twc,$cTemp,$cWindSpeed);
print "<table border=1><tr>";
print "<th width=275 bgcolor=$headercolor>Wind Speed (km/h)/Air Temp.</th>";
for ($cTemp = $tempStart; $cTemp < $tempEnd; $cTemp+=5){
print "<th width=100 bgcolor=$headercolor>$cTemp</th>";
}
if ($cTemp != $tempEnd){
print "<th width=100 bgcolor=$headercolor>$tempEnd</th></tr>";
$cTemp = $tempStart;
}
for ($cWindSpeed = 7; $cWindSpeed < $windSpeed; $cWindSpeed+=0.5){
print "<tr>";
print "<td align=center bgcolor=$windcolor>$cWindSpeed</td>";
for ($cTemp = $tempStart; $cTemp < $tempEnd; $cTemp+=5) {
print "<td align=center bgcolor=$tempcolor>$Twc</td>";
}
if ($cTemp != $tempEnd){
print "$<td align=center bgcolor=$tempcolor>$Twc</tb></tr>";
$cTemp = $tempStart;
}
}
if ($cWindSpeed != $windSpeed){
print "<td align=center bgcolor=$windcolor>$windSpeed</td>";
for ($cTemp = $tempStart; $cTemp < $tempEnd; $cTemp+=5) {
print "<td align=center bgcolor=$tempcolor>$Twc</td>";
}
if ($cTemp != $tempEnd){
print "$<td align=center bgcolor=$tempcolor>$Twc</tb></tr>";
$cTemp = $tempStart;
}
}
print "</tr>";
print "</table>";
?>
</body>
</html>
What happens is a table is created that looks like this:
http://i.stack.imgur.com/Iv00i.png
The header and left column are correct, but only yellow cells with the same set of variables seem to be calculating. It doesn't update the variables to match what's in the header and leaves most of the columns in the formula.
So each yellow cell is calculated using:
$Wind = 7
$Temp = -5
Please help me guys! I need to fix this in the next few hours, this is my last hope. Thank!
source to share
Well, you only call your function once, before the loops, and print the same result in every cell of the table.
You have to call your function with actual variables wherever you want to print it.
I would also return the computed value from the function instead of passing it by reference, which makes it much clearer and more logical (at least for me ...).
source to share
Your function does nothing. The $ Twc variable only exists inside your function and only once. You need to run the function multiple times with different data for different results, and you need to get the OUT variable of your function (via return) every time.
function windChillCalc($Twc, $Temp, $Wind){
$Twc = 13.12 + 0.6215*$Temp - (11.37*(pow($Wind, 0.16))) + (0.3965*$Temp*(pow($Wind,0.16)));
return $Twc
}
And do
$Twc = windChillCalc($Twc,$cTemp,$cWindSpeed);
inside a for loop when you need to get the result.
source to share