Solved

How to get a list of columns for the current query

Posted on 2000-02-17
14
125 Views
Last Modified: 2013-12-24
Simple question - How can I access a list of columns for a query, and how can I then get data from the columns. Eg, if I have a table containing columns Name, Address, Phone, how do I get a list of the three column names, and how do I then access the data in the columns - I cant go MyQuery.Phone because I dont know the names of the columns that will be present, and how many there will be.
0
Comment
Question by:Arachn1d
  • 6
  • 6
  • 2
14 Comments
 
LVL 1

Expert Comment

by:bigbadb
ID: 2531140
query_name.ColumnList

Returns a comma-delimited list of the query columns

Then you can read it into an array then access it that way

Hope this helps
0
 
LVL 5

Accepted Solution

by:
nathans earned 50 total points
ID: 2531694

I tested this and it works.
Nathan

<cfset datasource ="yourdatasource">
<cfset table ="yourtable">
<!--- Simply change the above datasource and table --->


<CFQUERY NAME="getcolumns" DATASOURCE="#datasource#">
SELECT      *
FROM         #table#
</CFQUERY>

<CFSET MyArray = ArrayNew(1)>


<html>
<head>
      <title></title>
</head>

<body>

<!--- <CFSET Item = ListGetAt(columnlist, lo, ",")>
 --->
<cfoutput>
<cfset cnt = 1>
<cfloop index="loopid" list="#getcolumns.columnlist#">
<cfset MyArray[#cnt#] = "#loopid#">
<cfset cnt = cnt + 1>
</cfloop>
<cfset length = cnt-1>
<table border="1" bordercolor="000099" cellpadding="0" cellspacing="0"><tr><td>
<table border="0" cellpadding="0" cellspacing="0" width="90%">
<tr>
<cfloop index="tablecol" from="1" to="#length#">
<td bgcolor="000099"><font face="Arial" color="ffffff" size="-2"><b>#MyArray[tablecol]#&nbsp;&nbsp;&nbsp;</b> </font></td>
</cfloop>
</tr>
<cfloop index="tablerow" from="1" to="#getcolumns.recordcount#">
<tr bgcolor="#IIf(tablerow Mod 2, DE('ffffff'), DE('EEEEEE'))#">
<cfloop index="tablecol" from="1" to="#length#">
<td><font face="Arial" color="000099" size="-2">
<cfset test = "getcolumns.#MyArray[tablecol]#">

#Evaluate(test)#
</font></td>
</cfloop>
</tr>
</cfloop>
</cfoutput>

</tr></table>
</td></tr></table>
</body>
</html>
0
 
LVL 1

Expert Comment

by:bigbadb
ID: 2531734
nice try stealing my answer nathans.  Query.columnlist works i figured you could come up with the array on your own
Arachn1d

good luck
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 5

Expert Comment

by:nathans
ID: 2531849
It takes MORE then just an array... you have to dynamically loop thru it as well.

==================================
More refined code.
==================================


<cfset datasource ="nsnd">
<cfset table ="cfMessageBoard">
<!---
<cfset datasource ="#url.ds#">
<cfset table ="#url.table#">
 --->


<CFQUERY NAME="getcolumns" DATASOURCE="#datasource#">
SELECT      *
FROM         #table#
</CFQUERY>

<CFSET MyArray = ArrayNew(1)>


<html>
<head>
      <title></title>
</head>

<body>

<!--- <CFSET Item = ListGetAt(columnlist, lo, ",")>
 --->
<cfoutput>
<cfset cnt = 1>
<cfloop index="loopid" list="#getcolumns.columnlist#">
<cfset MyArray[#cnt#] = "#loopid#">
<cfset cnt = cnt + 1>
</cfloop>
<cfset length = cnt-1>
<table border="1" bordercolor="000099" cellpadding="0" cellspacing="0"><tr><td>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<cfloop index="tablecol" from="1" to="#length#">
<td bgcolor="000099"><font face="Arial" color="ffffff" size="-2"><b>#MyArray[tablecol]#&nbsp;&nbsp;&nbsp;</b> </font></td>
</cfloop>
</tr>
</cfoutput>
<cfoutput query="getcolumns">
<tr bgcolor="#IIf(CurrentRow Mod 2, DE('ffffff'), DE('EEEEEE'))#">
<cfloop index="tablecol" from="1" to="#length#">
<cfset test = "getcolumns.#MyArray[tablecol]#">
<td align="center"><font face="Arial" color="000099" size="-2">
 #Evaluate(test)#
</font></td>
</cfloop>
</tr>
</cfoutput>
</table>
</td></tr></table>


</body>
</html>
0
 
LVL 1

Expert Comment

by:bigbadb
ID: 2531884
no its not you can parse that comma delem list into an array.  Have a counter var.  Then he will know how many columns he is dealing with and insert accordingly
0
 
LVL 5

Expert Comment

by:nathans
ID: 2531928
I am talking about to use it.  Maybe we are saying the same thing what is your example?
0
 
LVL 1

Expert Comment

by:bigbadb
ID: 2531994
<cfquery datasource="ohiotest" name="testme">
      select *
      from users
</cfquery>
<CFOUTPUT>
#TESTME.ColumnList#
<cfset holdtest = listtoarray(#testme.columnlist#)>
#arraylen(holdtest)#
</CFOUTPUT>

This right here returns the number of columns and you would access it just like an array from 1 to the arraylen()
0
 
LVL 5

Expert Comment

by:nathans
ID: 2532010
If I understand correctly he wants to get the data in each of the columns as well as the column names which is what I have done.


> how do I then access the data in the
> columns - I cant go MyQuery.Phone
> because I dont know the names of the
> columns that will be present, and how
> many there will be.
>

0
 
LVL 1

Expert Comment

by:bigbadb
ID: 2532051
you just loop through

loop through the recordcount
then with in the recordcount loop you have another loop to read each rows column from 1 to the array len


0
 

Author Comment

by:Arachn1d
ID: 2532249
Thankyou all for the great help, but AFAIK, none of you have answered the other part of my question - once I have the column list, how do I access the data in it? So I have an array/list/whatever of the columns. How do I access the data in the first column? The third one? Etc.
0
 
LVL 1

Expert Comment

by:bigbadb
ID: 2532281
this is done in the second loop

ie


<cfloop from="1" to="query.recordcount" index="count">

  <cfloop from="1" to="arraylen" index="count2">
     <cfset hold = "query.#array[count2]#">
     #Evaluate(hold)#
  </cfloop>
</cfloop>


THis would spit out each col and data within
0
 
LVL 5

Expert Comment

by:nathans
ID: 2532350
My second post of code you can cut and past as an example.  All you have to modify to see how it works is change the to cfset statements at the top of the code and you will see that this code will work.

I created the code for myself when I saw your post so I know it works. test it out.  

You will get the columns in a table with the data in the columns.
0
 

Author Comment

by:Arachn1d
ID: 2532775
Thanks for the answer. I found nathans was the first to actually explain how to access a column when you don't know it's name. If anyone else feels they deserve points, let me know, although I am a bit short (points, not feet high). One final question - how can I get the datatype for a column?
0
 
LVL 5

Expert Comment

by:nathans
ID: 2532845
Thanks,
Nathan Stanford
Mr. ColdFusion
-------------------------------------
CFTips+  FREE e-ZINE on ColdFusion
subscribe-cftips@nsnd.com
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Remove Index.php in Codeigniter 12 67
Domain Name E-mail Address & Outlook Integration 3 42
Website being blocked? 3 129
Best practices for sharing large amounts of data 9 99
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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