CFML Struttura CFLOOP in CFSCRIPT
Alcuni esempi per scrivere o per convertire i cicli Loop (definiti con il tag CFLOOP) in linguaggio CFSCRIPT. Codice sicuramente più performante ma anche più adatto nell'utilizzo i componenti cfc. Di seguito si riportano esempi per Loop di tipo indice, su Collection, Loop di Lista, Loop su dataset proveniente da Query su database
<!--- Index Loop --->
<cfscript>
writeOutput('<h3>Loop Indice</h3><br />');
for(i=0; i<10; i++) {
writeOutput("Indice: #i# <br /> ");
}
</cfscript>
<!--- Collection Loop --->
<cfscript>
testStru = structNew();
testStru.firstname = "Contech";
testStru.lastname = "Lab";
writeOutput('<h3>Collection Loop</h3><br />');
for(i in testStru) {
writeOutput("#i#[<b>#testStru[i]#</b>]<br>");
}
</cfscript>
<!--- List Loop --->
<cfscript>
TestList = 'Contech,Lab,Software,Management';
writeOutput('<h3>List Loop</h3><br />');
for(i=1;i<=ListLen(TestList);i++){
value = ListGetAt(TestList,i);
writeOutPut('#value#<br />');
}
</cfscript>
<!--- Query Loop --->
<cfquery name="qArt" datasource="cfartgallery">SELECT * FROM ArtistsWHERE 1 = 1</cfquery>
<cfscript>
writeOutput('<h3>Query Loop</h3><br />');
for(i=1;i<=qArt.recordCount;i++){
writeOutput('<b>ArtistId</b> : '& qArt[ 'ArtistId' ][ i ]& ' <b>FirstName</b> : ' & qArt.Firstname[i]& ' <b>LastName</b> : ' & qArt['Lastname'][i]& '<br />');
}
</cfscript>
<cfscript>
writeOutput('<h3>Loop Indice</h3><br />');
for(i=0; i<10; i++) {
writeOutput("Indice: #i# <br /> ");
}
</cfscript>
<!--- Collection Loop --->
<cfscript>
testStru = structNew();
testStru.firstname = "Contech";
testStru.lastname = "Lab";
writeOutput('<h3>Collection Loop</h3><br />');
for(i in testStru) {
writeOutput("#i#[<b>#testStru[i]#</b>]<br>");
}
</cfscript>
<!--- List Loop --->
<cfscript>
TestList = 'Contech,Lab,Software,Management';
writeOutput('<h3>List Loop</h3><br />');
for(i=1;i<=ListLen(TestList);i++){
value = ListGetAt(TestList,i);
writeOutPut('#value#<br />');
}
</cfscript>
<!--- Query Loop --->
<cfquery name="qArt" datasource="cfartgallery">SELECT * FROM ArtistsWHERE 1 = 1</cfquery>
<cfscript>
writeOutput('<h3>Query Loop</h3><br />');
for(i=1;i<=qArt.recordCount;i++){
writeOutput('<b>ArtistId</b> : '& qArt[ 'ArtistId' ][ i ]& ' <b>FirstName</b> : ' & qArt.Firstname[i]& ' <b>LastName</b> : ' & qArt['Lastname'][i]& '<br />');
}
</cfscript>