Error in cfloop

Hello experts.
I have an error using the code below when the form.widgettitle field has no value.Any help?
<cfloop from = "1" to="#listlen(form.id)#" index="mycount">
	<cfset thisid = listgetat(form.id, mycount)>
    <cfset thisorderval = listgetat(form.ordervalue, mycount)>
    <cfset thisisvisible = listgetat(form.isvisible, mycount)>
    <cfset thistitle = listgetat(form.widgettitle, mycount)>
    <cfset thisposition = listgetat(form.widgetposition, mycount)>
    
    <cfquery name = "qryUpdateWidgets" datasource="#request.dsn#">
    UPDATE widgets 
    SET isvisible = '#thisisvisible#',
    ordervalue = #thisorderval#,
    widgettitle = <cfif len(trim(thistitle))><cfqueryparam cfsqltype="cf_sql_varchar" value="#thistitle#"><cfelse>NULL</cfif>,
    widgetposition = '#thisposition#'
    WHERE id = #thisid#
    </cfquery>
    
</cfloop>

Open in new window

LVL 2
PanosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pdd1lanCommented:

<cfif #form.widgettitle# neq "">
      <cfset thistitle = listgetat(form.widgettitle, mycount)>
<cfelse>
      <cfset thistitle = "">
</cfif>
0
PanosAuthor Commented:
Hi pdd1lan.
The following code is returning the error :invali list index:
<cfloop from = "1" to="#listlen(form.id)#" index="mycount">
      <cfset thisid = listgetat(form.id, mycount)>
    <cfset thisorderval = listgetat(form.ordervalue, mycount)>
    <cfset thisisvisible = listgetat(form.isvisible, mycount)>
    <cfif #form.widgettitle# neq "">
      <cfset thistitle = listgetat(form.widgettitle, mycount)>
    <cfelse>
      <cfset thistitle = "">
    </cfif>
    <cfset thisposition = listgetat(form.widgetposition, mycount)>
   
    <cfquery name = "qryUpdateWidgets" datasource="#request.dsn#">
    UPDATE widgets
    SET isvisible = '#thisisvisible#',
    ordervalue = #thisorderval#,
    widgettitle = '#thistitle#',
    widgetposition = '#thisposition#'
    WHERE id = #thisid#
    </cfquery>
   
</cfloop>
0
Brijesh ChauhanStaff IT EngineerCommented:
can you paste the complete error, which line gives you this error ?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

PanosAuthor Commented:
Hi brijeshchauhan
Here is the full error report(i had to disable all the debugging and error report codes to get that)
In function ListGetAt(list, index [, delimiters]), the value of index, 14, is not a valid as the first argument (this list has 13 elements). Valid indexes are in the range 1 through the number of elements in the list.
0
Brijesh ChauhanStaff IT EngineerCommented:
Try this... assuming that the error is in widgettitle...

<cfloop from = "1" to="#listlen(form.id)#" index="mycount">
    <cfset thisid = listgetat(form.id, mycount)>
    <cfset thisorderval = listgetat(form.ordervalue, mycount)>
    <cfset thisisvisible = listgetat(form.isvisible, mycount)>
   
    <cfif len(form.widgettitle)>
     <cftry>
       <cfset thistitle = listgetat(form.widgettitle, mycount)>
     <cfcatch type="any">
    	<cfset thistitle = "">
    </cfcatch>
    </cftry>
    </cfif>

    <cfset thisposition = listgetat(form.widgetposition, mycount)>
   
    <cfquery name = "qryUpdateWidgets" datasource="#request.dsn#">
    UPDATE widgets
    SET isvisible = '#thisisvisible#',
    ordervalue = #thisorderval#,
    widgettitle = '#thistitle#',
    widgetposition = '#thisposition#'
    WHERE id = #thisid#
    </cfquery>
   
</cfloop>

Open in new window

0
PanosAuthor Commented:
I have no errors now but wrong entries in the db.
i deleted the cfif tag but again the entries are wrong.
i get the titles for wrong id's
0
gdemariaCommented:
Just testing to see if the list is long enough will not solve the problem.   The problem is that list functions ignore empty values, so if you have...

