Reputation: 245
Am I nuts or ignorant (both very possible) or does this code do nothing?
I am maintaining a legacy site written by a 3rd party company, so there is really no-one involved with the site that I can ask.
I understand they are looping through a recordset and getting the number of the last record. But I don't understand what the "if" block is doing. In psuedocode it seems to be saying "If I have a number and subtract from it that same number, after having divided it by four and then multiplied it by four and it does not equal zero...then"
When can that not equal zero (except when you generate a divide by zero error)? <-- ignore that, I was thinking backwards, you could get 0/4 but not 4/0. Rest of the question remains however.
The only thing I can think of is if the record number were negative? Is that possible, I don't claim to be an expert in ASP Classic but am not aware that could happen.
Anyone know what I am missing here?
do while not rs.EOF
recordNum = recordNum + 1
rs.MoveNext
loop
rs.MoveFirst
if recordNum-((recordNum\4)*4) > 0 then
recordNumber = (recordNum\4)+1
else
recordNumber = (recordNum\4)
end if
Upvotes: 4
Views: 401
Reputation:
Just to add to this conversation (even though it's been answered); the above 'algorithm' counts the number of 'pages' in the data and allows for the fact that there may be just over n pages but not quite n+1. Another way of doing this would be to read in a scalar value:
sqlString = "SELECT COUNT(id_field) AS total FROM table WHERE criteria = TRUE "
...
'Read data into array and pass to variable.
...
Dim maxPages
maxPages = recs \ 4 + (((recs \ 4) <> (recs / 4)) And 1)
Looping through the records, as per your post, Valis, is a huge waste of time.
Upvotes: 0
Reputation: 3111
it's a kind of pagination algorithm that does nothing meaningful, only wasting your cpu cycles, that IF in 100% of cases will result in 0
and btw: you will never reach a divide by zero exception, because divide 0 by 4 = 0.
Upvotes: 1
Reputation: 66398
First of all, as pointed out in a now deleted answer, recordNum\4
returns the integer portion of the division since the backslash operator in VBScript is used for Integer Division, while the ordiary slash (/) is used for floating point division.
Here are some sample values:
recordNum | recordNumber ----------|------------- 0 | 0 1 | 1 2 | 1 3 | 1 4 | 1 5 | 2 6 | 2 7 | 2 8 | 2 9 | 3
So this value is not the number of records, it rather answers questions like "how many groups of fours items I have"?
Now based on that, whoever wrote the code can then know how many table rows to display for example:
For x=1 To recordNumber
Response.Write("<tr>")
Response.Write("<td>...</td>")
Response.Write("<td>...</td>")
Response.Write("<td>...</td>")
Response.Write("<td>...</td>")
Response.Write("</tr>")
Next
Upvotes: 3