Jump to content

[SOLVED] table background color based on value


peterbarone

Recommended Posts

I'm having a problem. I'm trying to make this happen on query from my database . But if someone could help me with this I should be able to get it to work.

 

 

$h1 = 4;
$h2 = 5;



if ($h1 == 4){$color="green";}
if ($h2 == 5){$color="yellow";}

print "<table><tr>";
print "<td bgcolor=$color>$h1</td>";
print "<td bgcolor=$color>$h2</td>";
print "</tr></table>";

You forgot to escape your quotes here try this

 

$h1 = 4;
$h2 = 5;



if ($h1 == 4){$color1="green";}
if ($h2 == 5){$color2="yellow";}

print '<table><tr>';
print '<td bgcolor="'.$color1.'">$h1</td>';
print '<td bgcolor="'.$color2.'">$h2</td>';
print '</tr></table>';

Your logic is wrong, you are contradicting yourself and $color will always be yellow since h2 does equal 5. You would need separate names for the colors or else the last true statement will always be the color, that or do an elseif, then the first true statement will always be the color.

 

What exactly are you trying to accomplish?

 

 

EDIT::

 

You forgot to escape your quotes here try this

 

$h1 = 4;
$h2 = 5;



if ($h1 == 4){$color="green";}
if ($h2 == 5){$color="yellow";}

print "<table><tr>";
print '<td bgcolor='$color'>$h1</td>';
print '<td bgcolor='$color'>$h2</td>';
print '</tr></table>';

 

Flawed code. For one variables cannot be interpreted inside single quotes, for two that is a syntax error not adding the . for concatination and for three his original code was just fine, may not of been well formed html but it should have worked.

What exactly are you trying to accomplish?

 

$sh1= ($h1-$p1);
  
  if ($sh1 == -2){$color="green";}
  if ($sh1 == -1){$color="yellow";}
  if ($sh1 == 1) {$color="orange";}
  if ($sh1 == 0) {$color="blue";}
  if ($sh1 == 2) {$color="red";}
  if ($sh1 >= 3) {$color="white";}
echo "<table><tr><td bgcolor=$color>$h1</td>";
echo "<td bgcolor=$color>$h2</td>";
echo "<td bgcolor=$color>$h3</td>";
echo "<td bgcolor=$color>$h4</td>";
echo "<td bgcolor=$color>$h5</td>";
echo "<td bgcolor=$color>$h6</td></tr></table>";

 

That is what I'm trying to do. 

why not make an array

<?php
$headings = array();
$headings[0] = "";
$headings[1] = "yellow";
$headings[2] = "green";
$headings[3] = "blue";
echo "<table><tr>
<td bgcolor=\"".$headings[$h1]."\">".$h1."</td>
<td bgcolor=\"".$headings[$h2]."\">".$h2."</td>
<td bgcolor=\"".$headings[$h3]."\">".$h3."</td>
<td bgcolor=\"".$headings[$h4]."\">".$h4."</td>
<td bgcolor=\"".$headings[$h5]."\">".$h5."</td>
</tr></table>";
?>

one question.

 

$h1 can equal 1-10

$h2 can equal 1-10

$h3 can equal 1-10

 

and so on ...........

 

if $h1 = 4 td color needs to be yellow

if $h1 = 5 td color needs to be red

if $h2 = 3 td color needs to be blue

if $h2 = 4 td color needs to be yellow

 

ECT.....

 

How can I pick the color based on the value of $h1  and a different color for the value $h2

That makes sense.

 

 
($sh1 == -2) {$color="green";}
($sh1 == -1){$color="yellow";}
($sh1 == 1) {$color="orange";}
($sh1 == 0) {$color="blue";}
($sh1 == 2) {$color="red";}
($sh1 >= 3) {$color="white";}

 

But I'm very new at this and can't code it correct. 

I'd put something like this within a foreloop dowhile loop

 

$i++;
if($i%2!=0)
          $color="green";
else
          $color="yellow";

 

Now everytime a row gets generated, it will pass the above script first, and it basically alternates the 2 colors. At least i think thats what you are looking to do :).

ok due to my egnorance.  let me try it this way for all of you that are nice enough to help me

 

[td bgcolor=yellow]4
[td bgcolor=red]3[/td][/tr][/table][td bgcolor=green]5[/td][/tr][/table][td bgcolor=blue]6[/td][/tr][/table]

 

I need bgcolor to be set based on the value of the cell

 

if cell = 4 bgcolor= yellow

if cell = 5 bgcolor =green

 

Sorry I can't explain it better. I'm new

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.