PDA

View Full Version : Help! Better 'if' statement needed.



Lost
09-13-2007, 05:04 AM
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

Twey
09-13-2007, 06:20 AM
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:
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(" ") + " ";

Lost
09-13-2007, 06:32 AM
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.

Twey
09-13-2007, 06:47 AM
In that case you actually have five values, not four, and we need to do something a bit special for getstate2:
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(" ") + " ";

Lost
09-13-2007, 10:34 PM
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.

Twey
09-14-2007, 05:23 AM
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?

Lost
09-14-2007, 06:21 AM
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?