Go Back   CodingForums.com > :: Server side development > ASP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-20-2002, 01:31 PM   PM User | #1
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
half logic, half syntax

I don't think my brain is working right today, this is fairly straightforward but I can't get my head round it.

In an array, I have a list of costcodes that the currently logged-in user is associated with: they can sign off any orders that have that costcode.

I'm looping through a recordset of orders. Inside that loop, I then loop through the array, looking for a match between that order's costcode and one of the user's costcodes. If I find a match, I want to write an "Authorise" button" - if not, I don't.

It's not working because it doesn't break out of the if statement correctly. I'm aware of labeling statements and breaking out of them like that, and I think that's what I need to do - but can't find much reference on it. I'm doing this in JSCRIPT, by the way.

Here's what I've got, if anyone can help with the logic or pointing out where I'm going wrong with the syntax, I'd be grateful.

Cheers.


Code:
<%
	for(i=0;i<userCodes.length;i++){
		Inner:if((String(userCodes[i]))==(String(rs.Fields("costcode")))){
%>
			<td class="bodytext" valign="top"><input type="button" class="actionbutton" value="Authorise" onClick="authOrder('<%=rs.Fields("orderID")%>')"></td>
<%
		break Inner;
		}
		else{
%>
			<td class="bodytext" valign="top">&nbsp;</td>
<%
		break Inner;
		}
	}
%>
Spudhead is offline   Reply With Quote
Old 08-20-2002, 02:08 PM   PM User | #2
RadarBob
Regular Coder

 
Join Date: Jun 2002
Location: Round Rock, Texas
Posts: 443
Thanks: 0
Thanked 0 Times in 0 Posts
RadarBob is an unknown quantity at this point
Not sure 'zacly what you see your code doing but here's my 2 cents worth...

no, no, no, no... Don't convolute the code by forcing your way out - essentially a goto in the middle of a loop. Instead, build your loops so they control themselves. Some will say it's a matter of preference, but having dealt with this sort of thing in a major program re-write I did, I have to say from my experience it just makes code maintenance, modification, etc. much, much easier.
Code:
<%
var foundOne = new Boolean (false);
var i = 0;
while (i<userCodes.length || !foundOne){
   if((String(userCodes[i]))==(String(rs.Fields("costcode")))){
      foundOne = true;
%>
      <td class="bodytext" valign="top">
            <input type="button" class="actionbutton" value="Authorise" 
            onClick="authOrder('<%=rs.Fields("orderID")%>')"></td>
<%
   }else{
%>
      <td class="bodytext" valign="top"> </td>
<%
   }
   i++;
}// while()
%>
This code conveys much better what you are really trying to do vis-a-vis a doctored-up for loop. AND, it conforms to a basic tenant of structured programming - "enter at the top, exit at the bottom" (as opposed to breaking out somewhere in the middle)

Last edited by RadarBob; 08-20-2002 at 02:16 PM..
RadarBob is offline   Reply With Quote
Old 08-22-2002, 12:48 AM   PM User | #3
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
RadarBob, I didn't know there was any other way! I guess that's a good thing... lol (although I did cheat about 20 years ago in BASIC like that when I was a kid... probably not good practice); however select statements in ASP and switch() statements in other languages seem to be ok with breaking out of them... but of course that's not a loop - more like an if/then...

* Waiting for the REAL programmers to laugh at me now! *

__________________
Former ASP Forum Moderator - I'm back!

If you can teach yourself how to learn, you can learn anything. ;)

Last edited by whammy; 08-22-2002 at 12:51 AM..
whammy is offline   Reply With Quote
Old 08-22-2002, 03:31 PM   PM User | #4
RadarBob
Regular Coder

 
Join Date: Jun 2002
Location: Round Rock, Texas
Posts: 443
Thanks: 0
Thanked 0 Times in 0 Posts
RadarBob is an unknown quantity at this point
Quote:
however select statements in ASP and switch() statements in other languages seem to be ok with breaking out of them...
One does not get off on false technicalities! The design intent of the switch() is to jump around the remaining cases and on to the end (closing bracket) of the switch(). And you're right, it's not a loop. You changed the subject.

I'm talking about forcing your way out of a loop rather than having the loop logic do it. It's like saying (in code) "I'm doing this.." and in the middle of this, changing your mind. All your control logic should be stated up front as part of the IF(...), (chained together by &&, || as needed); not hidden inside various parts of the loop itself....

If you find yourself wanting to do that again, just created a well-named boolean that you can set, and test for that condition UP FRONT in the looping control. "Hear me now and believe me later", when you're dealing with a page-long loop you'll be glad you did. And you make testing a magitude easier. Trust me on this

BTW, after some reading on Boolean objects I'd change the while() like this:
Code:
while (i<userCodes.length || foundOne==false){
RadarBob is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:46 AM.


Advertisement
Log in to turn off these ads.