sorry if this seems silly, i am trying to learn php and mysql

i have tried a few times and all i do is crash the code.
What is am trying to do is change the cell background colour to
green if echo $row['s1']; is "2" and red if "3"
I can then add this for all the other items for s2, s3 s4 etc

this is for some monitoring software at home.
green means all okay, whereas red will stand out at a glance
Thanks in advance

echo "<h1>Output data list</h1>";



echo "<table border='1'>";

echo "<tr> <th>Module</th> <th>Serial</th> <th>Location</th> <th>s1</th> <th>s2</th> <th>s3</th> <th>s4</th> <th>s5</th> <th>s6</th> <th>s7</th> <th>s8</th> <th>s1_name</th> <th>s2_name</th> <th>s3_name</th> <th>s4_name</th> <th>s5_name</th> <th>s6_name</th> <th>s7_name</th> <th>s8_name</th></tr>";

// keeps getting the next row until there are no more to get

while($row = mysql_fetch_array( $result )) {

    // Print out the contents of each row into a table

    echo "<tr><td>"; 

    echo $row['module'];

    echo "</td><td>"; 

    echo $row['serial'];

    echo "</td><td>"; 

    echo $row['location'];

    echo "</td><td>";

    echo $row['s1'];

    echo "</td><td>"; 

    echo $row['s2'];

    echo "</td><td>";  

    echo $row['s3'];

    echo "</td><td>";  

    echo $row['s4'];

    echo "</td><td>"; 

    echo $row['s5'];

    echo "</td><td>"; 

    echo $row['s6'];

    echo "</td><td>"; 

    echo $row['s7'];

    echo "</td><td>";  

    echo $row['s8'];

    echo "</td><td>"; 

    echo $row['s1_name'];

    echo "</td><td>"; 

    echo $row['s2_name'];

    echo "</td><td>";

    echo $row['s3_name'];

    echo "</td><td>"; 

    echo $row['s4_name'];

    echo "</td><td>"; 

    echo $row['s5_name'];

    echo "</td><td>"; 

    echo $row['s6_name'];

    echo "</td><td>"; 

    echo $row['s7_name'];

    echo "</td><td>"; 

    echo $row['s8_name'];

    echo "</td></tr>"; 



} 

echo "</table>";

?>
Member Avatar
diafol

EEK. Use code tags

if($val == 2){
  $class= ' class="green"';
}elseif($val == 3){
  $class= ' class="red"';
}else{
  $class="";
}

then

echo "</td><td{$class}>";

You'd need to change the way you echo the data/table though. Use a loop, so you don't duplicate code (values 1-8).

$build="";
for($x=1;$x<9;$x++){
  ...conditional...
  $build .= "<td{$class}>" . $row['s{$x}'] . "</td>";
} 
echo $build;

THanks ardav For your valuable time.
I will work out your answer in more detail.
My example is based on "tutorials". I guess i am learning bad habits.
Regards
Terry