3

Good afternoon. Currently I am formatting some json from a query and when I get to the point of looping out the query, I get a very odd phenomenon. I specify a startrow of 20 and a endrow of 40. When it runs, I get 19 empty arrays prior to it populating 20-40. How can I get the loop to only return rows 20-40?

<cfcontent reset="Yes" />
<cfset thedata = StructNew() >
<cfset thestruct = StructInsert(thedata, "sEcho", "#NumberFormat(val(url.sEcho),"_")#", 1)>  
<cfset thestruct = StructInsert(thedata, "iTotalRecords", "#NumberFormat(qCount.total,"_")#", 1)>  
<cfset thestruct = StructInsert(thedata, "iTotalDisplayRecords", "#NumberFormat(qFiltered.recordCount,"_")#", 1)>
<cfset thearray = ArrayNew(2)> 
<!--- Populate the array row by row --->

<cfloop query="qFiltered" startrow="20" endrow="40">
    <cfset thearray[CurrentRow][1]=req_name>
    <cfset thearray[CurrentRow][2]=req_departments_name>
    <cfset thearray[CurrentRow][3]=dest_departments_name>
    <cfset thearray[CurrentRow][4]=priority_rank_name>
    <cfset thearray[CurrentRow][5]=issue_subject>
    <cfset thearray[CurrentRow][6]=systems_sub_name>
    <cfset thearray[CurrentRow][7]=tickets_status_name>
    <cfset thearray[CurrentRow][8]=tickets_types_name>
    <cfset thearray[CurrentRow][9]=due_date>
    <cfset thearray[CurrentRow][10]=departments_sub_name>
</cfloop>
<cfset thestruct = StructInsert(thedata, "aaData", thearray, 1)>
<cfoutput>#serializeJSON(thedata)#</cfoutput>

which returns this JSON

{
"iTotalDisplayRecords":"202",
"iTotalRecords":"661",
"aaData":[
[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],
["Derek Wolf","","","02- Revenue","Assist in validating error in JCA provided Discount Commission report","","Received","Work Request","Jan 1, 2012",""],
["Katie Hintz","","","02- Revenue","Order\/Install new POS Terminal at Katie's Workstation","","In Progress","Work Request","Oct 31, 2011",""],
["Katie Hintz","","","02- Revenue","Replace #6081 POS Printer at Kim's Desk","","Received","Work Request","Oct 31, 2011",""],
["","","","02- Revenue","Produce Brought To You By...  Video","","In Progress","Work Request","Nov 9, 2011",""],
["","","","02- Revenue","Produce Dept. Spotlight Video","","In Progress","Work Request","Nov 9, 2011",""],
["","","","02- Revenue","Produce Matty T's Pancake Breakfast","","In Progress","Work Request","Nov 9, 2011",""],
["","","","02- Revenue","Produce Fish! Video","","In Progress","Work Request","Nov 9, 2011",""],
["Sextus Selvaratnam","","","02- Revenue","Larger coin hopper system","","In Progress","Work Request","",""],
["Charles Canfield","","","02- Revenue","Ride theme improvment Castle","","In Progress","Work Request","",""],
["Omid Aminifard","","","02- Revenue","Contact Alterface about Desperados","","In Progress","Work Request","",""],
["Dino Kypreos","","","03- Repair","Repair or Replace Radio","","Received","Work Request","",""],
["Josh Cohen","","","03- Repair","SQL Server maintenance and tuning","","In Progress","Work Request","",""],
["","","","03- Repair","Move Weather Station to new Ops area","","Approved","Work Request","Nov 30, 2011",""],
["","","","03- Repair","Weather Station Move","","Approved","Work Request","Nov 30, 2011",""],
["","","","03- Repair","Strip and clean Moving Light fixtures","","Waiting Approval","Work Request","Dec 31, 2011",""],
["","","","03- Repair","Strip and clean Source Four light Fixtures","","Waiting Approval","Work Request","Dec 31, 2011",""],["","","","03- Repair","Replace Bad LCDs on ColorDash Pars LED light","","Received","Work Request","Dec 31, 2011",""],
["Josh Cohen","","","03- Repair","OMRON: repair terminal 4 computer","","On hold by originator","Work Request","",""],
["Amy Walters","","","03- Repair","Radio\/pager maintenance","","Waiting on parts","Work Request","",""],
["Mark Hersey","","","03- Repair","Inspect Ride Speaker Mounts for Safety","","Received","Work Request","Sep 30, 2011",""],
["Brian Gustav","","","03- Repair","Radio\/pager maintenance","","In Progress","Work Request","",""]],
"sEcho":"1"
}

UPDATE

John was able to get an answer for me. Now that I have it working I would like to generate all of the CFSET tags from a list

<cfset listColumns = "tickets_id,departments_sub_name,aging,priority_rank_name,systems_sub_name,issue_subject,tickets_status_name,due_date" />

<cfloop list="#listColumns#" index="thisColumn" delimiters=",">
    <cfset rowarray[listColumnsCount]=#thisColumn#>
    <cfset listColumnsCount ++>
</cfloop>

Instead of getting the column value output, I am just getting the column name in the JSON

Brian
  • 622
  • 10
  • 29
  • As an aside, associative array notation is a bit easier on the eyes than `StructInsert`. ie `` – Leigh Oct 06 '11 at 20:40

1 Answers1

4

Your problem is that you're using CurrentRow as the first index into thearray. So, even though your cfloop correctly skips the first 19 rows, you're telling it to put the first one in the 20th position in the array, so you get 19 empties before that.

You can manually subtract 19 from the CurrentRow value, but you'd have to change the calculation every time you change the startrow.

The better option is to build the inner array separately, and just ArrayAppend that to thearray. This will work regardless of your startrow and endrow values.

<cfloop query="qFiltered" startrow="20" endrow="40">
    <cfset rowarray = ArrayNew(1)>
    <cfset rowarray[1]=req_name>
    <cfset rowarray[2]=req_departments_name>
    <cfset rowarray[3]=dest_departments_name>
    <cfset rowarray[4]=priority_rank_name>
    <cfset rowarray[5]=issue_subject>
    <cfset rowarray[6]=systems_sub_name>
    <cfset rowarray[7]=tickets_status_name>
    <cfset rowarray[8]=tickets_types_name>
    <cfset rowarray[9]=due_date>
    <cfset rowarray[10]=departments_sub_name>
    <cfset ArrayAppend(thearray, rowarray)>
</cfloop>
John Flatness
  • 32,469
  • 5
  • 79
  • 81
  • @Brian: See http://stackoverflow.com/questions/2581394/how-to-get-a-dynamic-attribute-name-in-cfloop-over-query-in-coldfusion – John Flatness Oct 06 '11 at 22:16