I have code , and I need to change color row when in last column 'status' appears value = 'error'

<?php
//first, get the file...
$file = file('log.txt');

//now count the lines ..
$lines = count($file);

//start the table here..
echo '</br>';
echo'<table border="1" width="600">';
echo '    <td width="400">Date</td>';
echo '    <td width="300">Name</td>';
echo '    <td width="400">Status</td>';

//start the loop to get all lines in the table..
for ($i=0; $i<=$lines; $i++) {

//get each line and exlplode it..
  $part = explode(';', $file[$i]);
//now start printing ..

echo'<tr>

              <td width="400">'.$part[0].'</td>
              <td width="300">'.$part[1].'</td>
              <td width="400">'.$part[2].'</td>
         </tr>';


}


//close the table so HTML wont suffer :P
echo'</table>'; 
?>

I change this :

If ($part[2] == 'error') 
{
echo '<td width="400" bgcolor="red">'.$part[2].'</td>';
}
else
{
echo '<td width="400" bgcolor="green">'.$part[2].'</td>;
 }

but this did not work - no color change in row
someone has an idea how to improve?

Recommended Answers

All 3 Replies

If you don't mind being non-compatible with older browsers you could use css3's nth-child.

A quick example of this:

.lol:nth-child(even){
    background-color:#FFF;
}
.lol:nth-child(odd){
    background-color:#000;
}

Use CSS to set a background colour to rows with errors:

tr.error {
  backround-color: red;
}

Then check if there is an error and if so, apply the style to your table row (replaces line 22 in your posted code):

echo'<tr';

if ($part[2] == 'error') { echo ' class="error"'; }

echo '>';
commented: css is the way to go +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.