Jeff Fabiny
Jeff Fabiny

Reputation: 325

Concatenate a ColdFusion variable before it's read

I am not sure if this is possible. Can I concatenate two strings representing a variable name, and then read the variable value from that? For example:

<cfloop index="person" from="0" to="#numberAuthorized - 1#">
  //USING 'thePerson' DIDN'T WORK EITHER
  <cfset thePerson = 'authorized_name' & person>
  <cfquery name="qAddAuthorizedPeople" datasource="#thedb#" blockfactor="100">
    INSERT INTO people (requestid, fullname)
    VALUES ('#requestid#', '#authorized_name & 1#')
  </cfquery>
</cfloop>

The variables I need to read are authorized_name0, authrozied_name1, etc.. So I'm looping so I can increment an index an append it to the end of the variable name. Then insert into my database like that. Obviously, this isn't working. I also tried concatenating before and setting it to a variable (thePerson) and putting #thePerson# in the query, but that also didn't work. Any way I can do this?

Upvotes: 1

Views: 2863

Answers (2)

JamesRLamar
JamesRLamar

Reputation: 908

If you were getting your list of names from a query this how you would loop through and add them in an INSERT to the db:

<cfloop from="1" to="#structCount(numberAuthorized)#" index="person">
    <cfquery name="qAddAuthorizedPeople" datasource="#thedb#" blockfactor="100">
    INSERT INTO people (requestid, fullname)
    VALUES ('#requestid#', '#numberAuthorized["authorized_name"][person]#')
    </cfquery>
</cfloop>

In the above example "authorized_name" is assumed to be the name of the column the table or structure you are getting the names from. This could just as easily come from a struct of some kind.

I changed the to range to use structCount(numberAuthorized) if that is the name of the form struct coming in. If the form names have values appended to the end of each name like "authorized_name_0" and "authorized_name_1", it would be clearer in the code to set those variables before each insertion just so it is clear that is what you are doing. Example:

<cfloop from="1" to="#structCount(numberAuthorized)#" index="person">
  <cfset counter = counter + 1>
  <cfset newFullname = numberAuthorized["authorized_name_" & counter]>
  <cfquery name="qAddAuthorizedPeople" datasource="#thedb#" blockfactor="100">
  INSERT INTO people (requestid, fullname)
  VALUES ('#requestid#', '#numberAuthorized["authorized_name"][person]#')
  </cfquery>
</cfloop> 

It's still not clear if you're working from a struct or what so the syntax will needs to change depending on that. Consider the above more pseudo code from that perspective.

Upvotes: 0

Leigh
Leigh

Reputation: 28873

Edit I am not sure how you are passing these variables. But say they are in the FORM scope... FORM is a structure too. With any structure, you can access the keys dynamically using associative array notation. (Also, be sure to properly scope your variables.)

<cfloop from="0" to="#numberAuthorized - 1#" index="counter">
  <!--- extract value of authorized_name0, authorized_name1, ... --->
  <cfset variables.fullName = FORM["authorized_name"& counter]>

  <cfquery name="qAddAuthorizedPeople" datasource="#thedb#" blockfactor="100">
    INSERT INTO people (requestid, fullname)
    VALUES (
      <cfqueryparam value="#requestid#" cfsqltype="cf_sql_varchar">
      , <cfqueryparam value="#variables.fullName#" cfsqltype="cf_sql_varchar">
    )
  </cfquery>
</cfloop>

Upvotes: 5

Related Questions