Copy link to clipboard
Copied
I have created a form in which an instructor can request to have software installed onto the computers in the lab. After the instructor submits the information is compiled in CFMAIL and is sent to the department head.
Of the many many form fields, two are key. CourseNumber and SectionNumber
The output of these Fields are lists, for example
Course = COM1010, ART1200, MATH0600
Section1 = 102 169 147, 254 141, 122
Note: The section field uses a comma (,) as the delimiter. The spaces represent multiple section numbers of a single course.
Using the above example
COM1010
-Section 102
-Section 169
-Section 147
ART1200
-Section 254
-Section141
I'm having a problem making it output correctly. Below is the code I am using:
Form.Course = COM1010, ART1200, MATH0600
Form.Section1 = 102 169 147, 254 141, 122
<cfloop index="ListItem1" list="#form.Course#">
#ListItem1#<br /> <!---Lists One Course Number--->
<cfif isdefined ("Count")>
<cfset Count = #Count# + 1>
<cfelse>
<cfset Count = 1>
</cfif>
<cfloop index="ListSection" list="#form.Section1#" from=#Count#> <!---My Problem is Here, CF is not accepting the "From" var, which is the key to this code working correctly--->
<cfset MultiSection=Findnocase(" ","#ListSection#")> <!---Looks for a space ( ) delimiter--->
<cfif #MultiSection# gt 0>
<cfset NewList=#ListSection#>
<cfloop delimiters=" " index="ListSub" list="#NewList#"> <!---Breaks thelist into a smaller list using spaces and lists each value--->
-#ListSub#<br />
</cfloop>
<CFELSE> <!---If no space ( ) found than do this--->
-#ListSection#<br /> <!---Lists 2nd Loop--->
</cfif>
<cfbreak> <!---Makes it only display the sections with the correct course--->
<cfloop><!---End of the Form.Section Loop--->
</cfloop> <!---End of Form.Course Loop--->
For the life of me I can't figure out why this is happening. Without "From" the code works but prints the section numbers into every course, instead of the ones they are supposed to be in.
If you have any suggestions I would really appreciate the help.
<cfloop index="ListSection" list="#form.Section1#" from=#Count#> <!---My Problem is Here, CF is not accepting the "From" var, which is the key to this code working correctly--->
You can't loop over a list using the LIST attribute while using the FROM attribute. If you want to start at a specific point, but loop over a list, you'll have to do something like this:
<cfloop index="ListSection" from="#Count#" to="#ListLen(form.Section1)#"> ...
Of course, you probably have to have some way to access ea
...Copy link to clipboard
Copied
<cfloop index="ListSection" list="#form.Section1#" from=#Count#> <!---My Problem is Here, CF is not accepting the "From" var, which is the key to this code working correctly--->
You can't loop over a list using the LIST attribute while using the FROM attribute. If you want to start at a specific point, but loop over a list, you'll have to do something like this:
<cfloop index="ListSection" from="#Count#" to="#ListLen(form.Section1)#"> ...
Of course, you probably have to have some way to access each list element within the loop, I guess, so you might be better served by converting the list to an array and using that instead:
<cfset aSection1 = ListToArray(form.Section1)>
<cfloop index="ListSection" from="#Count#" to="#ArrayLen(aSection1)#">
... can refer to aSection1[ListSection] within the loop ...
</cfloop>
Dave Watts, CTO, Fig Leaf Software
Copy link to clipboard
Copied
Related lists are always a little tricky to work with - every so often you'll get an instance where your list counts between your course and section lists don't match. If you stick with your current logic, my advice FWIW is to make sure you validate your list lengths to ensure that both lists are the same length before processing.
Another suggestion, depending on how much you want to rework your code for maintainability is to combine your course and section data into a tokenized string:
e.g. instead of having 2 fields that results in lists, concatenate the course and section data into one element separated by a custom delimeter:
Course = COM1010, ART1200, MATH0600
Section1 = 102 169 147, 254 141, 122
becomes: couse_section=COM1010_102,COM1010_169,COM1010_147,ART1200_254,ART1200_141,MATH0600_122
Then when processing, you can retrieve each data element using ListFirst() and ListLast()
Of course, that all depends on how easy it is to modify your existing application.
Hope that helps,
- Michael
Copy link to clipboard
Copied
Dave,
That was excatly what I needed. Thank you.
Ami