Calendar

««Jan 2012»»
SMTWTFS
1234567
891011121314
15161718192021
22232425262728
293031

Alert Email

Get a short email alert whenever a new entry is published.

Confidential, secure it's piece of cake to keep uptodate.

Loop around queries in with do ... while

Published: 11:03 AM GMT, Thursday, 25 March 2010

Looping around a query object within a CFSCRIPT block can be a little cumbersome if you are using the standard for() method.

<cfscript>
qry = QueryNew( 'name' );
QueryAddrow( qry, 1 );
QuerySetCell( qry, 'name', 'Andy' );
QueryAddRow( qry, 1 );
QuerySetCell( qry, 'name', 'Alan' );

for ( x=1; x <= qry.recordcount; x++ ){
  writeoutput( qry.name[x] );
  //etc
}

</cfscript>

You have to do a lot more management of the loop using this method. With OpenBD you can reduce the complexity of this by using a do...while loop instead:

<cfscript>
qry = QueryNew( 'name' );
QueryAddrow( qry, 1 );
QuerySetCell( qry, 'name', 'Andy' );
QueryAddRow( qry, 1 );
QuerySetCell( qry, 'name', 'Alan' );

do{
  writeoutput( qry.name );
}while ( qry.nextRow() );

</cfscript>

As you can see this method is more akin to the standard CFOUTPUT/CFLOOP or running around a query.

Comments (0)

Add Comment