ID =  1,2,3,4

Order = A,,C,D

Then the Order for ID 3 will be D, which is wrong.  Testing to see if there are any values left, will not change the fact that you are getting the wrong values as you go along.

There are two solutions.

The preferred way is to add a number to the field names...

<input name="Order#counter#" .....>

<input name="ID#counter#"  .....>

Then you can easily associated all the fields from the same record.   This is the best way to do it because if someone enters a comma in the field, or leaves a field blank, you won't get messed up.

If you can guarantee that all fields entered will not have a comma, then you can use this method (not as good).   Just convert the list to an array with the new option to include empty list elements in the array.  Then just loop the array instead.

The following example shows for two fields, just replicate for all fields.   But again, the first method is much better than this...


ListToArray(list, delimiter, includeEmptyElements) returns Array

<cfset IDs = listToArray(form.id,",",true)>
<cfset OrderValues = listToArray(form.ordervalue,",",true)>

<cfloop from = "1" to="#arrayLen(IDs)#" index="counter">
    <cfset thisID = IDs[counter]>
    <cfset thisOrderVal = OrderValues[counter]>
    
    <cfquery name = "qryUpdateWidgets" datasource="#request.dsn#">
      UPDATE widgets 
       SET isvisible = '#thisisvisible#',
       ordervalue = #thisorderval#,
       widgettitle = <cfif len(trim(thistitle))><cfqueryparam cfsqltype="cf_sql_varchar" value="#thistitle#"><cfelse>NULL</cfif>,
       widgetposition = '#thisposition#'
      WHERE id = #thisid#
    </cfquery>
    
</cfloop>

Open in new window

0
PanosAuthor Commented:
Look below the code ,what is going on the loop.
The first one is when all widgettitle fields have a value and
the second if f.e the first has not.
id-widgettitle output

19-Tree_left,
20-Right_Tree,
1-testcategory1,
2-testcategory2,
3-testcategory3,
4-testcategory4,
11-testcategory5,
6-testcategory6,
7-testcategory7,
8-testcategory8,
9-testcategory9,
10-testcategory10,
12-testcategory11,
5-testcategory12,
15-testcategory13,


19-Right_Tree,
20-testcategory1,
1-testcategory2,
2-testcategory3,
3-testcategory4,
4-testcategory5,
11-testcategory6,
6-testcategory7,
7-testcategory8,
8-testcategory9,
9-testcategory10,
10-testcategory11,
12-testcategory12,
5-testcategory13,
15-,
0
PanosAuthor Commented:
Hi gdemaria.I missed your comment.
I will check it now
0
PanosAuthor Commented:
Hi again.
gdemaria please send me the  solution with the counters in the form.
I don't know if the following code is coorect but it turns an error:The element at position 15 of dimension 1, of array variable &quot;WIDGETTITLES,&quot; cannot be found.

and i get again wrong entries.

<cfset IDs = listToArray(form.id,",",true)>
<cfset OrderValues = listToArray(form.ordervalue,",",true)>
<cfset Isvisibles = listToArray(form.isvisible,",",true)>
<cfset Widgettitles = listToArray(form.widgettitle,",",true)>
<cfset Widgetpositions = listToArray(form.widgetposition,",",true)>

<cfloop from = "1" to="#arrayLen(IDs)#" index="counter">
    <cfset thisID = IDs[counter]>
    <cfset thisOrderVal = OrderValues[counter]>
    <cfset thisisvisible = Isvisibles[counter]>
    <cfset thistitle = Widgettitles[counter]>
    <cfset thisposition = Widgetpositions[counter]>
    
    <cfquery name = "qryUpdateWidgets" datasource="#request.dsn#">
      UPDATE widgets 
       SET isvisible = '#thisisvisible#',
       ordervalue = #thisorderval#,
       widgettitle = '#thistitle#',
       widgetposition = '#thisposition#'
      WHERE id = #thisid#
    </cfquery>
    
</cfloop>

Open in new window

0
gdemariaCommented:

You could do a dump to see what the data looks like..

