Jump to content

[SOLVED] Changing colours to match data in a table


Fearpig

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

::)

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.