Solved

Data has a variable

Posted on 2000-04-07
10
147 Views
Last Modified: 2013-12-24
I have a table with 2 columns. One column is the name of a variable. The other column is the value for that variable. How can I get the data in Column one to be a variable?  For example. Column one has a value of "Var1" and Column two is "test". I need to be able to reference "VAR1" as <cfoutput>#var1#</cfoutput> with "test" as the result "test". IDEAS?
0
Comment
Question by:wills1300
  • 4
  • 3
  • 3
10 Comments
 
LVL 5

Expert Comment

by:nathans
Comment Utility
Your loosing me...

you have a table in a database

datasource = MyDataSource
TableName  = MyTable
Columns    = Col1
             Col2

Data in table is
Col1             Col2
------------------------
Var1             Test


You want to be able to say
<cfoutput>#var1#</cfoutput>

and the actual output be

Test

=========================
If that is what your saying the
below code is the answer.
=========================
<cfquery name="MyQuery" datasource="MyDataSource">
Select *
From MyTable
Where Col1 = "Var1"
</cfquery>

<cfset Var1="#Col2#">

<html>
<head>
      <title></title>
</head>
<body>
Output:
<cfoutput>#Var1#</cfoutput>

</body>
</html>





When you call for Var1
             
0
 
LVL 2

Expert Comment

by:paulkd
Comment Utility
Imagine that Col1 is your myquery.col1 and Col2 is your myquery.col2 ...

<CFSET Col1="Var1">
<CFSET Col2="test">

<CFSCRIPT>
'#Col1#' = #col2#;
</CFSCRIPT>

<CFOUTPUT>
#var1#
</CFOUTPUT>
0
 
LVL 2

Expert Comment

by:paulkd
Comment Utility
paulkd changed the proposed answer to a comment
0
 
LVL 5

Expert Comment

by:nathans
Comment Utility
Hello, wills1300 Are you there?
0
 

Author Comment

by:wills1300
Comment Utility
Apologies for the delay. I had problems with my email.  

To refine what I was asking:

In essence I have 2 columns: name & value. There's 3 row of data as follow:

var1   "Today"
var2   "Tomorrow"
var3   "yesterday"

I want to be able to reference #var1# directly and have it output "Today" and so on for the other variables.  Does this make it any clearer?  

In the mean while I will try your suggestions. PAULKD could you expand on your suggestion. I don't quite understand what you have about "myquery.col1".

thanks.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Expert Comment

by:paulkd
Comment Utility
Your query was called myquery... I just attempted to simulate the result of the query.

You can't have two columns of data and a third row.

It might be best if you simulated your table data in spreadsheet fashion, with actual data

0
 
LVL 5

Expert Comment

by:nathans
Comment Utility
Explain to me why mine code does not do what you want.

you have what a table with two columns?

column 1
=========
var1
var2
var3

Column 2
=========

"Today"
"Tomorrow"
"yesterday"

you want to be able to say

#var2#

and the output would be

Tomorrow

If this is a correct assessment of what your asking then my code does just this.  If you tell me why my code does not do what you want then I will know what to do to fix it.

Nathan
0
 

Author Comment

by:wills1300
Comment Utility
Nathans (&PAULKD)- Your suggestion would work - but it would require me to run a query for each "variable". I need something more 'dynamic' (if that's the right word). In addition the <CFSET VAR1 = 'col1'> is too static.  I would prefer not to use a <CFSET> tag. Right now I am using a structure to put all the variables with their values. So all I need to do is do a #structfind(name_of_structure, "var1")#, which would return "today".  I would prefer to make my code more basic - thus why I looking for another way to do this.

I guess, in creating these 'global variables' I don't want to have to name them specifically (as in using a cfset tag). Its only when I refer to them that I can refer to their specific names.

thanks,
0
 
LVL 2

Accepted Solution

by:
paulkd earned 200 total points
Comment Utility
I wasn't suggesting using CFSET I just couldn't be bothered to write some CFQUERY code.

Where did 'global variables' come from? just to remind the viewers of the original plot

I have a table with 2 columns. One column is the name of a variable. The other column is the value for that variable. How can I get the data in Column one to be a variable?  For example. Column one has a value of "Var1" and Column two is "test". I need to be able to reference "VAR1" as <cfoutput>#var1#</cfoutput> with "test" as the result "test". IDEAS?

So....

A_Table

Col1VarName   Col2VarValue
Var1          Today
Var2          Tomorrow
Var3          Yesterday


The ColdFusion

<CFQUERY Name="X" datasource="history">
SELECT * from A_Table
</CFQUERY>

<CFOUTPUT QUERY="X">
<CFSCRIPT>
'#X.Col1VarName#' = #X.Col2VarValue#;
</CFSCRIPT>
</CFOUTPUT>

<CFOUTPUT>
#Var1#<br>#Var2#<br>#Var3#
</CFOUTPUT>


0
 

Author Comment

by:wills1300
Comment Utility
That's it PAULKD - That's exactly what I wanted.

thanks.

thanks also NATHANS
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

10 Experts available now in Live!

Get 1:1 Help Now