0

I'm creating a new form and I thought I'd take jQuery's serialize() out for a spin. This is a totally new form so I'm not locked into form field names yet (all greenfield work).

I have the choice of using either data set (watch the equal signs closely there - this output is not a typo) :

data=userId=99&firstName=John&lastName=Doe&interestName=2&interestName=8&sdf&interestName=5 or data=userId=99&firstName=John&lastName=Doe&interestName_1=2&interestName_2=8&sdf&interestName_3=5

My current solution isn't pretty and with the conditional logic will not be easy to maintain. Now that I've explained my looping, here is the fugly code:

<cfoutput>  
    <cfloop list="#url.data#" index="i" delimiters="&">
        <cfset key = listfirst(i,'=')>
        <cfset keyValue = listlast(i,'=')>
    <cfif key eq 'userId'>
        <cfset userId = keyValue>
    </cfif>
    <cfif key contains 'interestName_'>         
        <cfquery name="rsInsert" datasource="playground">
            insert into table (optionId,userId)
                values
                (#keyValue#,#userid#)
        </cfquery>
    </cfif>
</cfloop>
</cfoutput>

What are some options to making this more efficient?

7
  • This would be much easier to answer if it were just one question, the way this system is intended to work. Commented Apr 20, 2016 at 18:50
  • Good point. I can modify the question. Commented Apr 20, 2016 at 18:52
  • "This could easily generate well over 60 SQL updates so there has to be better approach" that's easy to fix, simply concatenate all the updates to a single call to <cfquery> Commented Apr 20, 2016 at 18:52
  • well... i don't think either of the two questions are particularly good ones (for this site). They're very... stylistic questions, borderline subjective/opinionated. Both ways of submitting the form field names are acceptable, and inserting into the database effectively was just a code design flaw that i outlined in previous comment. Commented Apr 20, 2016 at 18:53
  • I tried to avoid the opinionated aspect but in this case, anyway is better than the way j came up with. :/ Commented Apr 20, 2016 at 18:54

1 Answer 1

1

You should always avoid performing a db operation on each iteration of a loop if at all possible in any programming language. In this case, you can move the loop to inside the <cfquery> and do a multi insert. You could also further reduce the size of the query depending on db support by moving the INSERT INTO (...) portion outside of the loop. This of course depends on multi-insert being allowed in your datasource settings.

<cfquery name="rsInsert" datasource="playground">
    <cfloop list="#url.data#" index="i" delimiters="&">
        <cfset key = listfirst(i,'=')>
        <cfset keyValue = listlast(i,'=')>
        <cfif key eq 'userId'>
            <cfset userId = keyValue>
        </cfif>
        <cfif key contains 'interestName_'>         
            insert into table (optionId,userId)
                values
                (#keyValue#,#userid#);
        </cfif>
    </cfloop>
</cfquery>

Don't forget to prevent sql injection.

Sign up to request clarification or add additional context in comments.

7 Comments

I'm using MSSQL for the DB. I didn't know I could loop inside a cfquery! Holy wow! Will need to investigate that further. Absolutely on the cfqueryparam - always. I'm being called into a meeting and will try this in about an hour or so and let you now how it goes. Thanks for sticking with my borderline subjective/opinionated question. :)
didn't know I could loop inside a cfquery! When it comes to queries, CF's only role is to interpret any CF variables embedded in the sql and send the generated sql string off to the database for execution. As long as that sql is valid - according to your dbms - it can be just about anything you want.
My own experience is that sometimes looping inside the query makes things run faster and sometimes it makes them run slower. Also, I try to minimize the amount of ColdFusion processing inside query tags because it makes the code harder to troubleshoot.
I prefer to do this with cfscript rather than tag-based code.
For anyone else who finds this later, be sure to wrap your cfquery statement with a cftransaction so you can roll back ALL the changes in the event an error occurs.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.