Fearpig Posted July 3, 2007 Share Posted July 3, 2007 Hi guys, Could someone have a look at my code for me... I'm trying to change the colour of a cell in a table according to the data retrieved from a database. In the code below I first define the background colour as white (#FFFFFF), then I pull the data from an SQL database to populate the table. The efficiency column should be coloured according to the efficiency; A=Green, B=Blue/Green, D=Yellow. At the moment all the cells in this column are green and showing A efficiency regardless of what is stored in the database. <?php $Bg = '#FFFFFF'; $sql="SELECT * FROM tbl_Domestic_Boilers ORDER BY $Order"; //$sql="SELECT * FROM qry_Domestic_Boilers_and_Docs"; $result=odbc_exec($conn,$sql); if (!$result) {exit("Error in SQL");} echo "<table class='BodyText1' border=1>\n"; echo "<tr><th>Product</th><th>Type</th><th>Warranty</th><th>Efficiency</th><th>Power Output</th></tr>\n"; while (odbc_fetch_row($result)) { $Product=odbc_result($result,"Product"); $Boiler_Type=odbc_result($result,"Type"); $Warranty=odbc_result($result,"Warranty"); $Efficiency=odbc_result($result,"Efficiency"); $Output_Power=odbc_result($result,"Power"); $Part_ID=odbc_result($result,"Part_ID"); if ($Efficiency = 'A'){ $Bg = '#467A4F'; }elseif ($Efficiency = 'B') { $Bg = '#60A17D'; }elseif ($Efficiency = 'D') { $Bg = '#E6EE38'; }else { $Bg = '#FFFFFF'; } echo "<tr><td>$Product</td><td>$Boiler_Type</td><td>$Warranty</td>"; echo "<td bgcolor='$Bg'>$Efficiency</td><td>$Output_Power</td></tr>"; } echo "</table>\n"; ?> Any help would be appreciated as this is really bugging me now!! Cheers guys. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted July 3, 2007 Share Posted July 3, 2007 shouldn't it be == to compare not = Quote Link to comment Share on other sites More sharing options...
Fearpig Posted July 3, 2007 Author Share Posted July 3, 2007 OK... so I changed the if/else statement to the following: <?php if ($Efficiency == 'A'){ $Bg = '#467A4F'; }elseif ($Efficiency == 'B') { $Bg = '#60A17D'; }elseif ($Efficiency == 'D') { $Bg = '#E6EE38'; }else { $Bg = '#FFFFFF'; } ?> ...and now I get the correct data in that column but all of the cells are still white. Quote Link to comment Share on other sites More sharing options...
Fearpig Posted July 3, 2007 Author Share Posted July 3, 2007 Solved it... follow back from the start, take a deep breath and look at it again!! I had the efficiency saved into an nvchar(10) field so the values were being saved as "A ", "B "... (a letter and 9 spaces). I changed it to a nvchar(1) field and it worked straight away! DOH! Cheers for helping corillo181. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted July 3, 2007 Share Posted July 3, 2007 you might want to be more eficient also by using switch($Efficiency){ case "A": $Bg = '#467A4F'; break; case "B": $Bg= '#60A17D'; break; case "C": $Bg = '#E6EE38'; break; default: $Bg = '#FFFFFF'; } it looks better and faster. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.