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

loop within a loop 1

Status
Not open for further replies.

twcman

Programmer
Joined
Jun 28, 2001
Messages
284
Location
US
I have a section of code that loops through a query of user information and displays to a page. Within this loop is a couple of text boxes and select boxes. The text display's fine. It is the select boxes that display the value of the last record in the query. I think I have scoped the variables correctly but a snippet of code is below:

<cfloop query=&quot;findaddress&quot;>
<cfoutput>
<table border=&quot;1&quot; align=&quot;center&quot;>
<tr><td>

<!-- form that preloads with address information -->
<form action=&quot;listupdate.cfm?#URLToken#&action=updateaddress&quot; name=&quot;form#findaddress.currentrow#&quot; method=&quot;post&quot;>
<input type=&quot;Hidden&quot; name=&quot;addressid&quot; value=&quot;#findaddress.addressid#&quot;>
<input type=&quot;Hidden&quot; name=&quot;mainid&quot; value=&quot;#URL.mainid#&quot;>
<table align=&quot;center&quot; border=&quot;0&quot;>
<tr>
<td align=&quot;center&quot;>Type<BR>
<SELECT name=&quot;descaddid&quot; size=&quot;1&quot;>

<cfloop query=&quot;finddesc&quot;>
<option value=#finddesc.descaddid# <cfif #findaddress.descaddid# EQ #finddesc.descaddid#>SELECTED</cfif>>
#finddesc.descadd#</option>
</cfloop>

</select>

</td>
<td align=&quot;center&quot; colspan=&quot;2&quot;>Address<br><input type=&quot;Text&quot; name=&quot;address1&quot; value=&quot;#address1#&quot;> </td>
<td align=&quot;center&quot;>C/O<BR><input type=&quot;Text&quot; name=&quot;co&quot; value=&quot;#co#&quot; </td>
</tr>
<tr>
<td>City<BR>
<SELECT name=&quot;cityid&quot; size=&quot;1&quot;>

<cfloop query=&quot;findcity&quot;>
<option value=&quot;#findcity.cityid#&quot; <cfif #findcity.cityid# EQ #findaddress.cityid#>SELECTED</cfif>>
#findcity.city#</option>
</cfloop>
</select>
</td>


This code was designed to create a form for each record that when submitted it will update the information in that record.

I am also having a secondary problem. When I update or add (not shown) with this form, I use a cflocation after the update to send the user back to the original display page to view the changes. Problem is even though the query is being rerun, I have to refresh the page to see the results.
 
-> you don't say WHAT is not working for the loops (or i haven't understood)
-> the 2nd problem is a caching problem, there have been many threads here on this problem, i'd suggest that you first perform a search in the cf forum - and then in the javascript forum
 
When you put a loop inside a loop, ColdFusion Starts the previous loop all over again. So what you need to do is catch the current row of the loop before entering the second loop, and then pass it to the second loop.

<cfloop query=&quot;findaddress&quot;>
<cfset i = qrec.currentrow>
<cfoutput>
[COLOR=008080]<table border=&quot;1&quot; align=&quot;center&quot;>[/color]
[COLOR=008080]<tr>[/color][COLOR=008080]<td>[/color]

[COLOR=666666]<!-- form that preloads with address information -->[/color]
[COLOR=000080][COLOR=FA5000]<form action=&quot;listupdate.cfm?#URLToken#&action=updateaddress&quot; name=&quot;form#findaddress.currentrow#&quot; method=&quot;post&quot;>[/color][/color]
[COLOR=000080][COLOR=FA5000]<input type=&quot;Hidden&quot; name=&quot;addressid&quot; value=&quot;#findaddress.addressid#&quot;>[/color][/color]
[COLOR=000080][COLOR=FA5000]<input type=&quot;Hidden&quot; name=&quot;mainid&quot; value=&quot;#URL.mainid#&quot;>[/color][/color]
[COLOR=008080]<table align=&quot;center&quot; border=&quot;0&quot;>[/color]
[COLOR=008080]<tr>[/color]
[COLOR=008080]<td align=&quot;center&quot;>[/color]Type[COLOR=000080]<BR>[/color]
[COLOR=000080][COLOR=FA5000]<SELECT name=&quot;descaddid&quot; size=&quot;1&quot;>[/color][/color]

<cfloop query=&quot;finddesc&quot;>
[COLOR=000080][COLOR=FA5000]<option value=#finddesc.descaddid# <cfif findaddress.descaddid
Code:
[
i
Code:
]
EQ finddesc.descaddid>
[/color][/color]SELECTED</cfif>>
#finddesc.descadd#[COLOR=000080][COLOR=FA5000]</option>[/color][/color]
</cfloop>

[COLOR=000080][COLOR=FA5000]</select>[/color][/color]

[COLOR=008080]</td>[/color]
[COLOR=008080]<td align=&quot;center&quot; colspan=&quot;2&quot;>[/color]Address[COLOR=000080]<br>[/color][COLOR=000080][COLOR=FA5000]<input type=&quot;Text&quot; name=&quot;address1&quot; value=&quot;#address1#&quot;>[/color][/color] [COLOR=008080]</td>[/color]
[COLOR=008080]<td align=&quot;center&quot;>[/color]C/O[COLOR=000080]<BR>[/color][COLOR=000080][COLOR=FA5000]<input type=&quot;Text&quot; name=&quot;co&quot; value=&quot;#co#&quot; [COLOR=008080]</td>[/color][/color][/color]
[COLOR=008080]</tr>[/color]
[COLOR=008080]<tr>[/color]
[COLOR=008080]<td>[/color]City[COLOR=000080]<BR>[/color]
[COLOR=000080][COLOR=FA5000]<SELECT name=&quot;cityid&quot; size=&quot;1&quot;>[/color][/color]

<cfloop query=&quot;findcity&quot;>
[COLOR=000080][COLOR=FA5000]<option value=&quot;#findcity.cityid#&quot; <cfif findcity.cityid EQ findaddress.cityid
Code:
[
i
Code:
]
>
[/color][/color]SELECTED</cfif>>
#findcity.city#[COLOR=000080][COLOR=FA5000]</option>[/color][/color]
</cfloop>

[COLOR=000080][COLOR=FA5000]</select>[/color][/color]
[COLOR=008080]</td>[/color] - tleish
 
tleish,
Great information. What you say makes sense. Could you explain the use of qrec.currentrow? When I insert this code it generates an error. What did I miss?
 
My mistake, qrec.currentrow should be findaddress.currentrow, the name of your query

query_name.CurrentRow
The current row of the query being processed by CFOUTPUT or CFLOOP.

<cfloop query=&quot;findaddress&quot;>
<cfset i = findaddress.currentrow>
<cfoutput>
[COLOR=008080]<table border=&quot;1&quot; align=&quot;center&quot;>[/color]
[COLOR=008080]<tr>[/color][COLOR=008080]<td>[/color]

[COLOR=666666]<!-- form that preloads with address information -->[/color]
[COLOR=000080][COLOR=FA5000]<form action=&quot;listupdate.cfm?#URLToken#&action=updateaddress&quot; name=&quot;form#findaddress.currentrow#&quot; method=&quot;post&quot;>[/color][/color]
[COLOR=000080][COLOR=FA5000]<input type=&quot;Hidden&quot; name=&quot;addressid&quot; value=&quot;#findaddress.addressid#&quot;>[/color][/color]
[COLOR=000080][COLOR=FA5000]<input type=&quot;Hidden&quot; name=&quot;mainid&quot; value=&quot;#URL.mainid#&quot;>[/color][/color]
[COLOR=008080]<table align=&quot;center&quot; border=&quot;0&quot;>[/color]
[COLOR=008080]<tr>[/color]
[COLOR=008080]<td align=&quot;center&quot;>[/color]Type[COLOR=000080]<BR>[/color]
[COLOR=000080][COLOR=FA5000]<SELECT name=&quot;descaddid&quot; size=&quot;1&quot;>[/color][/color]

<cfloop query=&quot;finddesc&quot;>
[COLOR=000080][COLOR=FA5000]<option value=#finddesc.descaddid# <cfif findaddress.descaddid
Code:
[
i
Code:
]
EQ finddesc.descaddid>
[/color][/color]SELECTED</cfif>>
#finddesc.descadd#[COLOR=000080][COLOR=FA5000]</option>[/color][/color]
</cfloop>

[COLOR=000080][COLOR=FA5000]</select>[/color][/color]

[COLOR=008080]</td>[/color]
[COLOR=008080]<td align=&quot;center&quot; colspan=&quot;2&quot;>[/color]Address[COLOR=000080]<br>[/color][COLOR=000080][COLOR=FA5000]<input type=&quot;Text&quot; name=&quot;address1&quot; value=&quot;#address1#&quot;>[/color][/color] [COLOR=008080]</td>[/color]
[COLOR=008080]<td align=&quot;center&quot;>[/color]C/O[COLOR=000080]<BR>[/color][COLOR=000080][COLOR=FA5000]<input type=&quot;Text&quot; name=&quot;co&quot; value=&quot;#co#&quot; [COLOR=008080]</td>[/color][/color][/color]
[COLOR=008080]</tr>[/color]
[COLOR=008080]<tr>[/color]
[COLOR=008080]<td>[/color]City[COLOR=000080]<BR>[/color]
[COLOR=000080][COLOR=FA5000]<SELECT name=&quot;cityid&quot; size=&quot;1&quot;>[/color][/color]

<cfloop query=&quot;findcity&quot;>
[COLOR=000080][COLOR=FA5000]<option value=&quot;#findcity.cityid#&quot; <cfif findcity.cityid EQ findaddress.cityid
Code:
[
i
Code:
]
>
[/color][/color]SELECTED</cfif>>
#findcity.city#[COLOR=000080][COLOR=FA5000]</option>[/color][/color]
</cfloop>

[COLOR=000080][COLOR=FA5000]</select>[/color][/color]
[COLOR=008080]</td>[/color] - tleish
 
thank you for your help. It saved me alot of time.

chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top