0

I'm working on a social networking site and I want to print the 5 most popular people I follow.The result set is returning 5 most popular people I follow from database. I want the first one to be printed in an h1 heading, the second one in h2 and so on etc. My question is if there is any way to change the heading during the iteration without printing each result in if clauses.

Bellow is my code

<% ResultSet rs1 = dao.returnTop5(email); 
int i = 1; 
while (rs1.next()){ 
  String username1 = rs1.getString("username"); 
  String profilephoto = rs1.getString("profilephoto"); 
  String followers = rs1.getString("followers"); %> 
  <img src="<%= profilephoto %>" class="first" title="<%= username1 %>">
  <b><big>&nbsp <%= username1 %></a>
      <h2 class="color">#<%= i %> (<%= followers %> Followers)</h2>
  </big></b> <br> <% i++; 
 } %>
Zahid Ali
  • 456
  • 6
  • 20
  • yes, but provide some code and details what you actually want? what you have tried? – Zahid Ali Jan 15 '15 at 03:43
  • 1
    <% ResultSet rs1 = dao.returnTop5(email); int i = 1; while (rs1.next()){ String username1 = rs1.getString("username"); String profilephoto = rs1.getString("profilephoto"); String followers = rs1.getString("followers"); %> &nbsp <%= username1 %>

    #<%= i %> (<%= followers %> Followers)


    <% i++; } %>
    – Konstantina Papagiannopoulou Jan 15 '15 at 12:00
  • there is only one loop do you mean in first iteration and 2nd iteration or first loop and 2nd loop? – Zahid Ali Jan 15 '15 at 12:38
  • In first iteration I want the h1 heading and in second iteration the h2 heading. Sorry for my mistake. – Konstantina Papagiannopoulou Jan 15 '15 at 12:43
  • check this answer out do you want something like this? other wise you can also edit your question to make it more clear – Zahid Ali Jan 15 '15 at 12:49

1 Answers1

0

i don't know what exactly do you want in heading but i hope this will help you out to write your program

<% ResultSet rs1 = dao.returnTop5(email); 
int i = 1; 
while (rs1.next()){ 
  String username1 = rs1.getString("username"); 
  String profilephoto = rs1.getString("profilephoto"); 
  String followers = rs1.getString("followers"); 
   %>
      <h<%=i%>><%= username1 %></h<%=i%>>
 <%
  i++; 
 } %>

bellow is the general program for you you can copy it and paste to the any jsp file to test bellow program

    <%
    for (int a=1; a<=5; a++){
     %>
        <h<%=a%>>Hello World!</h<%=a%>>
    <%
    }        
    %>
Altmish-E-Azam
  • 1,561
  • 1
  • 13
  • 24
Zahid Ali
  • 456
  • 6
  • 20
  • 1
    I'm working on a social networking site and I want to print the 5 most popular people I follow. But I want the first one to be printed in an h1 heading, the second one in an h2 etc. My question is if there is any way to change the heading during the iteration without printing each result in if clauses as in the code above. – Konstantina Papagiannopoulou Jan 15 '15 at 19:00
  • i have updated answer without if condition and have added some code to help you understand i hope this will help. – Zahid Ali Jan 16 '15 at 07:42
  • try to edit your question if there is something missing eg: code or there is any requirement that you have not written – Zahid Ali Jan 16 '15 at 07:47