7 Replies Latest reply: Dec 8, 2011 4:13 AM by S_Divya RSS

    need help with display table in cfoutput query

    cfnew Community Member

      Hi,

       

      i need to do this form of outputting cfoutput query.

       

      <cfquery name="myquery" datasource="mydsn">

      select email,phone from mytable

      </cfquery>

       

      <table>

      <tr>

       

      <cfoutput query="myquery">

      <td>

      #myquery.email#<br/>

      #myquery.phone#<br/>

      </td

      </cfoutput>

      </tr>

      </table>

       

       

      but i need output like this like for every 2 records i need new row.

       

      Example.

       

      Now getting my outpout like this

       

      1   2   3   4  5   6  7

       

      i want my output like this

       

      1   2

      3   4

      5   6

      7

       

      Please help me

       

      Thanks.

        • 1. Re: need help with display table in cfoutput query
          Dan Bracuk Community Member

          <cfif currentrow mod 2 is 0 and currentrow lt recordcount></tr><tr></cfif>

           

          Your job is figure out where to put it.

          • 2. Re: need help with display table in cfoutput query
            BKBK CommunityMVP

            Do you really want to display the email and phone of 2 different users on the same row? That doesn't make sense to me. Then again, do you mean, by 2 records per row, that you wish to display the email and phone per user per row? If so, then you require something like this

             

            <table>

            <cfoutput query="myquery">

            <tr>

            <td>

            #myquery.email#

            </td>

            <td>

            #myquery.phone#

            </td>

            </tr>

            </cfoutput>

            </table>

            • 3. Re: need help with display table in cfoutput query
              cp_anil@rediff

              <cfquery name="myquery" datasource="mydsn">

              select email,phone from mytable

              </cfquery>

               

              <table>

              <tr>

               

              <cfoutput query="myquery">

              <td>

              #myquery.email#<br/>

              #myquery.phone#<br/>

              </td>

              <cfif currentrow mod 2 eq 0 or currentrow eq recordcount>

                   </tr>

              </cfif>

              </cfoutput>

              </table>

              • 4. Re: need help with display table in cfoutput query
                BKBK CommunityMVP

                cp_anil@rediff wrote:

                 

                <cfquery name="myquery" datasource="mydsn">

                select email,phone from mytable

                </cfquery>

                 

                <table>

                <tr>

                 

                <cfoutput query="myquery">

                <td>

                #myquery.email#<br/>

                #myquery.phone#<br/>

                </td>

                <cfif currentrow mod 2 eq 0 or currentrow eq recordcount>

                     </tr>

                </cfif>

                </cfoutput>

                </table>

                Not quite. Check Dan's suggestion once again.

                • 5. Re: need help with display table in cfoutput query
                  Dan Bracuk Community Member

                  Better yet, don't check my suggestion unless this is an academic exercise.  BKBK gave a better answer.

                  • 6. Re: need help with display table in cfoutput query
                    Michael G. Workman Community Member

                    Sometimes its tricky to get the kind of output you want when you are populating a table with the results of a query, during web application development.

                     

                    After every two records you want to close the row with </TR> tag, and create a new row in the table using the <TR> tag, which stands for TABLE ROW. Using the MOD function is probably the best way to do this. If the currentrow is divided by 2 with no remainder, then you have to create a new row of data. the equivalent in ColdFusion is <cfif (CurrentRow MOD 2) eq 0>

                     

                    Here is an example of the code you would need:

                     

                    <table>

                    <tr>

                    <cfoutput query="myquery">

                    <td>

                    #myquery.email#<br/>

                    #myquery.phone#<br/>

                    </td>

                    <!--- if we have outputed two cells of data, close the row and start a new row unless we are at the end of the data --->

                    <cfif ((currentrow mod 2) eq 0) or (CurrentRow eq RecordCount)>

                         </tr>

                         <cfif CurrentRow lt RecordCount> <!--- if we are not at the end of the data, start a new row in the table for data --->

                            <tr>

                         </cfif>

                    </cfif>

                    </cfoutput>

                    </table>

                     

                    You have to visualize how the data will be outputed. Hope this helps.

                     

                    Michael G. Workman

                    mworkman@usbid.com

                    http://www.usbid.com

                    http://ic.locate-ic.com

                    • 7. Re: need help with display table in cfoutput query
                      S_Divya

                      Please try this one

                       

                      <cfscript>

                          row=1;

                      </cfscript>

                      <table>

                         

                          <cfoutput query="myquery" >

                             

                             

                                  <cfif row mod 2 eq 1>

                                      <tr>

                                      <td>#row#</td><td>#myquery.email#</td><td>#myquery.phone#</td>

                                  </cfif>

                                  <cfif row mod 2 eq 0>

                                      <td>#row#</td><td>#myquery.email#</td><td>#myquery.phone#</td>

                                      </tr>

                                  </cfif>

                                  <cfset row=row+1>

                             

                             

                          </cfoutput>

                      </table>