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

Loop skipping records

Status
Not open for further replies.

DeZiner

Programmer
Joined
May 17, 2001
Messages
815
Location
US
My table 'Reports' is like so

record_number card_number amount Check
1 1 2
8 2 3
9 2 3


And the other 'MSA':

record_number card_number amount foundIt
a 1 2
b 2 3
c 4 5
d 2 3

I can loop through the first table records. I am finding matches in the second table. When found I mark the second table foundIt to Yes. My trouble, notice record_number 8 and 9 match b and d. Well when running the initial loop the Reports table skips 9 (because it matches 8?) therefore skips d.

Here is my code:
<html>
<head>




<!--- Get record count from Reports to loop through--->
<cfquery datasource=&quot;LinkPoint&quot; name=&quot;getcount&quot;>
select record_number
from Reports
</cfquery>
<!--- Set loop and start--->
<cfloop index=&quot;Idx&quot; From=1 TO=#getcount.RecordCount#>
<!---Get a row from the Reports table--->

<cfquery name=&quot;getReports&quot; datasource=&quot;LinkPoint&quot;>

SELECT *
FROM Reports
WHERE record_number=#Idx#
</cfquery>

<cfoutput query=&quot;getReports&quot;>
<cfset card=#card_number#>
<cfset money=#amount#>
<cfset row=#record_number#>

<!--- Now we have a row lets find it in MSA--->

<cfquery name=&quot;compare&quot; datasource=&quot;LinkPoint&quot;>

SELECT *
FROM MSA
Where card_number='#getReports.card_number#' AND amount='#getReports.amount#'

</cfquery>

</cfoutput>

<!--- If a record was returned let's mark it as found--->

<cfoutput query=&quot;compare&quot; maxrows=&quot;1&quot;>

<cfif #compare.RecordCount# GT 0 AND foundIt EQ &quot;&quot;>

<cfquery name=&quot;FoundIt&quot; datasource=&quot;LinkPoint&quot;>
update MSA
set foundIt = #getReports.record_number#
where record_number=#compare.record_number#
</cfquery>

<cfquery name=&quot;foundReport&quot; dataSource=&quot;LinkPoint&quot;>
update Reports
set Check = 'Yes'
WHERE record_number=#Idx#
</cfquery>
<cfelse>
</cfif>
</cfoutput>

</cfloop>

<head>
<body>
<cfoutput query=&quot;getcount&quot; maxRows=&quot;1&quot;>
<p>Rows To Loop Through: #getcount.recordCount#
</cfoutput>

<!---
<cfquery name=&quot;found_count&quot; datasource=&quot;LinkPoint&quot;>
SELECT *
FROM MSA
WHERE foundIt=&quot;Match&quot;
</cfquery>
<cfoutput query=&quot;found_count&quot; maxrows=&quot;1&quot;>
<p>Matches Found: #found_count.RecordCount#

</cfoutput>
--->
<cfquery name=&quot;results&quot; datasource=&quot;LinkPoint&quot;>
SELECT *
FROM MSA
</cfquery>

<ul>
<cfoutput query=&quot;results&quot;>

<li>#record_number# - #foundIt#
</cfoutput>
</ul>

<cfquery name=&quot;ReportsMatch&quot; datasource=&quot;LinkPoint&quot;>
SELECT *
FROM Reports
</cfquery>
<ul>
<cfoutput query=&quot;ReportsMatch&quot;>

<li>#record_number# - #Check#
</cfoutput>
</ul>

</body>
</html>


DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
Deziner,
Your initial loop is based on the number of records found in the Reports table. If your record numbers are not exactly the same, then it may not get to number 9. For example, lets say that it only finds 8 records total -- the record numbers might be 1,2,3,4,5,6,9,10. Notice that 7 and 8 were not found. Your total list length is eight and your loop is based on the lenghth of the records, not the actual record ID's themselves. (So it will only look for 1-8)

If you want to match the actual record number, try looping through a list of the ids instead. There is a quick way to get that using ValueList. Try using this for your initial loop:

<cfloop index=&quot;Idx&quot; List=&quot;#ValueList(getcount.Record_Number)#&quot;>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top