Solved

Inserting an array into another array with ColdFusion

Posted on 2014-01-30
3
190 Views
Last Modified: 2014-01-30
I have some code that checks a form for errors and puts any in an array. The results might look something like this:

------------------------------------------------------------------------------------------
Array: Errors
------------------------------------------------------------------------------------------
name        | Please enter your name.
------------------------------------------------------------------------------------------
questions  | Please enter your questions or comments
------------------------------------------------------------------------------------------

I have another array that needs to contain the information from this array, which should look something like this:

------------------------------------------------------------------------------------------
Array: data
------------------------------------------------------------------------------------------
success        | False
------------------------------------------------------------------------------------------
errors           | name        | Please enter your name.
------------------------------------------------------------------------------------------
                     | questions  | Please enter your questions or comments
------------------------------------------------------------------------------------------

I can't for the life of me figure out how to get the first array into the second array. Following is the code I have so far:

<cfset errors = ArrayNew(2)>
<cfset data = ArrayNew(3)>

<cfif Trim(FORM.name) EQ "">
	<cfset errors[1][1] = "name">
	<cfset errors[1][2] = "Please enter your name.">
</cfif>
<cfif Trim(FORM.email) EQ "" OR NOT IsValid("email", FORM.email)>
	<cfset errors[2][1] = "email">
	<cfset errors[2][2] = "Please enter a valid email.">
</cfif>
<cfif Trim(FORM.phone) EQ "">
	<cfset errors[3][1] = "phone">
	<cfset errors[3][2] = "Please enter your phone number.">
</cfif>
<cfif Trim(FORM.questions) EQ "">
	<cfset errors[4][1] = "questions">
	<cfset errors[4][2] = "Please enter your questions or comments.">
</cfif>

<cfif NOT Len(errors)>
	<cfset data[1][1] = "success">
	<cfset data[1][2] = "false">
	<cfset data[2][1] = "errors">
 	<cfset ArrayInsertAt(data[2], 2, errors)>
<cfelse>
	<cfset data[1][1] = "success">
	<cfset data[1][2] = "true">
	<cfset data[2][1] = "message">
	<cfset data[2][2] = "Thank you for your inquiry! We'll be in touch with you soon.">
</cfif>

Open in new window


The ArrayInsertAt obviously doesn't work, it's just the latest in my string of failed attempts. What should I be doing here? Thanks!
0
Comment
Question by:SiobhanElara
3 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 39821338
I'm a bit biased here, I don't like two-dimensional arrays for this purpose.  So easy to get lost.    Before answering your question directly, I would like to suggest changing your format to a simpler design.   Your design also has the flaw that if the person leaves out their name and message, then you have an array with position 1 and 4 populated but not 2 and 3.

The following code uses names values instead of numbers (structures instead of arrays) and fills your error array in order (without skipping over numbers)

<!--- assume success ---->
<cfset data = structNew()>
<cfset data.status  = "success">
<cfset data.valid   = true>
<cfset data.message = "Thank you for your inquiry! We'll be in touch with you soon.">
<cfset data.error   = arrayNew(1)> <!--- empty error array --->


<cfif Trim(FORM.name) EQ "">
    <cfset pos = arrayLen(errors)+1>
    <cfset errors[pos] = structNew()>
	<cfset errors[pos].code = "Name">
	<cfset errors[pos].message = "Please enter your name">
</cfif>
<cfif Trim(FORM.name) EQ "">
    <cfset pos = arrayLen(errors)+1>
    <cfset errors[pos] = structNew()>
	<cfset errors[pos].code = "email">
	<cfset errors[pos].message = "Please enter a valid email">
</cfif>
<cfif Trim(FORM.name) EQ "">
    <cfset pos = arrayLen(errors)+1>
    <cfset errors[pos] = structNew()>
	<cfset errors[pos].code = "phone">
	<cfset errors[pos].message = "Please enter your phone number">
</cfif>
<cfif Trim(FORM.name) EQ "">
    <cfset pos = arrayLen(errors)+1>
    <cfset errors[pos] = structNew()>
	<cfset errors[pos].code = "questions">
	<cfset errors[pos].message = "Please enter your questions or comments">
</cfif>
<cfif arrayLen(errors)>
   <cfset data.valid = false>
   <cfset data.message = "Please correct the following errors">
   <cfset data.error   = errors>
</cfif>

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 39821466
(no points ...)

FWIW - I completely agree with gdemaria.  His approach is much more intuitive.
0
 

Author Closing Comment

by:SiobhanElara
ID: 39821691
That is SO much better! Thank you.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using Coldfusion to create a CSV file 13 94
Cold Fusion Not Recognizing .NET DLL Updates 7 51
RETURN INFORMATION FROM COLDFSION CFC 1 63
Comparing 2 Lists Coldfusion 8 48
PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
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 …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

12 Experts available now in Live!

Get 1:1 Help Now