7 Replies Latest reply: May 2, 2008 10:19 AM by 807601 RSS

    How do i start a new row after five items in a resultset

    807601
      Hi,

      I'm wanting to create an image gallery. Instead of listing one picture per row, i want to list 5 pictures in each row. so 5 columns in each row. How do i do this? I'm guessing its by using the for loop but i can't think of a logical way of doing it.

      Any help please? or any tutorials for me figure it out.

      Cheers
        • 1. Re: How do i start a new row after five items in a resultset
          807601
          Can you post the question in a bit more context..? Does each call of rs.next() return one image..?

          You could play around with nested loops.

          Something like:

          //While there are more results (also moves cursor to next line)

          while (rs.next()){

          //Takes the cursor back to the line the loop started on (so you don't miss the image) - remove this if you find you don't need it.
          rs.previous();


          //Format the start of the line here

          //Get the 5 images for the line
          for(int i = 1; i <= 5; i++){

          //If it's not the last row (to prevent exceptions related to the cursor going off the end of the rs)
          if(rs.isLast() == false){

          //Code to get the image here
          rs.next()

          }

          }

          //Format the end of the line here
          }

          It might work..? I'm not in a position to test the code, but it's something to think about / play around with... Just a thought! Oh yeah, for rs.previous() you need to set the rs to be scrollable. See the API for more information.

          Tom

          Edited by: Dietrich on May 2, 2008 4:18 AM
          • 2. Re: How do i start a new row after five items in a resultset
            807601
            so,
            i query the database (select pics from pictable where user='user')
            then do a while loop to retrieve the results...
            then do an if statement to pull first 5... if more than 5 then start new row...

            am i on right lines?
            • 4. Re: How do i start a new row after five items in a resultset
              807601
              new row of images from the results... its cool ive done it....

              thank you
              • 5. Re: How do i start a new row after five items in a resultset
                807601
                Share your answer :(
                • 6. Re: How do i start a new row after five items in a resultset
                  807601
                         <table width="100%" border="0" cellspacing="8" cellpadding="0">
                         <tr>
                         <%
                         int count = 0;
                         RS = Stmt.executeQuery("select id,image from images where user='"+login+"'");
                         while(zRS.next())
                         {
                           int imageid          = RS.getInt("id");
                            Blob pic           = RS.getBlob("image");          
                            count++;
                       
                            if (pic.length()>0)
                            {
                            out.print("<td><a href=''><img src='"+request.getContextPath()+"/szpic.jsp?table=images&id="+imageid+"&column=image' width='60' height='75' border='1' alt='Pic "+count+"'></a></td>");
                            }
                            else
                            {
                            out.print("<td>No photo on file</td>");
                            }
                            
                           out.print("<td class='standard'><a href=''>Edit</a><br/><a href=''>Delete</a></td>");
                             
                             
                             if(count==5){out.print("</tr><tr>");}
                             if(count==10){out.print("</tr><tr>");}
                             if(count==15){out.print("</tr><tr>");}
                         }
                         
                         %>   
                         </tr>   
                         </table>
                  Couldn't have been easier ;-)
                  The above is probably not the best way to go about it but i'm doing a draft working layout for the people to play with so it'll do for now.

                  My next task is to display the images at a smaller size without distorting them... any ideas?

                  Edited by: fahafiz on May 2, 2008 8:17 AM
                  • 7. Re: How do i start a new row after five items in a resultset
                    807601
                    so basically, you're closing the row and opening a new row after every 5 images... if it's less than 5 then the row closes itself...