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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through a query 1

Status
Not open for further replies.

ma701sd

IS-IT--Management
Dec 15, 2000
94
GB
Hi,
Im trying to loop through a query in a database by performing a search for a keyword in all records in a database. This works fine i.e. given a keyword, N number of records are found. However, I also want to display the username responsible for each row. What happens is that two records are found but it only displays one username for two distinct records when it should display two usernames (one for each row). Can any1 help?

Thanx

Sam...

 
Hey Sam,

Do you mean that the query outputs two rows with different values for one field but the username field shows the same value for both rows?

If you can post the output code as well as your initial query, that would help spot the problem.

GJ
 
You have to set the &quot;QUERY&quot; attribute in the <CFOUTPUT> tag so that it knows it's looping through a query, otherwise it returns the first result of a query.

<CFQUERY NAME=&quot;qMyQuery&quot; DATASOURCE=&quot;myDataSource&quot;>
SELECT UserName FROM myTable WHERE myCell = 'Test'
</CFQUERY>

<!--- Output the number of records found --->
<CFOUTPUT>#qMyQuery.RecordCount#</CFOUTPUT><BR><BR>

<!--- Loop through the results of the query --->
<CFOUTPUT QUERY=&quot;qMyQuery&quot;>
#qMyQuery.UserName#<BR>
</CFOUTPUT>
 
Hi Gunjack,

Yep, Thats exactly what I mean.
Below is the code :
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<CFQUERY NAME =&quot;WebPageResults&quot; DATASOURCE=&quot;Online&quot;>
SELECT title,heading,section1heading1,section1text1,section2heading2,section2text2
FROM UserSpecific
WHERE title LIKE '%#Form.Criteria#%'
</cfquery>

<html>
<head>
<title>Webpage collection results</title>
</head>

<body>


<!--- <CFOUTPUT QUERY=&quot;WebPageResults&quot;> --->
<CFIF #form.criteria# is &quot;&quot;>
Im sorry, no results match the criteria you entered
<CFELSE>
<CFOUTPUT>#WebPageResults.RecordCount# user websites Found for &quot;#Form.Criteria#&quot;.<BR></cfoutput>
<CFLOOP QUERY=&quot;WebPageResults&quot;>
<CFOUTPUT>
#title# 1<BR> <!---This website was created by <a href=&quot;WebSearchSiteView.cfm?#username#=#username#&quot;>#username#</a> --->
#username# 2<BR>
</cfoutput>
</CFLOOP>
</cfif>

</body>
</html>

You might notice, ive put in a number 1 and 2 after each of the variables #title# and #UserName# respectively.
It displays two different title but it displays the same username for both titles when there are two usernames associated to the two titles.

Many thanx for your help GJ...Much appreciated

Sam....

 
Kathryn,

Here is the code from my online knowledge base search results. It displays all of the query results for a given search, my other code is at home, I'm on a laptop now, I'll post the other later if this is of no assistance.

<CFQUERY name=&quot;SearchResult&quot; dataSource=&quot;support&quot; username=&quot;######&quot;>
SELECT DISTINCT ABCard.Card_ID AS ResultField1, ABCard.Summary AS ResultField2, ABCard.Card_ID AS ID_Field, ABCard.ChangeDate AS DetailField4
FROM ABCard
WHERE #PreserveSingleQuotes(Criteria)#
Order by ABCard.ChangeDate Desc
</CFQUERY>
<CFOUTPUT query=&quot;SearchResult&quot;>
<TR bgcolor=&quot;#IIf(CurrentRow Mod 2, DE('ffffff'), DE('cccccc'))#&quot;>
<TD><font face='Tahoma'>#ResultField1#</font></TD>
<TD></TD>
<TD><A href=&quot;support_Detail.cfm?ID=#URLEncodedFormat(ID_Field)#&quot;><font face='Tahoma'>#ResultField2#</font></A></TD>
<TD><Font face='Tahoma'>#DetailField4#</FONT></TD>


</TR>
</CFOUTPUT>


SteelDragon
 
Hey Sam,

