i have a jsp page, in this page i am displaying data retrieved from mysql database. Now the problem is i must color table cell based on cell value. how to implement this.please give me an sample or working code foe this problem.( any code is accepted) preferably html or javascript.
if cell value is "late" then display that cell in red color
display the cell in green color
please help me.

Thanks ad Regards

Set the color property of text for the given td based on the value fetched from the tabe.

if value is 'late'
  <td style="color: red;">late</td>
  <td style="color: green;">on time<td>

You can use normal conditional statements to do this work though it would be better if you use JSTL tags to do conditional branching.


thanks for the reply. i tried this but i am not able to achieve what i need. if u have any other alternate code please forward me.

Thanks and Regards

I can churn out alternatives only if you provide me with the code you have so far. What I presented in my previous post was a fairly easy thing to have done. If you are not able to achieve it, post the most recent code.


hello friends, i have problem here, please help me.
i have a jsp page in that jsp page i am displaying the data retrived from the database(mysql). i am displaying data in table format.
noe the problem is i want to color table cell based on cell value. that is if table cell consists value say "met" then i want to color that table cell in green color. like this for other cell.
first i want check the value and then select bgcolor for that cell accordingl .

i need working code for this . or sample code for checking condition in <td> tag.
this is the code i am using to display the data in html form
<table border="1" cellpadding="1" cellspacing="2">
<tr bgcolor="lightblue">
<th>Type </th>
<th WIDTH=40>IdNUM</th>
<th WIDTH=%200>Title</th>
<th>Analysis Due</th>
Connection conn=DriverManager.getConnection("jdbc:odbc:mmt","root","admin");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(
"select Type,idnum,ProdNum,SolID,Title,DevManager,Owner,Severity,Status,AnalysisDue,Agoal,Pgoal,Igoal,Fgoal from wmr where ( SolInDev > 5 AND AnalysisDone=' ' ) AND (IsExternal='y')");
try {
String bgColor = "silver";
while (rs.next()) {bgColor = bgColor.equals("silver") ? "white" : "silver";%>
<tr bgcolor="<%=bgColor%>">
<td align="center"><%= rs.getString("Type") %></td>
<td ><%= rs.getString("Idnum") %></td>
<td align="right" ><%= rs.getString("ProdNum") %></td>
<td align="right" ><%= rs.getString("SolId") %></td>
<td align="center"><%= rs.getString("Title") %></td>
<td align="center"><%= rs.getString("DevManager") %></td>
<td align="center"><%= rs.getString("Owner") %></td>
<td align="center"><%= rs.getString("Severity") %></td>
<td align="center"><%= rs.getString("status") %></td>
<td align="center"><%= rs.getString("AnalysisDue") %></td>
<td align="center"><%= rs.getString("Agoal") %></td>
<td align="center"><%= rs.getString("Pgoal") %></td>
<td align="center"><%= rs.getString("Igoal") %></td>
<td align="center"><%= rs.getString("Fgoal") %></td>

<% }
}catch (Exception e) {
out.println("Exception: " + e.getMessage());
} %>

if pgoal value = "met" then change that cell color to green. i want this.

i think now u can provide me some code

thanks and regards
[IMG]http://www.siteownersforums.com/images/statusicon/user_online.gif[/IMG] [IMG]http://www.siteownersforums.com/images/buttons/report.gif[/IMG] [IMG]http://www.siteownersforums.com/images/misc/progress.gif[/IMG]

  String pGoal = rs.getString("pGoal");
    out.println("<td style='background-color: green;'>" + pGoal + "</td>")
   out.println("<td style='background-color: red;'>" + pGoal + "</td>")

I would not recommend scriptlets but I don't think you care about it though...

Why not create a stylesheet class for each color?

.bred {background-color: red;}
.bgrn {background-color: green;}

Then just insert the correct classes when you put the values in the tables, either directly or through a script:

<td class="bgrn">met</td>

Thanks for your effort and spending time for me. I will try this now.

Thanks and Regards

Thanks for the reply,
the problem is here i am not inserting any values to the table. i am retreiving data from the database and displaying.

so i have to check what that cell conmtains .if that cell contains "met" then i want change the bgcolor of that cell to green. like that

can u help me now
Thnaks and Regards

i tried that java scrit but i am geetting some other problem. that is nothing is displayed in that coloumn 0r cell .
if possible please embed that javascript in my code.
if u give me ur mail id i will post some report in which i need.
Thanks and Regards

I have already answered your question in post #6. If you still can't figure that out, try reading some good JSP and Servlet tutorials till you have a good grasp of the subject matter.