Why my cfloop stop after inserting first id?

2019-02-25 22:15发布

问题:

I created cfloop that goes through query that I created above the loop. Inside of the loop I have another loop that defines values and then cfquery with insert statement. I tested my loops before I tried to insert my records in database and everything looked good. After I tried to apply that to insert my records my insert statement terminates after inserting first records from my . Here is example of my code:

<cfloop query="qryRecords">
    <cfloop condition="TimeStart LTE meetingLength">
        <cfset TimeEnd = dateAdd("n", arguments.meeting, TimeStart)>
        <cfquery name="addRecords" datasource="test">
            Insert Into(Date,Name,Location)
            Values(<cfqueryparam cfsqltype="cf_sql_date" value="#arguments.date#">,
                    <cfqueryparam cfsqltype="cf_sql_char" value="#Name#">,
                    <cfqueryparam cfsqltype="cf_sql_time" value="#Location#">);
        </cfquery>
        <cfset TimeStart = dateAdd("n", arguments.meeting, TimeStart)>          
    </cfloop>
</cfloop>

In my qryRecords I have 40+ records and I should insert multiple time records. My insert do that just for the first record and stop. Like I mentioned above I tried to output that on the screen with this code:

<cfloop query="qryRecords">
    <cfloop from="#test.Stime#" to="#test.Etime#" index="i" step="#CreateTimeSpan(0,0,test.meetingLeng,0)#">
                <cfset TimeEnd = dateAdd("n", test.meetingLeng, i)>
                    <tr>
                        <td>(#ID#) #timeFormat(TimeStart, "hh:mm tt")# - #timeFormat(TimeEnd, "hh:mm tt")#</td>
                    </tr>
                <cfset TimeStart = dateAdd("n", test.meetingLeng, i)>           
            </cfloop>       
</cfloop>

My output after I tested this code looks like this:

    (3) 08:30 AM - 08:40 AM
    (3) 08:40 AM - 08:50 AM
    (3) 08:50 AM - 09:00 AM
    (3) 09:00 AM - 09:10 AM
    (3) 09:10 AM - 09:20 AM
    (3) 09:20 AM - 09:30 AM
    (3) 09:30 AM - 09:40 AM
   *(12) 09:40 AM - 08:40 AM
    (12) 08:40 AM - 08:50 AM
    (12) 08:50 AM - 09:00 AM
    (12) 09:00 AM - 09:10 AM
    (12) 09:10 AM - 09:20 AM
    (12) 09:20 AM - 09:30 AM
    (12) 09:30 AM - 09:40 AM
   *(23) 09:40 AM - 08:40 AM
    (23) 08:40 AM - 08:50 AM
    .......... so on.

I can't see anything wrong with my Insert query and why that stops after inserting just one sequence of records in db. If anyone can see where I'm making mistake in my code please let me know.

回答1:

If qryRecords doesn't contain a TimeStart column, then TimeStart needs to be reset between the 2 opening cfloop tags (btwn <cfloop query="qryRecords"> and <cfloop condition="TimeStart LTE meetingLength">). Also, the cfloop condition should be comparing TimeStart w/ the last slot's start time (not the meetingLength minutes).