Try changing your variables inside the cfoutput tags to #WebPageResults.username#. Although I can't reproduce it now, I seem to remember having problems in the past with <cfloop> not scoping the query variables correctly.

If this doesn't fix it, modify your criteria to exclude the row that matches the username being displayed and see what happens. That should force it to display the other username. If that works, put the criteria back and reverse the sort order.

Let me know what happens as I don't see anything wrong that would cause that problem.

GJ
 
Hey GJ,
It works like a dream i.e. it loops through all records and displays each username and one of its columns that ive decided to output, now the next problem...
What I then do within the loop is to create a link to an action page which will display the record to extract other info, however, the variable passed to the next page is the first username variable...Should it not pass the username variable that I select i.e. if I click A then dispays A's record, else if I click B then it should display B's record?
Currently, regardless of me clicking A's link or B's Link, it still extracts A's record.

I did use a CFOUTPUT tag on the action page to display what varaible is being passed and its displaying A's. On the action page, I am running a query to

select *
From UserSpecific
Where uername=#UserName#
But if username is always A, then it will extract A's record...

Any ideas,

Cheers
Sam...
 
Hey Sam,

It sounds like the problem is in your code that creates the link. I would check the same thing and make sure when you create your dynamic link, you're using #queryname.username# and not just #username#. If that's not it, post the code where you're creating the link and I'll take a look.

GJ
 
Hey GJ

I think whats happening is that the link is generated for the last record that is found, but I though the loop would take care of that i.e. generate the correct username bacause that is what the loop does...loop through all records and display all users...So I guess it mut be the link. I did put in the #queryname.username# but it still does not work..Ive posted the code below:

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<CFQUERY NAME =&quot;WebPageResults&quot; DATASOURCE=&quot;Online&quot;>
SELECT username, title,heading,section1heading1,section1text1,section2heading2,section2text2
FROM UserSpecific
WHERE title LIKE '%#Form.Criteria#%'
</cfquery>

<html>
<head>
<title>Webpage collection results</title>
</head>

<body>

<!--- <CFOUTPUT QUERY=&quot;WebPageResults&quot;> --->
<CFIF #form.criteria# is &quot;&quot;>
Im sorry, no results match the criteria you entered
<CFELSE>
<!--- <CFOUTPUT>#WebPageResults.username# user websites Found for &quot;#Form.Criteria#&quot;.<BR></cfoutput> --->

<CFLOOP QUERY=&quot;WebPageResults&quot;>
<CFOUTPUT>
A website created by #WebPageResults.username# was Found for &quot;#Form.Criteria#&quot;.<BR>
#title# 1<BR>
#username# 2<BR>
This website was created by <a href=&quot;WebSearchSiteView.cfm?#username#=#username#&quot;>#webpageresults.username#</a><BR><BR>
</cfoutput>
</CFLOOP>
</cfif>

</body>
</html>

ANy ideas??

Cheers

Sam...
 
Hehe, I think I see the problem. You put the queryname on the visible part of the link but not the part the browser actually uses :)

Try this:
<a ref= &quot;WebSearchSiteView.cfm?username=#webpageresults.username#&quot;>
#webpageresults.username#</a>

GJ
 
Hey GJ,
Thanx for the speedy response, It works but something weird is happening...

The variable being passed to the action page is weird.

If I perfrom a dynamic query provided by the #queryname.username# on the previous it still does not display the correct record, however, If I hard code the username I want into the query then it works yet it still displays the username that I dont want ( i checked this by displaying the variable within CFOUTPUT tags), I'm not even passing it!...or am I?

Let me give you an example,
Theres two users in the DB, A and B, A is the first record in the DB and B is the second. I Perform a search which displays one record A. I pass this username (A) onto the third action page by providing the query with the Variable value A but it still displays B, Yet if I hardcode the Username A into the query on the third page then it displays A's record but somehow it still displays B's username as the vaiable (I did this as a test to make sure that A's user name was being passed and not B's)???

Ive posted the code:

Page one is the search page.

