using a variable as a variable name

Discussion in 'Programming' started by Jamie18, Jun 20, 2008.

  1. #1
    i'm attempting to make a more or less dynamic querying page for my site... where the user can choose which columns to be shown in the result set.. since i don't want to hardcode everything in i've attempted to loop through a few things

    here's my problem
    
    <!--- query for client columns --->
    <cfquery name="clientinfo" datasource="#request.library#" cachedwithin="#createtimespan(0,1,0,0)#">
    	SELECT	field_name,
    			lang1,
    			lang2
    	FROM	field
    	WHERE	page_type like 'contacts'
    </cfquery>
    
    ...
    ...
    <cfset contact_fields=form.contact_fields>
    
    <cfquery name="something" datasource="#request.library#">
    	SELECT	<cfloop list="#contact_fields#" index="name">
    				#name# as 'contact_name',
    			</cfloop>
    			...
    			...
    </cfquery>
    
    
    <cfloop query="contactinfo">
    	<cfif listfind(contact_fields,"#name#")>
    		<td class="ptrlist">
    			[COLOR="Red"]#contact_#field_name##[/COLOR]
    		</td>
    	</cfif>
    </cfloop>
    
    Code (markup):
    so i'll try and explain what's going on a bit.. (although this is just a snippet of a much larger app it should be enough to get my problem across)

    i've got some select boxes for filters and fields
    - the user selects how to filter the query and which fields to show in the results and submits

    from that i do the query
    - in the query each different select box of fields gets a prefix (ie contact_) for the contact select box

    my problem is in the output.
    - i loop through to determine which fields are to be displayed based on what the user has chosen

    here i need to display fields such as contact_phone, contact_id, etc. however i need to do this in a loop which means i'm using a variable within a variable
    #contact_#field_name##

    this obviously doesn't work
    does anyone know a way around this?

    sorry if my description is a little vague.. i can't think of a good way to explain it
     
    Jamie18, Jun 20, 2008 IP
  2. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i figured it out....... if anyone else runs into a similar problem in the future..

    i replaced the #contact_#field_name## with
    <cfset thisfield="something.client_"&field_name&"[#something.currentrow#]">
    #evaluate(thisfield)#
     
    Jamie18, Jun 20, 2008 IP