Skip to main content
Known Participant
November 20, 2011
Question

need help with display table in cfoutput query

  • November 20, 2011
  • 5 replies
  • 3069 views

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.

This topic has been closed for replies.

5 replies

Participant
December 8, 2011

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>

Participating Frequently
November 30, 2011

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

Participating Frequently
November 24, 2011

<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>

BKBK
Community Expert
Community Expert
November 24, 2011

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.

Inspiring
November 24, 2011

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

BKBK
Community Expert
Community Expert
November 21, 2011

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>

Inspiring
November 21, 2011

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

Your job is figure out where to put it.