Page two is what you have been helping me with but I'll post it again:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<CFQUERY NAME =&quot;WebPageResults&quot; DATASOURCE=&quot;Online&quot;>
SELECT username, title,heading,section1heading1,section1text1,section2heading2,section2text2
FROM UserSpecific
WHERE title LIKE '%#Form.Criteria#%'
</cfquery>

<html>
<head>
<title>Webpage collection results</title>
</head>

<body>

<!--- <CFOUTPUT QUERY=&quot;WebPageResults&quot;> --->
<CFIF #form.criteria# is &quot;&quot;>
Im sorry, no results match the criteria you entered
<CFELSE>
<!--- <CFOUTPUT>#WebPageResults.username# user websites Found for &quot;#Form.Criteria#&quot;.<BR></cfoutput> --->

<CFLOOP QUERY=&quot;WebPageResults&quot;>
<CFOUTPUT>
A website created by #WebPageResults.username# was Found for the criteria &quot;#Form.Criteria#&quot;.<BR>

#title# 1<BR>
This website was created by <a href=&quot;WebSearchSiteView.cfm?#username#=#webpageresults.username#&quot;>#webpageresults.username#</a><BR><BR>

</cfoutput>
</CFLOOP>
</cfif>
</body>
</html>

Page three:

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<CFOUTPUT>
<H1>#Username#</h1>
</cfoutput>

<CFQUERY DATASOURCE= &quot;ONLINE&quot; NAME=&quot;RecreateUserPage&quot;>
SELECT *
FROM UserSpecific
WHERE Username= 'A'
</CFQUERY>

<CFOUTPUT>
<H1>#Username#</h1><BR>
</cfoutput>

As you can see, on this page, I am hardcoding the username A yet as a check I am seeing what variable is being displayed it its displaying B so in essence, If I pass a variable, It will pick up B as the username to seacrh on...

I hope this makes sense??

Cheers

Sam
 
Hey Sam,

I think you have another variable defined somewhere else called &quot;username&quot; that has &quot;b&quot; as it's value. I think that if you again scope the output variable with the query name, you'll see the correct value. Do you by chance have a session, client, application, or possibly another query with a username field that might be taking precendence?

You could try something like <cfoutput>#variables.username# - #form.username# - #cookie.username# - #session.username# - #client.username# - #application.username# ....</cfoutput> and see what happens. You will almost assuredly get an error but just delete out the variables it can't find untill you either find another username variable or eliminate every possible scope. I think you will find though that username is defined somewhere else.

Hope this helps,
GJ
 
Hey GJ,

U are right, I used a process of elimination and found that I got errors for all scoped variables apart from client.username.

So what I'm doing is using CFPARAM to ceate a default value for username and use that as a criteria for the query,
But it still somehow manages to use the client.username variable. This si what Ive done to try an fix this problem but it doesnt work, do you have any more ideas?


<CFIF ISDEFINED ('Client.UserName')>
<CFPARAM NAME= &quot;usernametemp&quot; DEFAULT=&quot;&quot;>
</CFIF>
<!--- <CFOUTPUT>#deletingusername# CAN I C USERNAME? #deletingusername#</cfoutput> --->

<CFQUERY DATASOURCE= &quot;ONLINE&quot; NAME=&quot;Visitorsiteview&quot;>
SELECT *
FROM UserSpecific
WHERE Username= '#usernametemp#'
</CFQUERY>

<CFOUTPUT>
#Usernametemp#<BR>
#Visitorsiteview.columnlist#<BR>
#Visitorsiteview.currentrow#<BR>
#Visitorsiteview.recordcount#<BR>
</cfoutput>
 
Hey Sam,

I'm a little confused as that last code is different from the earlier code. What is the problem you're trying to solve at this point? In regards to the code above, the cfparam will always create usernameTemp and set the value to &quot;&quot;. If you use this in your query, I wouldn't think you would match any records since they would unlikely have a NULL username value.

GJ
 
Sam,

GunJack has been very helpful. Let him know by voting for him as TipMaster.

Ryan ;-]
 
Hi Ryan, Definately, GJ has been very help and patient with me whenever I have posted a question and has always pretty much got me out of all my stick situations!, theres no hesitation on my part to vote for GJ...Umm....How do I do it?? :)

