?
Solved

Query in ColdFusion

Posted on 2004-11-19
5
Medium Priority
?
173 Views
Last Modified: 2013-12-24
Hey guys,
     I'm fairly new to Dreamweaver/Coldfusion and was wondering how to do the following:
I have a database that I want to pull almost all the data from. For example, I have TableA with 26 fields, named A-Z in order respectively. I want to place this data into a HTML table (2x13), in a specific order that is not the order of the columns in my table. (e.g. C, Z, Y, Q…). How do I do that in one query (select * from TableA), but select the actual values from the specific fields to enter into my HTML table? That way, for each of the cells within the HTML table, I can insert the proper value from the query. Thanks.
               Sincerely,
                     JL
0
Comment
Question by:jl_forum
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:thack111
ID: 12629077
make the query:
<cfquery name='getdata' datasource='myDSN'>
    select *
    from tableA
</cfquery>
Then display it

<table>
<tr><th>heading1</th><th>heading2</th></tr>
<cfoutput query='getdata'>
      <tr><td>#B#</td><td>#A#</td></tr>
</cfoutput>
it will loop through the "<cfoutput>" tags till all records are displayed, and you can access the values by the field names, and put them in any order you want.

good luck
0
 

Author Comment

by:jl_forum
ID: 12631303
Actually, thats what I wanted to know. How exactly do you access the values? You mentioned by field names, but what does that mean? E.g. C.Value (?) I'm not sure. Thanks.
0
 
LVL 15

Expert Comment

by:DanielSKim
ID: 12631334
This provides a somewhat dynamic solution, allowing you to specify in a single list the number of fields and the order you want the fields to be displayed. All you have to do is put in a comma delimited list the field order you want, from left to right. Example:

All you have to is the order, like this:
<cfset fieldOrder = "B,C,A,D">
And you will get:
B C
A D

You said you are fairly new to ColdFusion, so if you are unfamiliar with some of the CF functions i've used below, let me know what you would like me to explain.

<!---
<cfquery name="qryName" datasource="datasourceName">
select *
from tableName
</cfquery>
---->

<!---
*********************************************************************
- creates a sample query
- comment out, or delete the following block of code, when using the query above
*********************************************************************
--->
<cfset qryName = QueryNew("A,B,C,D")>
<cfset QueryAddRow(qryName)>
<cfset QuerySetCell(qryName, "A", "A")>
<cfset QuerySetCell(qryName, "B", "B")>
<cfset QuerySetCell(qryName, "C", "C")>
<cfset QuerySetCell(qryName, "D", "D")>
<!---
*********************************************************************
--->


<!---
*********************************************************************
set the field order here, putting in a comma-delimited list how you want the values to appear from left to right
*********************************************************************
--->
<cfset fieldOrder = "B,C,A,D">
<cfset numOfFields = ListLen(fieldOrder)>

<table>
<cfloop from="1" to="#numOfFields#" index="f">
  <cfset fieldName = ListGetAt(fieldOrder, f)>
  <cfset fieldValue = Evaluate("qryName." & fieldName)>
  <cfif f MOD 2 EQ 1>
  <tr>
  </cfif>
    <td><cfoutput>#fieldValue#</cfoutput></td>
  <cfif f MOD 2 EQ 0>
  </tr>
  </cfif>
</cfloop>
</table>
0
 
LVL 1

Accepted Solution

by:
thack111 earned 200 total points
ID: 12634994
you can access the values of the fields, by
<cfoutput query='queryName'>
#fieldsName#
</cfoutput>
the above loops through all records.

or

<cfoutput>
#queryName.fieldName#
</cfoutput>
the above references the first record or the only record in a 1 record result.

You can put as much inside the <cfoutput> as you need to.
hope that helps.
0
 

Author Comment

by:jl_forum
ID: 12660865
Cool! =) Thanks.
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

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

Turn A Profile Picture Into A Cartoon Using Photoshop And Illustrator This tutorial will teach you how to make a cartoon style image out of a regular picture. I have tried to keep the tutorial as simple as possible. I used Adobe CS4 for this tuto…
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
The purpose of this video is to demonstrate how to connect a WordPress website to Google Analytics. This will be demonstrated using a Windows 8 PC Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php :…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
Suggested Courses

809 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