Reputation: 31
So basically what I'm doing is creating php script which prints a table and updates and calculates the values depending on what is input into the form.
So I have a separate HTML file which contains the form, which passes 3 variables:
$tempStart
$tempEnd
$windSpeed
then I have a created a function which is used in each field of the table as follows:
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 full script is as follows:
<?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 ends up happening is it creates a table that looks like this:
https://i.sstatic.net/Iv00i.png
The header and the left most column is correct, but it seems to only calculate the yellow cells with the same set of variables. It doesn't update the variables according to what is in the header and left most column to the formula.
So basically, every 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. Thanks!
Upvotes: 3
Views: 225
Reputation: 906
Your function doesn't really do anything. The variable $Twc only exists inside your function, and only that one time. You need to run the function multiple times with different data for different results, and you need to get the variable OUT of your function (via return) each 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 your for loop when you need to get the result.
Upvotes: 1
Reputation: 91734
Well, you are only calling your function once, before the loops, and printing that same result in every table cell.
You should call your function with the actual variables everywhere where you want to print it.
I would also return the calculated value from the function instead of passing it by reference, that makes it a lot clearer and more logical (to me at least...).
Upvotes: 1