GJ, You have been most helpful and I appreciate it. Thanx.
WIth regards to th CF Param thing, What I tried to do was to delete the client.username variable that was being picked up somehow (It was late and I was doing silly programming !). I think Ive deleted it now so Im not having that problem.
WHat I've done now is loop through the query and create a link to each record to a second page to view the particular username record, however when I pass the Var to the nxt page, the username variable is not being displayed therefore the query to search for a username variable (that should be passed is not being performed.

Below is the revised code:
Page 1:
<CFLOOP INDEX = &quot;client.username&quot; LIST=&quot;#GetClientVariablesList()#&quot;>
<CFSET deleted=deleteClientVariable(&quot;client.username&quot;)>
</cfloop>

<CFOUTPUT>#client.username#....If I cant see anything then Ive deleted the client variable<BR><BR></cfoutput>


<CFQUERY NAME =&quot;WebPageResults&quot; DATASOURCE=&quot;Online&quot;>
SELECT username, title,heading,section1heading1,section1text1,section2heading2,section2text2
FROM UserSpecific
WHERE title LIKE '%#Form.Criteria#%'
</cfquery>

<html>
<head>
<title>Webpage collection results</title>
</head>

<body>

<!--- <CFOUTPUT QUERY=&quot;WebPageResults&quot;> --->
<CFIF #form.criteria# is &quot;&quot;>
Im sorry, no results match the criteria you entered
<CFELSE>
<!--- <CFOUTPUT>#WebPageResults.username# user websites Found for &quot;#Form.Criteria#&quot;.<BR></cfoutput> --->

<CFLOOP QUERY=&quot;WebPageResults&quot;>
<CFOUTPUT>
<BR>A website created by #WebPageResults.username# was Found for the criteria &quot;#Form.Criteria#&quot;.<BR>

<FORM ACTION=&quot;WebSearchSiteView1.cfm?#username#=#webpageresults.username#&quot; METHOD=&quot;POST&quot;>
<INPUT TYPE=&quot;Hidden&quot; NAME=&quot;#username#&quot; Value=&quot;#username#&quot;>
<INPUT TYPE=&quot;submit&quot; VALUE=&quot;View&quot;>
</form>

</cfoutput>
</CFLOOP>
</cfif>

Im using hidden variables and within the input tags, the it displays both usernames (I have two username in the DB and I am verifying this by viewing &quot;Source&quot; in IE5.

But the action pae, I use a CFOUT to view #username# but it does not display anything. Heres the action page code snippet:
<cfoutput>&quot;#form.username#&quot;</CFOUTPUT>

<CFQUERY DATASOURCE= &quot;ONLINE&quot; NAME=&quot;Visitorsiteview&quot;>
SELECT *
FROM UserSpecific
WHERE Username= '#username#'
</CFQUERY>

<CFOUTPUT>
#Username#<BR>
#Visitorsiteview.columnlist#<BR>
#Visitorsiteview.currentrow#<BR>
#Visitorsiteview.recordcount#<BR>
</cfoutput>

<!--- Querying the database and displaying the webpage the user created on a previous session stored in the DB --->
<CFOUTPUT QUERY= &quot;Visitorsiteview&quot;>
<HTML>
<HEAD>
<TITLE>#title#</TITLE>
</HEAD>




 
Hey Sam,

I don't think you're really deleting the client variable so much as setting it to blank. If you've really deleted it, the line below

<CFOUTPUT>#client.username#....If I cant see anything then Ive deleted the client variable<BR><BR></cfoutput>

should generate an error because a truly deleted variable would not be defined. If it outputs the text &quot;... If I cant' see ...&quot; then it would indicate to me that client.username still exists but is set to NULL. To loop through and delete all the client variables, I think this is what you want.

<CFLOOP INDEX = &quot;x&quot; LIST=&quot;#GetClientVariablesList()#&quot;>
<CFSET deleted=deleteClientVariable(#x#)>
</cfloop>

I think your problem at this point is still that client.username is interferring with your other code but this time, it's simply blank.

Let me know if I'm missing something.
GJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top