Skip to main content
Inspiring
December 9, 2008
Question

cfloop question

  • December 9, 2008
  • 4 replies
  • 585 views
I have the following cfloop statement :

<cfloop index="x" from="1" to="10">

Everytime it loops, a line item number is generated ( line item + 1) and inserted into the table, up to a max of 10.

Lets say I stop at 3 line items. The next time I go back in, I want the loop to start from 4 to 10. I tried to query the table to find the max and add 1, then tried to plug that value in but it does not work, I get a javascript error (there is some javascript involved with the loop).

How would I get the loop to start from 4 instead of 1 ?
    This topic has been closed for replies.

    4 replies

    December 10, 2008
    Try something like this:

    <cfset session.j = 1>
    <cfloop from="1" to="10">
    <tr id="row_#session.j# <cfif session.j GT 1>style=display: none;"</cfif>>
    <td class="titleText" align="center">#session.j#<input type="hidden" name="x_#session.j# id="x" value="#session.j#"></td>
    <td class="TitleText" align="center"><select name=conditionCode_#session.j#>
    ......more stuff here.....
    <cfset session.j = session.j + 1>
    </cfloop>
    Inspiring
    December 9, 2008
    Maybe session.j is greater than 10. Start dumping your variables so you can see what is going on.
    December 9, 2008
    Try looping from 1 to however many times you need lines at this point (eg. you mentioned that you stopped the loop after 3 times) and reference the session.j variable inside your loop.
    trojnfnAuthor
    Inspiring
    December 9, 2008
    I used cfoutput to display the variables and max line item is 3 and session.j is 4, which is what I want.

    <cfset session.j=#qryGetLineItem.maxLineItem# + 1>
    <cfoutput>max lines is #qryGetLineItem.maxLineItem# and session is #x#</cfoutput>

    This part of the original loop statment. So instead of starting from=1, I want to start from=#session.j#, which in this case is 4, since I left off at 3 last time.

    <cfloop index="x" from="1" to="10">
    <tr id="row_#x#" <cfif x GT 1>style="display:none"</cfif> >
    <td class="TitleText" align="center"><cfoutput>#x#</cfoutput><input type="hidden" name="x_#x#" id="x" value="#x#"></td>
    <td class="TitleText" align="center"><select name="conditionCode_#x#">

    How can I modify the above loop to use the variables ? I cannot get it to work.
    December 9, 2008
    Try using a session variable to generate your line item number... then you can call the loop any time and always get the next number in line.
    trojnfnAuthor
    Inspiring
    December 9, 2008
    I query the table to get the max line item value :

    <cfquery name="#qryGetLineItem# datasource="dbName">
    select max(lineItem) as maxLineItem
    from table
    where docNo = '#url.docNo#'
    </cfquery>

    Then I setup the session variable j to add 1 to the maxlineitem :
    <cfset session.j=#qryGetLineItem.maxLineItem# + 1>

    I then insert j into the loop:

    <cfloop index="x" from="#session.j#" to="10">

    But nothing is hapening, the entire line is with the loop does not show up.