<cfset IDs = listToArray(form.id,",",true)>
<cfset OrderValues = listToArray(form.ordervalue,",",true)>
<cfset Isvisibles = listToArray(form.isvisible,",",true)>
<cfset Widgettitles = listToArray(form.widgettitle,",",true)>
<cfset Widgetpositions = listToArray(form.widgetposition,",",true)>

<cfdump var="#IDs#">
<cfdump var="#widgetTitles#">

<cfabort>
<cfloop.....>


Meanwhile, I will provide an example of the form field counters..
0
gdemariaCommented:

For each record, increment a counter and attach a number to the form fields...

<cfoutput query="myData">
  <cfset ctr = myData.currentRow>  <!----- set the counter ---->

  <input name="Order#ctr#" .....>

   <input name="ID#ctr#"  .....>

</cfoutput> <!--- end form field loop ---->

<input type="hidden" name="recordCount" value="#ctr#"> <!---- now hold the total numb records --->



The code below is a sample of the action page, showing 3 of the forme fields.

In essence, you are gettnig the value of FORM.ID1,  FORM.ID2, etc..

To do this, use the structure format FORM["ID1"]  except you want to make the counter dynamic, so it would be   FORM["ID" & counter] or   FORM["ID#counter#"]

make sense?

<cfloop from = "1" to="#form.recordCount#" index="counter">
    <cfset thisID = form["id" & counter]>
    <cfset thisOrderVal = form["OrderValues" & counter]>
    <cfset thistitle    = form["Widgettitles" & counter]>
    
    <cfquery name = "qryUpdateWidgets" datasource="#request.dsn#">
      UPDATE widgets 
       SET isvisible = '#thisisvisible#',
       ordervalue = #thisorderval#,
       widgettitle = '#thistitle#',
       widgetposition = '#thisposition#'
      WHERE id = #thisid#
    </cfquery>
    
</cfloop>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PanosAuthor Commented:
Below the two screen shots.
All the widgettitle fields have a vakue in the first one
One has not in the second
screen1.gif
screen2.gif
0
gdemariaCommented:
hmmm, curious.   What type of field is form.widgettitle ?   If the form field is TEXT or SELECT there should be a blank in the array.  If the form field type is CHECKBOX, then that would be a problem.

Can you show the value of this?

<cfdump var="#form.widgettitle">

I expect to see two commas together somewhere such as

     Coupon Code,,Wish List, Brand Browser

Those two commas together are where the value is left blank..


(in either case, the field counter approach should resolve it as well and is a better more robust solution)
0
PanosAuthor Commented:
Here are the output.
Right_Tree,Featured Item,Links Section,Best Seller,Featured Item,Quick Jump,Cart Contents,GiftCards,OrderCheck,Affiliates,Email List,Coupon Code,Wish List,Brand Browser
Nothing for the blank field.(the code for the output)
<cfset IDs = listToArray(form.id,",",true)>
<cfset OrderValues = listToArray(form.ordervalue,",",true)>
<cfset Isvisibles = listToArray(form.isvisible,",",true)>
<cfset Widgettitles = listToArray(form.widgettitle,",",true)>
<cfset Widgetpositions = listToArray(form.widgetposition,",",true)>

<cfdump var="#form.widgettitle#">

But the happy news are that your second solution is working perfect.
Thank you very much
0
PanosAuthor Commented:
Thank you again very much.
regards
panos
0
gdemariaCommented:
> Right_Tree,Featured Item,Links Section,Best Seller,Featured Item,Quick Jump,Cart Contents,GiftCards,OrderCheck,Affiliates,Email List,Coupon Code,Wish List,Brand Browser

Wondering why there is no ,, for the blank field.  Possible reasons include:  the field is a checkbox, the field is disabled, it is not a typical form field??


Glad the other solution is working, it is way to go..

0
PanosAuthor Commented:
This isa  tutorial i did copy from somewhere.
In the beginning before i add the question here there was for the widgettitlefield   type="widgettitle" .I did correct this mistake.But i don't know if the CF server did hold this.May be clearing the cache it would solve the problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.