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

Condition inside For...Next loop isn't executed

Status
Not open for further replies.

ChefSausage

Programmer
Oct 19, 2002
36
US
I have a For...Next loop around some HTML code for output, and just before the response.write line, I have an If condition to alter the output for one variable when the condition is met.

totalpages = 48, maxpage = 5 (just for your reference)

<%
startPage = 1
endPage = startPage + (maxPage - 1)
For i = 1 to totalpages %>
<option value=&quot;<%=i%>&quot;>
<%If endPage = totalpages Then
endPage = totalpages
End If
response.write(&quot;(&quot; & startPage & &quot;-&quot; & endPage & &quot;)&quot;)%> </option>
<%
startpage = startPage + 1
endPage = endPage + 1
Next
%>

The problem I'm having is each loop outputs startpage and endpage in a dropdown; the latter var being 4 higher than the former (ex: 1-5,2-6,3-7,etc). When endpage hits the value of 48 I want it to stop looping and only display 48. That is what the If condition is for. But, ASP is completely ignoring that If statement and continually loops, all the way up to 52. Am I missing something obvious b/c it seems very straight forward.
 
I don't think I quite get what your aiming for, your if statement is not doing anything right now because t is saying:
if a = b then set a = b

If you want to print something different for the option then include it in this if statement and put th current Response.Write into an else portion of that if statement.

-Tarwn

01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Actually, there is a slight tweak to that If condition. This is what it should be:

If a >= b then
a = b
End If

The value of 'a' will appear up until 'a' equals or exceeds the value of 'b', at which time I want the value of 'a' to always equal the value of 'b'. My problem is this If statement is being ignored entirely inside the loop. That is what is confusing me. The If condition is fine, but is pointless if I can't get ASP to read and execute those 3 lines.
 
Hmm, I made that change (and a couple cosmetic changes) as well as adding in the lastif statement, I don't see anything being skipped in your code:
Code:
<%
totalpages = 10
maxpage = 4

startPage = 1
endPage = startPage + (maxPage - 1)
For i = 1 to totalpages 
	'moved this to a Response.Write for efficiencies sak, escaping in and out of ASP is inefficient compared to a single Response.Write
	Response.Write(&quot;<option value=&quot;&quot;&quot; & i & &quot;&quot;&quot;>&quot;)

	If endPage >= totalpages Then
		endPage = totalpages
	End If

	'If we are at the last page, only show that number
	If startPage < totalpages Then
	    Response.Write(&quot;(&quot; & startPage & &quot;-&quot; & endPage & &quot;)</option>&quot; & vbCrLf) 'added carriage return line feed to make source easier to read
	Else
		Response.Write(&quot;(&quot; & startPage & &quot;)</option>&quot; & vbCrLf) 'added carriage return line feed to make source easier to read
	End if

startpage = startPage + 1
endPage = endPage + 1
Next
%>
Output:
Code:
<option value=&quot;1&quot;>(1-4)</option>
<option value=&quot;2&quot;>(2-5)</option>
<option value=&quot;3&quot;>(3-6)</option>
<option value=&quot;4&quot;>(4-7)</option>
<option value=&quot;5&quot;>(5-8)</option>
<option value=&quot;6&quot;>(6-9)</option>
<option value=&quot;7&quot;>(7-10)</option>
<option value=&quot;8&quot;>(8-10)</option>
<option value=&quot;9&quot;>(9-10)</option>
<option value=&quot;10&quot;>(10)</option>

Hopefully seeing it put together like this will help you find your error,

-Tarwn

01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Yes, it did work. And it's touched up a bit nicer, too. Thanks for the assistance.
 
u shld put the option tag outside the for loop. it is because since you include the <option> tag into the loop, it assume that you want to include the tag.

<option...>

for loop at here

if condition here

end loop

</option>

this shld solve your problem.
 
Oh, no. It's not working properly. Now it's really frustating.

I entered your code exactly the way you posted it. When I have 'totalpages' declared at the top like you do with an integer, it works fine. But, if I assign it's value to itself again (totalpages = totalpages), the loop doesn't work like you created it to be. It still loops past the totalpages value (to 52).

totalpages = 48 (assigned just above loop - works)
totalpages = totalpages (var. was assigned near top of page - doesn't work)
don't reassign value just above loop - doesn't work

*bangs head on desk*
 
ChefSausage, did u try my method?
another method will be putting the <option> tag in the if statement.


for loop at here

if condition here
<option...>
</option>

end if
end loop


this shld solve your problem.
 
Nope, that didn't do it. Still loops past 48. My new code:
*totalpages is assigned dynamically by a url value, but I'm
merely re-referencing it again in here.
<%
totalpages = totalpages
startPage = 1
endPage = startPage + (maxPage - 1)
For i = 1 to totalpages
If endPage >= totalpages Then
endPage = totalpages
End If
'If we are at the last page, only show that number
If startPage < totalpages Then
Response.Write(&quot;<option value=&quot;&quot;&quot; & i & &quot;&quot;&quot;>(&quot; & startPage & &quot;-&quot; & endPage & &quot;)</option>&quot; & vbCrLf)
Else
Response.Write(&quot;<option value=&quot;&quot;&quot; & i & &quot;&quot;&quot;>(&quot; & startPage & &quot;)</option>&quot; & vbCrLf)
End if

startpage = startPage + 1
endPage = endPage + 1
Next%>

The problem is, if totalpages isn't a static value it's not working. Now THAT freaks me out.
 
Try doing a cInt to the value coming from the form, querystring, whatever. That will force it to a numeric type and might help. I don't see why it would be over looping. Another alteration would be to drop the extra counters and simply use the i as your counter and page reference:
Code:
totalpages = cInt(Request.Form(&quot;myTotal&quot;)) 'or from recordset, querystring, etc

For i = 1 to totalpages
    If startPage >= totalpages - maxPage Then
        endPage = totalpages
    Else
        endPage = i + (maxPage - 1)
    End If
    'If we are at the last page, only show that number
    If startPage < totalpages Then
        Response.Write(&quot;<option value=&quot;&quot;&quot; & i & &quot;&quot;&quot;>(&quot; & i & &quot;-&quot; & endPage & &quot;)</option>&quot; & vbCrLf)
    Else
        Response.Write(&quot;<option value=&quot;&quot;&quot; & i & &quot;&quot;&quot;>(&quot; & i & &quot;)</option>&quot; & vbCrLf)
    End if
Next

This way you only have one counter and there should be no way for i to go over totalpages. Also the endPage value is set in your first if statement now, so that should never exceed totalPages either.

-Tarwn

01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Holy mother of God. The cInt() function worked for it. Many, many thanks. Now it works like it should.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top