Results 1 to 7 of 7

Thread: Help! Better 'if' statement needed.

  1. #1
    Join Date
    Aug 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help! Better 'if' statement needed.

    Hi All,

    Could someone please give me a better 'if' statement than the one I have. Basically I work on our intranet which is run on a 3rd party application using asp pages based on a sql database.

    I managed to get this script to run for the last thing I did:

    var getpobox=rs("std_postal_address").value;
    var getposub=rs("std_postal_suburb").value;
    var getpopcode=rs("std_postal_code").value;
    var getstate1=rs("std_state").value;
    var getstate2=conn.execute("select states_title from states_of_australia where states_code='" + getstate1 + "'")(0);


    if(getpobox && !getposub && !getpopcode && !getstate1){
    rs("po_address").value=getpobox + " ";
    }

    if(getpobox && getposub && !getpopcode && !getstate1){
    rs("po_address").value=getpobox + " " + getposub + " ";
    }

    if(getpobox && getposub && getpopcode && !getstate1){
    rs("po_address").value=getpobox + " " + getposub + " " + getpopcode + " ";
    }
    if(getpobox && getposub && getpopcode && getstate2){
    rs("po_address").value=getpobox + " " + getposub + " " + getpopcode + " " + getstate1 + " ";


    However, as you can see, this statement needs to check the 4 fields 4 times so I'm left with 16 different possibilities I need to check for!

    That was ok, but now I have another one I need to do with 256 possibilities and I really dont want to be typing it out (and thinking of all of them) this time.

    Can anyone help re-design this code or point me in an easier direction so that I dont need to think of 256 possible outcomes?

    Thanks in advance!

    Lost

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    if(getpobox && getposub && getpopcode && getstate2){
    I'm assuming you mean getstate1?

    Whenever you find yourself repeating code, it's a sure sign that you should be using a loop. Put them into an array first, then we can use some basic list-processing functions (map and filter) on them:
    Code:
    Array.prototype.map = function(f) {
      for(var i = 0, r = [], n = this.length; i < n; ++i)
        r.push(f(this[i], i));
    
      return r;
    };
    
    Array.prototype.filter = function(f) {
      if(!f)
        f = function(v) { return !!v; };
    
      for(var i = 0, r = [], n = this.length; i < n; ++i)
        if(f(this[i], r))
          r.push(this[i]);
    
      return r;
    };
    
    rs("po_address").value = ["postal_address", "postal_suburb", "postal_code", "state"].map(
      function(v) {
        return rs("std_" + v).value;
      }).filter().join(" ") + " ";
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Twey,

    No, unfortunately I did mean getstate2!!! It was the only way it would work believe it or not.

    Thank you so much for your prompt reply! I will try your way but with this 3rd party application you never know your luck.

    Thanks.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    In that case you actually have five values, not four, and we need to do something a bit special for getstate2:
    Code:
    var gs1 = rs("std_state").value,
      gs2 = conn.execute("select states_title from states_of_australia where states_code='" + gs1 + "'")(0);
    
    rs("po_address").value = ["address", "suburb", "code"].map(
      function(v) {
        return rs("std_postal_" + v).value;
      }).concat([gs1 && gs2]).filter().join(" ") + " ";
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Aug 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Twey,

    My deepest apologies for making you write out all this code. I was honestly only expecting an answer of "Whenever you find yourself repeating code, it's a sure sign that you should be using a loop." Not that I dont appreciate it very much because I do. However the actual code I am attempting to write this for is to show whether or not a date has expired with a Yes/No (with a Yes being Active and No being not Active) answer. On top of that I need to display it including the location it is active for or not.

    For example the text to be displayed would be
    Wor - Yes
    GGSS - No
    and to display nothing if there is no expiry date in the field.

    Putting into your script would be:

    Array.prototype.map = function(f) {
    for(var i = 0, r = [], n = this.length; i < n; ++i)
    r.push(f(this[i], i));

    return r;
    };

    Array.prototype.filter = function(f) {
    if(!f)
    f = function(v) { return !!v; };

    for(var i = 0, r = [], n = this.length; i < n; ++i)
    if(f(this[i], r))
    r.push(this[i]);

    return r;
    };

    rs("inc_ind_status").value = ["inc_induction_expiry", "inc_ind_exp_date_ggss", "inc_ind_exp_date_wor", "inc_ind_exp_date_ill", "inc_ind_exp_date_cas", "inc_ind_exp_date_casw", "inc_ind_exp_date_kwtp", "inc_ind_exp_date_kwrp" , "inc_ind_exp_date_ben", "inc_ind_exp_date_vwaho", "inc_ind_exp_date_bay", "inc_ind_exp_date_bun", "inc_ind_exp_date_gib", "inc_ind_exp_date_lug", "inc_ind_exp_date_wcwpo", "inc_ind_exp_date_gcdp"].map(
    function(v) {
    return rs("inc_ind_status" + v).value;
    }).filter().join(" ") + " ";

    However now I am unsure of where to set the conditions and how to display it using your code, or even if I can. As you can see I'm new to coding!

    Thank you again so much Twey for pointing me in the right direction it's very much appreciated.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    My deepest apologies for making you write out all this code.
    Two pasted functions and four lines?

    I'm not entirely sure of what you're asking here. Can you clarify? Last I saw you were just joining things together, now you're converting them to a boolean somehow? On what criteria?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Aug 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes, I know...I'm so sorry about that.

    Basically what I need to do is show in one field [inc_ind_status] whether or not an induction is still valid for a location. (ggss,wor,etc) It would appear as:
    GGSS - Yes (still valid)
    Wor - No (expired)

    The induction expiry's are held in a sql table as date fields.
    [inc_induction_expiry] - Noo exp date
    [inc_ind_exp_date_ggss] - GGSS exp date
    [inc_ind_exp_date_wor] - Wor exp date
    [inc_ind_exp_date_ill] - Ill exp date
    [inc_ind_exp_date_cas] - Cas exp date
    [inc_ind_exp_date_casw] - Casw exp date
    [inc_ind_exp_date_kwtp] - kwtp exp date
    [inc_ind_exp_date_kwrp] - kwrp exp date
    [inc_ind_exp_date_ben] - Ben exp date
    [inc_ind_exp_date_vwaho] - vwaho exp date
    [inc_ind_exp_date_bay] - bay exp date
    [inc_ind_exp_date_bun] - bun exp date
    [inc_ind_exp_date_gib] - gib exp date
    [inc_ind_exp_date_lug] - lug exp date
    [inc_ind_exp_date_wcwpo] - wcwpo exp date
    [inc_ind_exp_date_gcdp] - gcdp exp date

    So I need to check if any or all of the inc_ind_exp_x dates are earlier than todays date (it's expired) or later (it's valid) then display in the one field.

    Is this making any more sense?

Bookmarks

Posting Permissions

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