3

基本的に私がやっていることは、テーブルを出力し、フォームに入力された内容に応じて値を更新および計算するphpスクリプトを作成することです。

したがって、3 つの変数を渡すフォームを含む別の HTML ファイルがあります。

$tempStart
$tempEnd
$windSpeed

次に、テーブルの各フィールドで次のように使用される関数を作成しました。

        function windChillCalc(&$Twc, $Temp, $Wind)
        {
    $Twc = 13.12 + 0.6215*$Temp - (11.37*(pow($Wind, 0.16))) + (0.3965*$Temp*(pow($Wind, 0.16)));

        }

完全なスクリプトは次のとおりです。

<?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>

最終的には、次のようなテーブルが作成されます。

http://i.stack.imgur.com/Iv00i.png

ヘッダーと一番左の列は正しいですが、同じ変数セットを持つ黄色のセルのみを計算しているようです。ヘッダーの内容に従って変数を更新せず、数式の左端の列にします。

したがって、基本的に、すべての黄色のセルは次を使用して計算されます。

$Wind = 7
$Temp = -5

みんな助けてください!数時間以内にこれを修正する必要があります。これが私の最後の希望です。ありがとう!

4

2 に答える 2

1

あなたの機能は実際には何もしません。変数 $Twc は関数内にのみ存在し、その 1 回だけです。異なる結果に対して異なるデータで関数を複数回実行する必要があり、毎回関数の変数 OUT を (return 経由で) 取得する必要があります。

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
}

そして、やります

$Twc = windChillCalc($Twc,$cTemp,$cWindSpeed); 

結果を取得する必要がある場合は、 for ループ内で。

推奨読書。

于 2013-01-28T23:22:37.593 に答える
1

さて、ループの前に関数を 1 回だけ呼び出して、同じ結果をすべてのテーブル セルに出力します。

出力したい場所ならどこでも、実際の変数を使用して関数を呼び出す必要があります。

また、参照渡しではなく、関数から計算された値を返します。これにより、より明確で論理的になります(少なくとも私にとっては...)。

于 2013-01-28T23:05:10.110 に答える