• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

embedding variable within variable

Ok, I've got a script created in coldfusion, and the way its written is that each variable name has a common prefix. The prefix is written out every time it is needed.

What I'd like to do is make it so that I can declare this prefix as a variable at the beginning of the document, and then affix the variable name to each instance that it is needed..

For instance, if I have the columns, "member_id, member_title, member_name"

They have corresponding variables that are used to contain data as a form is populated.

I'd like to be able to declare M = member, and then have "#M#_id, #M#_title, #M#_name"

That way if I wanted to change things, like from member to customer, I could simply go to the beginning of the document and set M = customer and throughout the document everything would use customer instead..

Problem is, I have to basically include a variable within a variable, and CFM doesn't seem to like that.

Take a simple query

<CFQUERY NAME="TEST" DATASOURCE="DATA">

SELECT member_id, member_title, member_name

FROM tbl_member_info

WHERE member_id = '#MYID#'
;
</CFQUERY>

I'd like to have it written something like

<CFQUERY NAME="TEST" DATASOURCE="DATA">

SELECT #M#_id, #M#_title, #M#_name

FROM tbl_#M#_info

WHERE #M#_id = '#MYID#'
;
</CFQUERY>

 
its been several years since i've ditched cold fusion, but i'm pretty sure you'll need to use the evaluate function
 
now that i've got some caffeine in my blood, i think all you need to do is

<cfset mysqlstatement = 'SELECT ' . #M# . '_id, ' . #M# . '_title, ' . #M# . '_name FROM tbl_' . #M# . '_info WHERE ' . #M# . '_id = \'' . #M# . '\''>

then output mysqlstatement inside of your cfquery


in my opinion, thats too much extra effort just to make a table and fieldname prefix change 'easier'
 
Originally posted by: troytime
now that i've got some caffeine in my blood, i think all you need to do is

<cfset mysqlstatement = 'SELECT ' . #M# . '_id, ' . #M# . '_title, ' . #M# . '_name FROM tbl_' . #M# . '_info WHERE ' . #M# . '_id = \'' . #M# . '\''>

then output mysqlstatement inside of your cfquery


in my opinion, thats too much extra effort just to make a table and fieldname prefix change 'easier'

yea, on the easier point.. im starting to think it will actually be easier to maintain seperate documents that have different prefixes then to actually create code that can be changed dynamically.. in concept, its a good idea to have 1 document that has generic code, but now that Im faced with creating that and managing that, I think id rather have 5 different scripts that are absent all of the "extra" code needed to make 1 generic one..



 
right on.

a lot of times, especially in cold fusion, if there is platform or db specific code it'll be in an include in a folder containing all of that platform/db code

so there'd be a /mysql/ directory for all mysql includes
and all the includes would use a constant thats set in Aplication.cfm
 
Back
Top