Solved

Coldfusion loop through a list of pairs name  -  value

Posted on 2016-10-19
3
27 Views
Last Modified: 2016-10-19
Hello experts.
I'm using jquery serialize function in a form in a pop up window to store temporaly the values of name and value of a form in a hidden field (myhiddenfield) in my main form.
so my hidden field has a value like name_1=value_1&name_2=value_2&name_3=value_3
Submitting the form I'm taking the myhiddenfield value as a list with delimiter the & sign
<cfloop list="#myhiddenfield#" index="item" delimiters="&">
  <cfset firstcolumn =  listfirst(item,'=')>
  <cfset secondcolumn =  listlast(item,'=')>
</cfloop>

The problem i have is if there is not any value for a one key.
f.e.  name_1=&name_2=value_2&name_3=value_3
I get for the first list item the same value for firstcolumn and secondcolumn
For testing i tried the listgetat function like listGetAt(item,2,'=') and i get an error Invalid list index 2 because the listitem has one element.
I have a solution using the listlen function like:
<cfif listlen(item,'=') is 2>
    <cfset secondcolumn = listlast(item,'=')>
<cfelse>
    <cfset secondcolumn = ''>
</cfif>
but i need your opinion if there is another way to handle this.
Thank you in advance
0
Comment
Question by:Panos
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 41850408
Despite the weird name, I like using getToken(). It doesn't throw an error if you supply an index that's greater than the number of elements in the list. It just returns an empty string.

      <cfset firstColumn = getToken(item, 1, "=")>
      <cfset secondColumn = getToken(item, 2, "=")>

You could also convert it to an array, setting includeEmptyFields=true, but it's a bit more wordy:

         <cfset data = ListToArray(item, "=", true)>
         <cfif arraylen(data) gte 2>
          <cfset firstColumn = data [1]
          <cfset secondColumn = data[2]>
         </cfif>
0
 
LVL 2

Author Comment

by:Panos
ID: 41850421
Thank you again for your help
0
 
LVL 52

Expert Comment

by:_agx_
ID: 41850475
Welcome :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
COLD FUSION 6 20
Build PDF in coldfusion 2 57
Coldfusion scheduled tasks as one task 2 66
coldfusion, javascript onclick url update 4 27
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now