3

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."&degC to ".$tempEnd."&degC</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.stack.imgur.com/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!

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202

2 Answers2

1

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...).

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • thanks for the QUICK answer. As you probably see by the basic question, I'm in the process of learning php, and as I do understand what you're saying, I'm not exactly sure how to accomplish that. – user2019835 Jan 28 '13 at 23:07
  • Ive spent the past 3 days on this lol, believe it or not, probably switched the code completely about 3-4 times This is closest I've gotten. I'm going to keep researching and see if I can figure out how to call the function from each cell. But if you might be so kind as to push me in the right direction, that would be awesome too ;) – user2019835 Jan 28 '13 at 23:07
  • @user2019835 Like I said, just call the function everywhere with the actual values of the second and third parameter right before you print `$Twc` – jeroen Jan 28 '13 at 23:08
  • Thank you so much, you and the other answer literally helped me figure something out that i spent probably 8 hours on yesterday. I honestly thought it was hopeless. Thank you so much! – user2019835 Jan 28 '13 at 23:34
1

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.

Recommended Reading.

Morgan
  • 867
  • 3
  • 11
  • 34
  • Thanks so much! I'm actually getting someone with this, I'm managing to get the correct values. now I just need to get them in the right columns, as they are one column over. Thanks so much, seriously , you guys are seriously amazing. – user2019835 Jan 28 '13 at 23:33
  • 1
    If you're going to use the returned value of this function, you should probably remove the 'by reference' (&) before &$Twc – thaJeztah Jan 28 '13 at 23:44
  • @theJeztah - Good catch, I didn't even notice the reference char in there. Should be removed. – Morgan Jan 29 '13 at 00:07