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>";

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :).

Link to comment
Share on other sites

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

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.