0

I need help trying to figure out how to set my 2nd condition ($column == 'Status) in this foreach loop as it is not using my color_array.

I created an array color_array for setting values to a particular color:

$color_array = array(
        'Succeeded'       => 'blue',
        'Failed'          => 'red',
        'Review Logs'     => 'yellow'
);

I want my column Status to be color coded. My foreach loop here creates my table:

$keys = array('Server', 'Target','Set','Time', 'Length','Size','Status');
echo '<table id="stats_1"><tr>';
foreach ($keys as $column) {
   echo '<th>' . $column . '</th>';
}
echo '</tr>';

foreach ($data as $row){
  foreach ($keys as $column){
     if (isset($row[$column])){
         if ($column == 'Server'){
            echo '<td> <a href="' . $server_array[$row[$column]] . '">' . $row[$column] . '</a></td>';
         } else {
            echo '<td>' . $row[$column] . '</td>';
         }
         if ($column == 'Status'){  //2nd condition here
            echo '<td> <font color="' . $color_array[$row[$column]] . '">' . $row[$column] . '</font></td>';
         } else {
            echo '<td>' . $row[$column] . '</td>';
         }
     } elseif ($column == 'Length') {
         echo '<td> n/a </td>';
     } elseif ($column == 'Size') {
         echo '<td> n/a </td>';
     } else {
         echo '<td> </td>';
     }
  }
}
echo '</table>';

The first case ($column == 'Server') works fine, but after adding the 2nd case, i would think it would work the same way? but its not...Somehow my logic is wrong. How do I get the 2nd case to work? Thanks.

1
  • Why are you looping through a hard coded array when it would be much simpler to just print out the columns and HTML directly? e.g., echo "<td>{$column['length']}</td><td>{$column['size']}</td>";, etc. Commented Sep 17, 2010 at 0:38

2 Answers 2

2

Should be something like this i think:

if ($column == 'Server'){
    echo '<td> <a href="' . $server_array[$row[$column]] . '">' . $row[$column] . '</a></td>';
}elseif ($column == 'Status'){  //2nd condition here
    echo '<td> <font color="' . $color_array[$row[$column]] . '">' . $row[$column] . '</font></td>';
} else {
    echo '<td>' . $row[$column] . '</td>';
}
Sign up to request clarification or add additional context in comments.

Comments

1

Did you mean to say this?

if (isset($row[$column])){
    if ($column == 'Server'){
        echo '<td> <a href="' . $server_array[$row[$column]] . '">' . $row[$column] . '</a></td>';
    } elseif ($column == 'Status'){  //2nd condition here
        echo '<td> <font color="' . $color_array[$row[$column]] . '">' . $row[$column] . '</font></td>';
    } else {
        echo '<td>' . $row[$column] . '</td>';
    }
 } elseif ($column == 'Length') {
     echo '<td> n/a </td>';
 } elseif ($column == 'Size') {
     echo '<td> n/a </td>';
 } else {
     echo '<td> </td>';
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.