Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts

    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;
    		}
    	}
    %>

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    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
    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! *

    Last edited by whammy; 08-22-2002 at 12:51 AM.
    Former ASP Forum Moderator - I'm back!

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

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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){


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •