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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 (-;

 

Link to comment
Share on other sites

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 />

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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