Jump to content

[SOLVED] Color Coding a specific Table Column from MYSQL output.


cuboidgraphix

Recommended Posts

Greetings again,

I have a php/mysql page that outputs an html table with field names and data rows.  I want to make it a lil more fancy by color coding a specific column. In this html table it's the last column, which is a percentage column.  I want to color code the number according to value whereby if it's more than 2.999 % it's red, if it's between 1.5 and 2.999 it's orange if it's below 1.5 percent it's green ... and so forth.

My problem is that I can't find a way to target a specific column.  On my script below, for instance, the IF statement targets all the columns.  Can someone please help me out?

 

The output part of my script is as follows:

 

<?php
print "<table width=\"800\">";	
print "<tr>";						
for($i = 0;$i < mysql_num_fields($data);$i++)
    {
    print "<th>" . mysql_field_name($data, $i) . "</th>";				
    } 
print "</tr>";			
for ($i = 0; $i < mysql_num_rows($data); $i++)
    {
    print "<tr>";
    $row = mysql_fetch_row($data);
        for($j = 0;$j < mysql_num_fields($data);$j++) 
            {
            if($row[$j] >= 2.9999)
                {
                echo("<td class=\"red\">" . $row[$j] . "</td>");
                }
            else
                {
                echo("<td>" . $row[$j] . "</td>");
                }
            }	
    print "</tr>";							
    } 
print "</table>";
?>

Link to comment
Share on other sites

Try this:

 

 

<?php
print "<table width=\"800\">";	
print "<tr>";						
for($i = 0;$i < mysql_num_fields($data);$i++)
    {
    print "<th>" . mysql_field_name($data, $i) . "</th>";				
    } 
print "</tr>";			
for ($i = 0; $i < mysql_num_rows($data); $i++)
    {
    print "<tr>";
    $row = mysql_fetch_row($data);
        for($j = 0;$j < mysql_num_fields($data);$j++) 
            {
              if(mysql_field_name($row[$j]) == "your_field_name_here")
              {
                if($row[$j] >= 2.9999)
                {
                  $class=" class=\"red\"";
                } 
            } 
echo("<td $class>" . $row[$j] . "</td>");
}
    print "</tr>";							
    } 
print "</table>";
?>

Link to comment
Share on other sites

Or, if you know you want the last field:

 

<?php
print "<table width=\"800\">";	
print "<tr>";
$num_fields = mysql_num_fields($data);						
for($i = 0;$i < $num_fields;$i++)
    {
    print "<th>" . mysql_field_name($data, $i) . "</th>";				
    } 
print "</tr>";			
for ($i = 0; $i < mysql_num_rows($data); $i++)
    {
    print "<tr>";
    $row = mysql_fetch_row($data);
        for($j = 0;$j < $num_fields;$j++) 
            {
            if($j = $num_fields){//last column
                if($row[$j] >= 2.9999) 
                    $class = 'red';
                elseif($row[$j] >= 1.5) 
                    $class = 'orange';
                else
                    $class = 'green';
            }else{
                $class = '';
            }
            echo("<td class=\"$class\">" . $row[$j] . "</td>");
            }	
    print "</tr>";							
    } 
print "</table>";
?>

<?php
print "<table width=\"800\">";	
print "<tr>";						
for($i = 0;$i < mysql_num_fields($data);$i++)
    {
    print "<th>" . mysql_field_name($data, $i) . "</th>";				
    } 
print "</tr>";			
for ($i = 0; $i < mysql_num_rows($data); $i++)
    {
    print "<tr>";
    $row = mysql_fetch_row($data);
        for($j = 0;$j < mysql_num_fields($data);$j++) 
            {
            if($row[$j] >= 2.9999)
                {
                echo("<td class=\"red\">" . $row[$j] . "</td>");
                }
            else
                {
                echo("<td>" . $row[$j] . "</td>");
                }
            }	
    print "</tr>";							
    } 
print "</table>";
?>

Link to comment
Share on other sites

can use something like this

 

<?php
function bgcolor($field, $value){
  if($field == "sort"){
    if($value >= "2.99"){
    $bgcolor = "red";
    } else {
      if($value < "2.99" && $value > "1.5"){
      $bgcolor = "orange";
      } else {
      $bgcolor = "green";
      }
    }
  } else {
  $bgcolor = "";
  }
return $bgcolor;
}
?>

 

Now call it in your loop

<?php
print "<table width=\"800\">";
print "<tr>";
for($i = 0;$i < mysql_num_fields($data);$i++)
    {
    $fieldname =  mysql_field_name($data, $i);
    print "<th>" .$fieldname . "</th>";
    }
print "</tr>";
for ($i = 0; $i < mysql_num_rows($data); $i++)
    {
    print "<tr>";
    $row = mysql_fetch_row($data);
        for($j = 0;$j < mysql_num_fields($data);$j++)
            {
            $fieldname =  mysql_field_name($data, $j);
                echo("<td bgcolor=\"".bgcolor($fieldname, $row[$j])."\">" . $row[$j] . "</td>");

            }
    print "</tr>";
    }
print "</table>";
?>

 

Now you can add other fields in the function if you want other rows to be color coded. I used bgcolor just for test but you can apply the value to whatever you want.

 

Ray

Link to comment
Share on other sites

Hi Craygo,

 

Something came up...  What if I wanted to target 2 or more field names, each having a different color code?  How could I do that? I tried doing... 

 

<?php
function bgcolor($field, $value){
  if($field == "sort1"){
    if($value >= "2.99"){
    $bgcolor = "red";
    } else {
      if($value < "2.99" && $value > "1.5"){
      $bgcolor = "orange";
      } else {
      $bgcolor = "green";
      }
    }
  } else {
  $bgcolor = "";
  }
return $bgcolor;
  if($field == "sort2"){
    if($value >= "90"){
    $bgcolor = "green";
    } else {
      if($value < "80" && $value > "70"){
      $bgcolor = "orange";
      } else {
      $bgcolor = "red";
      }
    }
  } else {
  $bgcolor = "";
  }
return $bgcolor;
}
?>

 

This didn't work... It only color codes the "sort1".. Can I have your input on this?

 

Please and thanks in advance.

Link to comment
Share on other sites

What happened is you are returning the bgcolor right after the first if statement. Since you want to look for one field at a time you have to chain the if/then's. Might be better to use a switch instead

<?php
function bgcolor($field, $value){
  switch ($field){
  case "sort1":
  if($value >= "2.99"){
  $bgcolor = "red";
  } else {
    if($value < "2.99" && $value > "1.5"){
    $bgcolor = "orange";
    } else {
    $bgcolor = "green";
    }
  }
  break;
  case "sort2":
  if($value >= "90"){
  $bgcolor = "green";
  } else {
    if($value < "80" && $value > "70"){
    $bgcolor = "orange";
    } else {
    $bgcolor = "red";
    }
  }
  break;
  default:
  $bgcolor = "";
  }
return $bgcolor;
}
?>

 

Ray

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.