Skip to main content
Participating Frequently
July 29, 2010
Answered

Code Help: query based cfloop with startrow and endrow in cfscript

  • July 29, 2010
  • 1 reply
  • 2193 views

This is probably a pretty simple thing - but I'm trying to learn as much cfscript as possible by converting my cfcs to cfscript. I have found examples of for loops in cfscript based off a query, but I'm not sure where the startrow and endrow would fit in.

<cfloop query="qQuery" startrow="start#" endrow="#end#">

     <cfset var1=var>

     <cfset var2=var2>

</cfloop>

Thanks a bunch for any tips.

~ Ben

    This topic has been closed for replies.
    Correct answer TwoEdge

    You should be able to use a "for" loop with indexes and keys. Something like (where "start" and "end" are your values):

    <cfscript>

    // Loop over the query.

    for (intCurrentRow = start ;intCurrentRow LTE end;intCurrentRow = (intCurrentRow + 1))

    {

    //access the query as if it were a structure of arrays with the current row index:

    stcSongs.songID = qSongs["songID"][intCurrentRow];

    stcSongs.songName = qSongs["songName"][intCurrentRow];

    //etc...

    }

    </cfscript>

    Hope that helps.

    1 reply

    Inspiring
    July 29, 2010

    You should never write any code until you know what it is supposed to accomplish.  Your question does not make me think that you do.

    Participating Frequently
    July 29, 2010

    The actual cfml code is making an array from a looped structure for a json call for an extjs grid. Below is a larger extraction of code from the CFC. Hopefully that answers your question? Thank you for your initial reply.

    <cfset i = 1>

    <!--- Build Array --->

    <cfloop query="qSongs" startrow="#params.start#" endrow="#end#">

         <cfscript>

              stcSongs = StructNew();

              stcSongs.songID = qSongs.songID;

              stcSongs.songName = qSongs.songName;

              stcSongs.songArtist = qSongs.songArtist;

              stcSongs.songAlbum = qSongs.songAlbum;

              stcSongs.songGenre = qSongs.songGenre;

              stcSongs.songDuration = timeFormat(qSongs.songDuration, "H:mm:ss");

              stcSongs.songPlayCount = qSongs.songPlayCount;

              stcSongs.songRating = qSongs.songRating;

              stcSongs.songDateAdded = dateFormat(qSongs.songDateAdded, "short");

              stcSongs.songDateLastPlayed = dateFormat(qSongs.songDateLastPlayed, "short");

              stcSongs.songFileSize = (qSongs.songFileSize / 1000) & " MB";

              stcSongs.songBitRate = qSongs.songBitRate & " Kbps";

              stcSongs.songTrackNumber = qSongs.songTrackNumber;

              stcSongs.songReleaseYear = dateFormat(qSongs.songReleaseYear, "yyyy");

              stcSongs.songComposer = qSongs.songComposer;

              stcSongs.songConductor = qSongs.songConductor;

              arrSongs = stcSongs;

              i = i + 1;

         </cfscript>

    </cfloop>

    <!--- Final JSON Return --->

    <cfset renderText(serializejson({rows=arrSongs,dataset=#qSongs.RecordCount#}))>

    TwoEdgeCorrect answer
    Inspiring
    July 29, 2010

    You should be able to use a "for" loop with indexes and keys. Something like (where "start" and "end" are your values):

    <cfscript>

    // Loop over the query.

    for (intCurrentRow = start ;intCurrentRow LTE end;intCurrentRow = (intCurrentRow + 1))

    {

    //access the query as if it were a structure of arrays with the current row index:

    stcSongs.songID = qSongs["songID"][intCurrentRow];

    stcSongs.songName = qSongs["songName"][intCurrentRow];

    //etc...

    }

    </cfscript>

    Hope that helps.