Jump to content

Formating a table row based on a cell value


rama1

Recommended Posts

how to format only one specific row based on the value of  one cell in a row?

 

$result = mysql_query(select bal,  etc. etc ..query )

 

while($row = mysql_fetch_assoc($result))

 

echo "<tr>";

 

echo "<td>" . $row['Account'] . "</td>";

 

echo "<td>" . $row['licence'] . "</td>";

 

echo "<td>" . $row['own'] . "</td>";

 

echo "<td>" . $row['bal'] . "</td>";

 

  echo "</tr>";

 

Would appreciate help on how to format only the $row['bal'] output to red colour IF bal >5000

 

Thanks

Have a css style class that styles the td the way you want.  I'm going to assume that the name of this class is ".red"

 


echo ($row['bal'] > 5000) ? '' : '';
echo "{$row['bal']}";

 

If the first line looks a bit cryptic it's using the ternary operator, which is the equivalent of an if-then-else statement.

 

I also included a little hint on how you can include an associative array element inside an interpolated string.  A lot easier to read and maintain than having to concatenate all over the place.

Hi gizmola, tried the 'css style class' route with the following:

 

<head>

 

 

<style type="text/css">

td {color: red; }

</style>

 

 

</head>

....

$result = mysql_query(select bal,  etc. etc ..query )

 

while($row = mysql_fetch_assoc($result))

 

 

echo "<tr>";

 

echo "<td>" . $row['Account'] . "</td>";

 

echo "<td>" . $row['licence'] . "</td>";

 

echo "<td>" . $row['own'] . "</td>";

 

echo ($row['bal'] > 50000) ? '<td class="red">' : '<td>';

echo "{$row['bal']}</td>";

//  echo "<td>" . $row['bal'] . "</td>";

 

  echo "</tr>";

 

  }

....

The problem is that all the entries are formatted in red. Any way I can target the css to the $row['bal'] based on a condition.

 

I will try the 'associative array element inside an interpolated string' later once I have figured out what it means (-;

 

This is not a php question at this point... it's become a css question.  You didn't do what I instructed.

 

<br />
td {color: red; }<br />

 

Is not creating a style class, it's saying "make all

elements red.

 

<br />
.red {<br />
  color: red;<br />
}<br />

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.