Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate Structure From Qurey

Status
Not open for further replies.

imstillatwork

IS-IT--Management
Joined
Sep 26, 2001
Messages
1,605
Location
US
I ned to create a structure of table.ID and table.Name.
I know how structures work (I think), Now, How do I dynamicly create a structure from a query? I was hopeing it was this easy:::
==================
<cfquery name=&quot;getNameID&quot; datasource=&quot;vwv&quot;>
SELECT UserName, UserID
FROM tblUsers
</cfquery>

<cfset stNameID = structNew()>

<cfoutput query=&quot;getNameID&quot;>
<cfset stNameID.#UserID# = #UserName#>
</cfoutput>
======================
But its not.....
 
I have am currently working on a website that is using Structures as well and am finding them harder than i first thought. this is the way that I have done it:

<cfset stNameID = structNew()>

<CFOUTPUT QUERY=&quot;QUERY1&quot;>
<CFSCRIPT>
StructInsert(stName,UserID,Username);
</CFSCRIPT>
</CFOUTPUT>

hope that this helps !
 
OK that works, But how do I get back a set of values from that? If I give the UserID, how can I get back the corosponding Username? I cant get it to work.

Kevin
 
You can use StructFind(stName, Required username) this will output the value of the structure

If you want to loop over the structure the you can use:

<CFLOOP COLLECTION=&quot;#stName#&quot; ITEM=&quot;i&quot;>
<!--- referencing the data as--->
<cfoutput>
#stName#<BR>
</cfoutput>
</CFLOOP>
 
I understand how to loop through the entire structure. How do a give it one key and get one matching value? just one.
Say I have a UserID of 24, I want to get the Name out of the structure that goes with it.

thanks
 
When looping though a query, I prefer to use <cfloop> to avoid needless whitespace in the code whenever possible. It's also reportedly faster than <cfoutput> when looping through queries

<cfquery name=&quot;getNameID&quot; datasource=&quot;vwv&quot;>
SELECT UserName, UserID
FROM tblUsers
</cfquery>

[COLOR=666666]<!--- Create Structure --->[/color]
<cfset stNameID = structNew()>
<cfloop query=&quot;getNameID&quot;>
<cfset stNameID
Code:
[
getNameID.UserID
Code:
]
= getNameID.UserName>

</cfoutput>


[COLOR=666666]<!--- Loop Through collection (k=key) --->[/color]
<cfoutput>
<cfloop collection=&quot;#stName#&quot; item=&quot;k&quot;>
UserID: #k# - UserName: #stName
Code:
[
k
Code:
]
#[COLOR=000080]<BR>[/color]
</CFLOOP>
<cfoutput>


[COLOR=666666]<!--- Target one UserID from URL var --->[/color]
<cfset URL.UserID = 24>
<cfoutput>
UserID: #URL.UserID# - UserName: #stName
Code:
[
URL.UserID
Code:
]
#
</cfoutput> - tleish
 
Ok, here is the next part... How does one verify that a value does or does not exist for a given key?

<cfset URL.UserID = 3>

<cfif not #stNameID[URL.UserID]# eq &quot;&quot;> <!---this does not work--->

<cfoutput>
USERID: #URL.UserID#
<br>CARID: #stNameID[URL.UserID]#

</cfoutput>
</cfif>
 
Use the StructKeyExists() function:

Description
Returns TRUE if a key is in a structure and FALSE if it is not.

Syntax
StructKeyExists(structure, key)

=== START CODE EXAMPLE ===
<cfif StructKeyExists(stNameID, URL.UserID)>
<cfoutput>
USERID: #URL.UserID#[COLOR=000080]<br>[/color]
CARID: #stNameID
Code:
[
URL.UserID
Code:
]
#
</cfoutput>
</cfif>
=== END CODE EXAMPLE === - tleish
 
He he he .... oh me... and i just finished telling someone else to download the CFML reference from macromedia.com..

Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top