Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

cfloop question/help

Participant ,
Apr 07, 2008 Apr 07, 2008
I have the following cfloop in my action page. It is not working so I put in cfouput to see what values are returned.

<cfif isDefined("form.fieldnames") and mid(form.fieldnames,1,4) is "Del_">

<cfloop index="i" list="#form.fieldnames#" delimiters=",">
<cfoutput>loop is from i, list is #form.fieldnames#<br></cfoutput>
<cfif left(i,4) is "del_">
<cfset select_urdn_number = removechars(i,1,4)>
<cfset select_urdn_number = #evaluate(select_urdn_number)#>
<cfoutput>evaluate urdn nubmer is #select_urdn_number#<br></cfoutput>
</cfif>

Here is the output I am getting from the cfoutput :
loop is from i, list is DEL_2178,DEL_2921,REGION,SITE,ACTIVITY_TYPE
evaluate urdn nubmer is 2178
loop is from i, list is DEL_2178,DEL_2921,REGION,SITE,ACTIVITY_TYPE
evaluate urdn nubmer is 2921
loop is from i, list is DEL_2178,DEL_2921,REGION,SITE,ACTIVITY_TYPE
loop is from i, list is DEL_2178,DEL_2921,REGION,SITE,ACTIVITY_TYPE
loop is from i, list is DEL_2178,DEL_2921,REGION,SITE,ACTIVITY_TYPE


The evaluate urdn numbers are correct, I am suppose to get two records, but the last part repeats three times (why ?) and when I query again to display the records, it shows the first one, the the second four times :

URDN 2178 2178
URDN 2921 2921
URDN 2921 2921
URDN 2921 2921
URDN 2921 2921


Why is this happening and what do I need to do to only get the two records ?

Thanks
371
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 07, 2008 Apr 07, 2008
see how many fields you have in the form? see how many outputs you get?
see the connection?

your loop is wrong. you are looping over all fields in the form instead
of only over the ones that hold the data you need.

all your loop logic has to be within <cfif left(i,4) is "del_"></cfif>

also, removechars(i,1,4) can be easily replaced with right(i, len(i)-4)
or even better with listlast(i, "_")
and there's no need to evaluate() anything, i believe...

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 07, 2008 Apr 07, 2008
quote:

Originally posted by: Newsgroup User
and there's no need to evaluate() anything, i believe...

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/


Neither do I.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 07, 2008 Apr 07, 2008
I moved the loop inside the ciff and it works, it eliminates the duplicates, here is what I have :
<cfif isDefined("form.fieldnames") and mid(form.fieldnames,1,4) is "Del_">
<cfloop index="i" list="#form.fieldnames#" delimiters=",">
<cfset select_urdn_number = listlast(i,"_")>
<cfquery>
</cfloop>

Now here is something else that I am running into, which was working when the loop is outside cfif, but I get duplicates like before, so I put it insid like above, and now it does not work.

The forrm field is <input type="checkbox" name="del_#urdn_number#_#urdn_line_item#" value="Yes">
and the code that worked before, when it the loop was outside cfif is :
<cfif isDefined("form.FieldNames")>

<cfloop index="i" list="#form.fieldnames#" delimiters=",">

<cfif left(i,4) is "del_">
<cfset select_urdn_number = listgetat(i, 2, "_")>
<cfset select_urdn_line_item = listlast(i, "_")>

But like before, it was giving me duplicates in the query, so I moved the loop inside the cfif lke above

<cfif isDefined("form.fieldnames") and mid(form.fieldnames,1,4) is "Del_">
<cfloop index="i" list="#form.fieldnames#" delimiters=",">

<cfset select_urdn_number = listgetat(i, 2, "_")>
<cfset select_urdn_line_item = listlast(i, "_")>

and now I am getting this errro :

Invalid list index 2.
In function ListGetAt(list, index [, delimiters]), the value of index, 2, is not a valid as the first argument (this list has 1 elements). Valid indexes are in the range 1 through the number of elements in the list.


