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

If statement in Loop not working

Status
Not open for further replies.

mpnut

MIS
Aug 27, 2003
242
Can anyone tell me why this isn't working. What is supposed to happen is a button is clicked and the page is should highlight in red the row that was clicked. For some reason I can't get it to work.

Here is the script:

<body bgcolor="#CCCCCC">
<h1>TEST</h1>
<table>
<%
dim cont
dim good
dim nbr
dim nmbr
cont=10%>

<%
good=Request.Form("nmbr")
%>

<%nbr=0%>
<%Do Until nbr=10%>
<%nbr=nbr+1%>
<%
IF good=nbr THEN
colr="#FF0000"
ELSE
colr="#CCCCCC"
END IF
%>
<tr bgcolor=""<%=colr%>"">
<td><form action="Test.asp" method="post" style="margin:0px; padding:0px;"><input type="submit" value="View" /><input type="hidden" name="nmbr" value="<%=nbr%>" /></form></td><td><%=nbr%></td><td>TEST</td>
</tr>
<%Loop%>
</table>


</body>

What am i missing?
 
I think this is a data type problem.

Request.Form returns a string. nbr appears to be an integer. when comparing strings to integers, it's best to specifically convert them for the comparison.

Ex:

IF [!]cInt([/!]good[!])[/!]=nbr THEN

or

If [!]Trim([/!]good[!])[/!] = [!]Trim([/!]nbr[!])[/!] Then

Personally, I prefer the first method (converting to int). Unfortunately, this could error if the value of good cannot be converted to an integer.

Make sense?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
<tr bgcolor=""<%=colr%>"">
[tt]<tr bgcolor=[highlight]"[/highlight]<%=colr%>[highlight]"[/highlight]>[/tt]
 
Thank you for getting back so quickly. Yes, that makes total sense. I tried both of your examples, however, and neither seems to make a difference. To make sure, I added a "good=good+1" and then did a response.write and good returned a value of 1 + the button I clicked (if i clicked button 4, it returned 5). So I assume that means "good" is an integer.
 
tsuji
Thanks for the response. I accidently left that in there. I had been trying it with single quotes, double quotes and no quotes. I just happen to copy and paste when i had the double quotes in there. None of which made a lick of difference.
 
Ok. I'm not sure what this means, but I'm guessing it means that "good" was NOT being seen as an integer. I added a line directly after "good=Request.Form("nmbr")" that reads "good=good+0" and it seems to work fine now. If someone can explain it to me I'd appreciate it.
 
I prefer to do a LOT of data validation. It's a good habit to get in to.

<%
If IsNumeric(Request.Form("nmbr") & ".0e0") Then
good=cInt(Request.Form("nmbr"))
Else
good = 0
End If
%>

Obviously, you expect Request.Form("nmbr") to be an integer. As such, my recommendation would be to check it for numeric. If it is, then convert it to integer right from the beginning. If it is not numeric, then set it to some default value (in my example, the default is set to 0).

Aside:
IsNumeric will return to if the string can be converted to any numeric type, so fractional numbers will return true, and so will scientific notation. All of the following will return true from IsNumeric:

1
1e4
2.8

If you want to ONLY allow integers, then adding .0e0 before checking the value will force the function to return true ONLY for integers.

Make sense?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
That is cint() meant for (convert the string to an integer data type) before doing the comparision.
>If someone can explain it to me I'd appreciate it.
But that was explained clearly by gmmastros - you just have to read it.
 
Thank you. Yes now that makes perfect sense. tsuji, I didn't understand how using "cInt(good)" didn't work, but how "good=good+0" did. Please keep in mind that what may be clear to you is not clear to everyone. You have a lot more experience than I do, so a little more explanation was needed.
 
Okay, don't worry. You still need to read gmmastros - that's what I meant.

But I can add an oddity/digression as a supplementary note for the general interest. If one is familiar with window script host, one will not miss to acknowledge that the comparison operation will trigger automatically a type conversion to the best-guess of the vbs engine. Hence a standalone vbscript will do this.
[tt]
'wsh vs iis-script-host vbs engine
good="111"
if good=111 then
'here true : wsh does this
else
'here false : iis-script-host does this
end if
[/tt]
But vbs engine as served on the iis server, this conversion is not happening (as explained request.form(...) results always in string subtype. Comparison (=)won't automatically initiates a best-guess coersion of data type. Whereas when the engine sees an arithmetic operator (+), say, good+0, the conversion does happen on the server host as well as the windows script host. Hence, for comparsion operation, on the server-side, conversion does need be explicitly enforced mostly.

That's my little note.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top