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?

Dani AI

Generated

Short checklist for 's example and the suggestions from and :

A few likely causes why the row never changes color: the status field often contains trailing newlines or spaces from file() so == 'error' fails; the for-loop using <= will try to read one past the last line; and header cells were printed without a proper header row. Adding a class to the <tr> is the right approach, but it must be combined with trimming/sanitizing the parsed fields and a safer loop.

A compact, more robust pattern (read lines, trim fields, set a class, escape output):

$file = file('log.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
echo "<table border='1'>";
echo "<tr><th>Date</th><th>Name</th><th>Status</th></tr>";
foreach ($file as $line) {
  $parts = array_map('trim', explode(';', $line));
  if (count($parts) < 3) continue;
  $status = strtolower($parts[2]);
  $rowClass = ($status === 'error') ? 'row-error' : 'row-ok';
  echo "<tr class=\"$rowClass\">";
  echo "<td>" . htmlspecialchars($parts[0]) . "</td>";
  echo "<td>" . htmlspecialchars($parts[1]) . "</td>";
  echo "<td>" . htmlspecialchars($parts[2]) . "</td>";
  echo "</tr>";
}
echo "</table>";

Apply CSS that targets the cells so the whole row shows the color (this avoids some browser/override issues):

.row-error td { background: #f8d7da; }
.row-ok    td { background: #e9f7ef; }

Extra tips: use file() flags or fgetcsv() for more reliable parsing, always trim() the status before comparison (CRLF from Windows files is common), check the generated HTML in the browser inspector to confirm the class is present, fix any for ($i=0; $i<=$lines; $i++) off-by-one logic, and escape output with htmlspecialchars() when printing log content.

Recommended Answers

All 3 Replies

Member Avatar for Member #46692

maybe

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.