Ternary Operation

Hi Experts,

                 I need some help with a ternary conditional statement. I have an HTML table.

                 Right now whenever the data in column 4 is zero, I show Column 3 as N/A.

                 what I want now is,
                 keeping the same condition as it is (N/A when column 4 is zero), whenever the value of column three is more than 0, color the text as red.
                 My current code is below:


         
<table align="left" border="1" cellspacing="1" width="100%">
<tr>
<th>Aspect</th>
<th align="center">Metrics in SLA</th>
<th align="center">Metrics out of SLA</th>
<th align="center">Total metrics</th>
<th align="center">SLA compliance</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td><a href="<%=(rs.getString(1))%>"><b><%=rs.getString(1)%></b></a></td>
<%
int totalMetrics = rs.getInt(4);
%>
<td class="tab_2Col"><%=totalMetrics == 0 ? "N/A" : "" + rs.getInt(2)%></td>
<td class="tab_3Col"><%=totalMetrics == 0 ? "N/A" : "" + rs.getInt(3)%></td>
<td class="tab_4Col"><%=totalMetrics == 0 ? "N/A" : "" + totalMetrics%></td>
<td class="tab_5Col"><%=totalMetrics == 0 ? "N/A" : "" + nv.format(rs.getFloat(5)) + "%"%></td>
</tr>
<%
met_in += rs.getInt(2); met_out += rs.getInt(3); met_tot += rs.getInt(4);
}
met_SLA = met_tot == 0 ? 0.0 : ((met_in / met_tot) * 100);
%>
 
<tr>
<td><b>Total</b></td>
<td class="tab_2Col"><%=nf.format(met_in)%></td>
<td class="tab_3Col"><%=nf.format(met_out)%></td>
<td class="tab_4Col"><%=nf.format(met_tot)%></td>
<td class="tab_5Col"><%=nv.format(met_SLA)%>%</td>
</tr>
<%
rs.close();
pstmt.close();
%>
</table>

Open in new window

LVL 10
aman0711Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gibu GeorgeChief Technology OfficerCommented:

<td class="tab_2Col" <%=(rs.getInt(2)>0 &&totalMetrics!=0)?"style="color:RED;":""%>  ><%=totalMetrics == 0 ? "N/A" : "" + rs.getInt(2)%></td>

Open in new window

0
aman0711Author Commented:
Hey Gibu,

                    Its giving me error on this peace of code:

                     "style="color:RED;":""%>
0
a_bCommented:

<td class="tab_2Col" <%=(rs.getInt(2)>0 &&totalMetrics!=0)?"style=/"color:RED;/":"%>  ><%=totalMetrics == 0 ? "N/A" : "" + rs.getInt(2)%></td>

You need escape sequences
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

aman0711Author Commented:
Hi a_b,
               I am getting the same error with your code as well:

               "style=/"color:RED;/":"

               Multiple annotations found at this line.
0
Gibu GeorgeChief Technology OfficerCommented:


<td class="tab_2Col" <%=(rs.getInt(2)>0 &&totalMetrics!=0)?"style=\"color:RED;\"":""%>  ><%=totalMetrics == 0 ? "N/A" : "" + rs.getInt(2)%></td>

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aman0711Author Commented:
Thanks Gibu, a_b :-)
0
a_bCommented:
Whoops, I got the direction of the slash incorrect. gibu_george has the correct direction of the slash.
0
aman0711Author Commented:
Code to be replaced in the question:
<table align="left" border="1" cellspacing="1" width="100%">
<tr>
<th>Asp</th>
<th align="center">Met</th>
<th align="center">Metr</th>
<th align="center">Tot</th>
<th align="center">com</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td><a href="<%=(rs.getString(1))%>"><b><%=rs.getString(1)%></b></a></td>
<%
int totalMetrics = rs.getInt(4);
%>
<td class="tab_2Col"><%=totalMetrics == 0 ? "N/A" : "" + rs.getInt(2)%></td>
<td class="tab_3Col"><%=totalMetrics == 0 ? "N/A" : "" + rs.getInt(3)%></td>
<td class="tab_4Col"><%=totalMetrics == 0 ? "N/A" : "" + totalMetrics%></td>
<td class="tab_5Col"><%=totalMetrics == 0 ? "N/A" : "" + nv.format(rs.getFloat(5)) + "%"%></td>
</tr>
<%
met_in += rs.getInt(2); met_out += rs.getInt(3); met_tot += rs.getInt(4);
}
met_SLA = met_tot == 0 ? 0.0 : ((met_in / met_tot) * 100);
%>
 
<tr>
<td><b>Tot</b></td>
<td class="tab_2Col"><%=nf.format(met_in)%></td>
<td class="tab_3Col"><%=nf.format(met_out)%></td>
<td class="tab_4Col"><%=nf.format(met_tot)%></td>
<td class="tab_5Col"><%=nv.format(met_SLA)%>%</td>
</tr>
<%
rs.close();
pstmt.close();
%>
</table>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.