2014-03-06 46 views
0

我想獲取MySQL輸出並將CSS條件格式應用於查詢輸出的PHP表。我在當前版本的代碼中接收到「數組到字符串的轉換」 - 這至少會產生一些錯誤。這是我到目前爲止。將數組轉換爲條件格式的字符串

<table> 
<thead> 
<tr> 
<th>Date</th> 
<th>High</th> 
<th>Low</th> 
<th>Average</th> 
<th>DFNorm</th> 
<th>HDD</th> 
<th>CDD</th> 
<th>Liquid</th> 
<th>Snow</th> 
<th>SDepth</th> 
<th>WAvg</th> 
<th>WMax</th> 
<th>Clouds</th> 
<th>Weather</th> 
</tr> 
</thead> 
<tbody> 

<?php 

$result = mysqli_query($con," 
select 
Date, 
High, 
Low, 
Average, 
DFNorm, 
HDD, 
CDD, 
Liquid, 
Snow, 
SDepth, 
WAvg, 
WMax, 
Clouds, 
Weather 
from CF6MCI order by Date DESC;"); 

while($row = mysqli_fetch_array($result)) 
{ 

$High = array(); 
$High[] = $row['High']; 

/* NOT WORKING BELOW THIS LINE !! 

$cHigh = array(); 
$cHigh = ''; 
switch ($High) 
    { 
    case ($High <= -25): $cHigh = 'C6TB25'; break; 
    case ($High <= -20) and ($High >= -24): $cHigh = 'C6TB20B24'; break; 
    case ($High <= -15) and ($High >= -19): $cHigh = 'C6TB15B19'; break; 
    case ($High <= -10) and ($High >= -14): $cHigh = 'C6TB10B14'; break; 
    case ($High <= -5) and ($High >= -9): $cHigh = 'C6TB05B09'; break; 
    case ($High <= 0) and ($High >= -4): $cHigh = 'C6T000B04'; break; 
    case ($High <= 4) and ($High >= 1): $cHigh = 'C6T004001'; break; 
    case ($High <= 9) and ($High >= 5): $cHigh = 'C6T009005'; break; 
    case ($High <= 14) and ($High >= 10): $cHigh = 'C6T014010'; break; 
    case ($High <= 19) and ($High >= 15): $cHigh = 'C6T019015'; break; 
    case ($High <= 24) and ($High >= 20): $cHigh = 'C6T024020'; break; 
    case ($High <= 29) and ($High >= 25): $cHigh = 'C6T029025'; break; 
    case ($High <= 32) and ($High >= 30): $cHigh = 'C6T032030'; break; 
    case ($High <= 34) and ($High >= 33): $cHigh = 'C6T034033'; break; 
    case ($High <= 39) and ($High >= 35): $cHigh = 'C6T039035'; break; 
    case ($High <= 44) and ($High >= 40): $cHigh = 'C6T044040'; break; 
    case ($High <= 49) and ($High >= 45): $cHigh = 'C6T049045'; break; 
    case ($High <= 54) and ($High >= 50): $cHigh = 'C6T054050'; break; 
    case ($High <= 59) and ($High >= 55): $cHigh = 'C6T059055'; break; 
    case ($High <= 64) and ($High >= 60): $cHigh = 'C6T064060'; break; 
    case ($High <= 69) and ($High >= 65): $cHigh = 'C6T069065'; break; 
    case ($High <= 74) and ($High >= 70): $cHigh = 'C6T074070'; break; 
    case ($High <= 79) and ($High >= 75): $cHigh = 'C6T079075'; break; 
    case ($High <= 84) and ($High >= 80): $cHigh = 'C6T084080'; break; 
    case ($High <= 89) and ($High >= 85): $cHigh = 'C6T089085'; break; 
    case ($High <= 94) and ($High >= 90): $cHigh = 'C6T094090'; break; 
    case ($High <= 99) and ($High >= 95): $cHigh = 'C6T099095'; break; 
    case ($High <= 104) and ($High >= 100): $cHigh = 'C6T104100'; break; 
    case ($High <= 109) and ($High >= 105): $cHigh = 'C6T109105'; break; 
    case ($High <= 114) and ($High >= 110): $cHigh = 'C6T114110'; break; 
    case ($High <= 119) and ($High >= 115): $cHigh = 'C6T119115'; break; 
    case ($High >= 120): $cHigh = 'C6T120'; break; 
    } 
NOT WORKING ABOVE THIS LINE!! */ 

echo "<tr>"; 
echo "<td>" . $row['Date'] . "</td>"; 
echo "<td>" . ($High) . "</td>"; // class='" $cHigh "' -- THIS IS WHERE IT ERRORS STRING 
echo "<td>" . $row['Low'] . "</td>"; // class='" $cLow "' 
echo "<td>" . $row['Average'] . "</td>"; 
echo "<td>" . $row['DFNorm'] . "</td>"; 
echo "<td>" . $row['HDD'] . "</td>"; 
echo "<td>" . $row['CDD'] . "</td>"; 
echo "<td>" . $row['Liquid'] . "</td>"; // class='" $cLiquid "' 
echo "<td>" . $row['Snow'] . "</td>"; // class='" $cSnow "' 
echo "<td>" . $row['SDepth'] . "</td>"; 
echo "<td>" . $row['WAvg'] . "</td>"; 
echo "<td>" . $row['WMax'] . "</td>"; // class='" $cWMax "' 
echo "<td>" . $row['Clouds'] . "</td>"; // class='" $cClouds"' 
echo "<td>" . $row['Weather'] . "</td>"; 
echo "</tr>"; 

} 

?> 

回答

1

您無法將數組與一個整數進行比較。

更換

$High = array(); 
$High[] = $row['High']; 

$High = $row['High']; 

沒有必要使其成爲一個數組,你只需要一個值。

如果你想檢查一系列值,你也不能做switch($High)。相反,您可以使用switch(true)

順便說一句,你不需要製作$cHigh一個數組。

+0

謝謝!那樣做了!我發現爲了使格式化工作,應用和使用相同的規則。 ($ Var)。在那裏! – user3260912

+0

如果這回答了您的問題,請點擊打勾將其標記爲已接受。 :-) –

+0

我有一個問題 - 我能夠得到第一個條件格式化部分昨天晚上工作,但它似乎保持錯誤,只是在我開始定義第二個開關後顯示一個白頁。我假設這是因爲我正在使用開關($ High)和開關($ Low)而不是兩個開關(true)[s]。 – user3260912

相關問題