I do not understand what listgetat does, but that is the problem. What do I do to solve the problem ?

The original had one extract, the urdn number. Now this one has two extracts, the urdn number and the line item number.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 07, 2008 Apr 07, 2008
quote:


<cfif isDefined("form.fieldnames") and mid(form.fieldnames,1,4) is "Del_">
<cfloop index="i" list="#form.fieldnames#" delimiters=",">
<cfset select_urdn_number = listlast(i,"_")>
<cfquery>
</cfloop>



<cfquery>? where's the </cfquery> and the body of the tag?

furhter:
this line of yours does not make any sense:
<cfif isDefined("form.fieldnames") and mid(form.fieldnames,1,4) is "Del_">

form.fieldnames is a list of all submitted for fields. thus
mid(form.fieldnames,1,4) is looking for first 4 characters of the whole
list.

furthermore:
<cfloop index="i" list="#form.fieldnames#" delimiters=",">
<cfset select_urdn_number = listlast(i,"_")>

you are looping through all field names one by one here. you are not
setting any condition for the form field name like you do later in your
code, so the second line above looks at EVERY fieldname, even the ones
WITHOUT any _ in the name. obviously in such a case that line will throw
an error.

if i understand your requirements correctly, all you need is:

<cfif isDefined("form.fieldnames")>
<cfloop index="i" list="#form.fieldnames#" delimiters=",">
<cfif left(i,4) is "del_">
<cfset select_urdn_number = listgetat(i, 2, "_")>
<cfset select_urdn_line_item = listlast(i, "_")>
</cfif>
</cfloop>
</cfif>

PS: listgetat():
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_l_12.html


Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 07, 2008 Apr 07, 2008
I think that is what I had originally, before posting here for help because I ran into problems.

Anyways, here is my code. Two things are happening. If I submit the form without selecting a checkbox, I should get an error message. Instead, it says the select_urdn_number in the query where clause does not exists.Also, if there are four line items on the form and I check two of the four checkboxes and submit, the first selected line item is inserted once and the next one is inserted four times.

This is where I am lost and need help. The isdefined should be false if no checkboxes are selected, so it should fall thru to the error message. And why does it insert more than once ?

<cfif isDefined("form.FieldNames")>

<cfloop index="i" list="#form.fieldnames#" delimiters=",">

<cfif left(i,4) is "del_">
<!---cfset select_urdn_number = removechars(i,1,4)>
<cfset select_urdn_number = #evaluate(select_urdn_number)#--->
<cfset select_urdn_number = listgetat(i, 2, "_")>
<cfset select_urdn_line_item = listlast(i, "_")>
</cfif>

<cfquery>
update query goes here - this works

where urdn_number = '#select_urdn_number#'
and
urdn_line_item = '#select_urdn_line_item#'
</cfquery>

<cfquery>
insert query goes here - if 4 line items on the form, inserts one record 4 times instead of just once
</cfquery>

</cfloop>

<cfoutput>
#select_urdn_number# updated successfully
</cfoutput>
<cfelse> - if no checkboxes selected,
Display errro message here
</cfif>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 07, 2008 Apr 07, 2008
LATEST
no, the <cfif isDefined("form.FieldNames")> will not be false if no
checkbox is checked. as long as you click a 'submit' button at least
that button;s name will be in the form.fieldnames list.

so, apart from checking for <cfif isDefined("form.FieldNames")> you also
need to check that at least one checkbox is defined. can you do it?

and all your update/insert queries, obviously, need to go inside the
<cfloop index="i" list="#form.fieldnames#" delimiters=",">
<cfif left(i,4) is "del_">
block if you want to run them for each selected checkbox...

just a suggestion which will make your life a lot simpler:
name all your checkboxes the same - name="del_urdn", for example - but
give them different VALUEs, i.e. value="#urdn_number#_#urdn_line_item#".
this way your form will have ONE filed named del_urdn with a
comma-separated list of selected checkboxes' values as value.
this will make it easier for you to validate the form and to run your loop.

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources