Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
<cfif currentrow mod 2 is 0 and currentrow lt recordcount></tr><tr></cfif>
Your job is figure out where to put it.
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
<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>
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Better yet, don't check my suggestion unless this is an academic exercise. BKBK gave a better answer.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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>