Skip to main content
Known Participant
May 21, 2008
Question

adding <cfloop> contents to a structure key

  • May 21, 2008
  • 1 reply
  • 255 views
I am trying to loop through the contents of a query and add the values created from the loop to a structure. Here is my query:
<CFQUERY datasource="#sDSN#" name="qEvents">
SELECT event_id,
event_startdate,
event_enddate
from hs_event

</CFQUERY>

Here is my the code related to my loop.

<!---Create an event structure structure to hold the dates of the events.--->
<CFSET objEvents = StructNew()>
<!--- Populate the event structure by running a loop that will go from the startdate listed in the event_startdate column and go to the hs_event_enddate column, incrementing by 1 for each "day" between these 2 dates.--->
<CFLOOP query="qEvents">
<CFSET intDateFrom = Fix(qEvents.event_startdate)>
<CFSET intDateTo = Fix(qEvents.event_enddate)>
<CFLOOP index="intDate" from="#Max(ThisMonthYear, intDateFrom)#" to="#Min(Days, intDateTo)#" step="1">
<CFSET objEvents[intDate] = qEvents.event_id>
</CFLOOP>
</CFLOOP>
<cfoutput><cfdump var="#objevents#"></cfoutput>
When I run this, it does not throw an error. However, it also does not fill the structure with the event_id values. The structure remains empty. What am I doing wrong? Thanks so much in advance! Dan
This topic has been closed for replies.

1 reply

Inspiring
May 21, 2008
If I understand your post correctly, you are looking to build a structure that contains the event id, and all the dates for that event. I'd do it something like this:

<cfloop query = "qEvents">
<cfscript>
objEvents.#currentrow# = eventid[currentrow];
ThisDate = event_startdate[current_row];
ctr = 1;
while (ThisDate lte event_enddate[currentrow]) {
objEvents.#currentrow#.date#ctr# = ThisDate;
ctr = ctr + 1;
ThisDate = DateAdd("d", 1, ThisDate);
}
closing tags