Skip to main content
Known Participant
May 22, 2008
Answered

**repost**adding <cfloop> contents to a structure key

  • May 22, 2008
  • 1 reply
  • 303 views
Another question in regards to an earlier post with the same name that Mr. Bracuk helped me with. Relevant information again.
I am trying create a structure which contains all the dates of a particular event inside of its elements. A visual representation would look like: EventID:Day1:2008-02-01
Day2:2008-02-02
Day3:2008-02-03.
My query:
<CFQUERY datasource="#sDSN#" name="qEvents">
SELECT event_id,
event_startdate,
event_enddate
from hs_event
</CFQUERY>

My code:
<!--- 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.--->
<cfset ObjEvents = structNew()>
<CFOUTPUT>
<CFLOOP query="qEvents">
<CFSET ObjEvents[currentrow] = qEvents.event_id>

<cfset ThisDate = event_startdate[currentrow]>
<cfset nCounter = 1>
<CFLOOP condition="ThisDate LTE event_enddate[currentrow]">

<CFSET nCounter = nCounter + 1>
<CFSET ThisDate = DateAdd("d", 1, ThisDate)>

</CFLOOP>

</CFLOOP>
</CFOUTPUT>

Mr. Bracuk got me this far. But, I am not at all familiar with the CFSCRIPT tag, and only slightly familiar with the nature of structures <very, very new to programming>. I am assuming from my reading that I need to nest an EventsID structure inside the ObjEvents structure that I have created, and from there, elements that have named value pairs of Day1 = ActualDay. I truly appreciate your help! Dan
This topic has been closed for replies.
Correct answer Dan_Bracuk
This was my previous suggestion.
<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);
}


This line,
objEvents.#currentrow# = eventid[currentrow];
equates to this line,
<CFSET ObjEvents[currentrow] = qEvents.event_id>
and both are probably wrong. Mine might be wrong because it would start a variable name with a number, and yours has array notation for a structure instead of dot notation. Try this:
<CFSET ObjEvents.Row#currentrow# = qEvents.event_id[currentrow]>.
That should give you things like
ObjEvents.Row1=123
ObjEvents.Row2=456, etc

In your inner loop, where I suggested this:
objEvents.#currentrow#.date#ctr# = ThisDate;
you want
<cfset objEvent.Row#currentrow#.date#nCounter# = ThisDate>
That should give you things like
objEvent.Row1.date1 = 2008-01-01
objEvent.Row1.date2 = 2008-01-02
etc

Do it piecemeal and look at your results with <cfdump var="#objEvent#">

1 reply

Dan_BracukCorrect answer
Inspiring
May 22, 2008
This was my previous suggestion.
<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);
}


This line,
objEvents.#currentrow# = eventid[currentrow];
equates to this line,
<CFSET ObjEvents[currentrow] = qEvents.event_id>
and both are probably wrong. Mine might be wrong because it would start a variable name with a number, and yours has array notation for a structure instead of dot notation. Try this:
<CFSET ObjEvents.Row#currentrow# = qEvents.event_id[currentrow]>.
That should give you things like
ObjEvents.Row1=123
ObjEvents.Row2=456, etc

In your inner loop, where I suggested this:
objEvents.#currentrow#.date#ctr# = ThisDate;
you want
<cfset objEvent.Row#currentrow#.date#nCounter# = ThisDate>
That should give you things like
objEvent.Row1.date1 = 2008-01-01
objEvent.Row1.date2 = 2008-01-02
etc

Do it piecemeal and look at your results with <cfdump var="#objEvent#">
Known Participant
May 23, 2008
Answer worked as explained. I can now access each date according to element name. Thank you for your suggestion. It helped me. dan