Jump to content

Complex If Statement that returns specific text frames


Recommended Posts

Hello,

 

Need some help with a project I am working on. We have 4 generic certificates that will go to specific states that are generic. There are also 4 certificates that could be state specific. On page 3 of the certificate there is one variable but depending on the state and certificate the text box location is different.

 

I am trying to create a rule that looks at the Cert ID first then specific states would have their own Text frame. Below is what I started with and is not working for what I want. The first if statement is working for the two Cert 1 text boxes but all the Cert 2 text boxes are still on and vice versa.

 

if (Field("Cert ID") == "1")

{

if ((Field("State") == "OH") || (Field("State") == "TX"))

FindTextFrame("Cert1_1").suppress = true;

else

 

FindTextFrame("Cert1_2").suppress = true;

}

else

{

 

if (Field("Cert ID") == "2")

{

if ((Field("State") == "ID") || (Field("State") == "IL"))

FindTextFrame("Cert2_1").suppress = true;

else

 

FindTextFrame("Cert2_2").suppress = true;

}

}

Link to comment
Share on other sites

I am trying to create a rule that looks at the Cert ID first then specific states would have their own Text frame. Below is what I started with and is not working for what I want. The first if statement is working for the two Cert 1 text boxes but all the Cert 2 text boxes are still on and vice versa.

There's a problem with the syntax of your if/else statement. Since you have the 'else' statement (in green), you don't need the extra 'if' statement (in red):

if (Field("Cert ID") == "1")
{
if ((Field("State") == "OH") || (Field("State") == "TX"))
       FindTextFrame("Cert1_1").suppress = true;
else

	FindTextFrame("Cert1_2").suppress = true;
}
[color="SeaGreen"]else
{[/color]

[color="Red"]if (Field("Cert ID") == "2")
{[/color]
if ((Field("State") == "ID") || (Field("State") == "IL"))
	 FindTextFrame("Cert2_1").suppress = true;
else

	FindTextFrame("Cert2_2").suppress = true;
[color="red"]}[/color]
[color="SeaGreen"]}[/color]

Or you could use 'else if':

if (Field("Cert ID") == "1") {
 if ((Field("State") == "OH") || (Field("State") == "TX"))
   FindTextFrame("Cert1_1").suppress = true;
 else
   FindTextFrame("Cert1_2").suppress = true;
} [color="Red"]else if[/color] (Field("Cert ID") == "2") {
 if ((Field("State") == "ID") || (Field("State") == "IL"))
   FindTextFrame("Cert2_1").suppress = true;
 else
   FindTextFrame("Cert2_2").suppress = true;
}

That being said, your current code only handles suppressing frames "Cert1_1" and "Cert1_2" when the "Cert ID" is "1." In other words, when the "Cert ID" is "2," your code doesn't try to suppress the Cert1 frames. I get the impression from your post that you want to suppress all Cert1 frames when the "Cert ID" is "2." If that's the case, you could try writing your rule like this:

FindTextFrame("Cert1_1").suppress = (Field("Cert ID") == "Cert2" || Field("State") == "OH" || Field("State") == "TX");
FindTextFrame("Cert1_2").suppress = (Field("Cert ID") == "Cert2" || Field("State") != "OH" || Field("State") != "TX");
FindTextFrame("Cert2_1").suppress = (Field("Cert ID") == "Cert1" || Field("State") == "ID") || (Field("State") == "IL");
FindTextFrame("Cert2_2").suppress = (Field("Cert ID") == "Cert1" || Field("State") != "ID") || (Field("State") != "IL");

 

You could also reduce some of that redundancy by determine which frame won't need to be suppressed and suppressing the rest using the forEach method:

['Cert1_1','Cert1_2','Cert2_1','Cert2_2']
 .forEach( function(s) { 
   var cert = Int(Field('Cert ID') - 1);
   var states = [ ['OH','TX'], ['ID','IL'] ][cert];
   var frame = 'Cert' + Field('Cert ID') + '_' + 
     ((states.indexOf(Field('State')) > -1) + 1);
   FindTextFrame(s).suppress = s != frame; 
 });

Link to comment
Share on other sites

Thanks Ste! You know your stuff. So trying to understand your last rule below. So if I want to include Cert3_1 for all generic states but Cert3_2 for only ID and IL. And Cert4_1 for all generic states but Cert4_2 for only IL and OK. How would I adjust? This should let me figure out the next page of variables. Thanks again!

 

['Cert1_1','Cert1_2','Cert2_1','Cert2_2']

.forEach( function(s) {

var cert = Int(Field('Cert ID') - 1);

var states = [ ['OH','TX'], ['ID','IL','OK'] ][cert];

var frame = 'Cert' + Field('Cert ID') + '_' +

((states.indexOf(Field('State')) > -1) + 1);

FindTextFrame(s).suppress = s != frame;

});

Link to comment
Share on other sites

So trying to understand your last rule below. So if I want to include Cert3_1 for all generic states but Cert3_2 for only ID and IL. And Cert4_1 for all generic states but Cert4_2 for only IL and OK. How would I adjust? This should let me figure out the next page of variables. Thanks again!

To better understand what the code is doing, you should first take a look at how the 'forEach' method works.

 

I'll try to explain how the code I wrote works:

We start with an array of all of your text frame names. The 'forEach' method passes each of the elements in that array (your frames) into the function as the variable 's,' though you could name it whatever you want. I just happen to be partial to the letter "S."

Within the function, I created the variable 'cert' which is just the "Cert ID" field less 1 and converted to an integer. Arrays are zero-indexed – meaning that the first element is at position '0', the second at position '1', etc. With that in mind, I use the 'cert' variable to return the 0th position of the 'states' array when "Cert ID" is "1" and the 1st position of the 'states' array when "Cert ID" is "2".

The 'states' array is an array of arrays. I'm using it to determine which list of states to use for a given "Cert ID." So to illustrate:

// Assuming 
//   Field("Cert ID") == '1'
//   Field("State") == 'OH'
var cert = Field('Cert ID'); // returns "1" (string)
cert = Int(cert);            // returns 1 (integer)
cert = cert - 1;             // returns 0 (integer)

/*
* Another way to write the 'states' array that might 
* make it a little clearer what's happening:
* 
* var states = [];            // [] (empty array)
* states[0] = ['OH','TX'];    // [['OH','TX']]
* states[1] = ['ID','IL'];    // [
*                             //    ['OH','TX'], // <-- Position 0
*                             //    ['ID','IL']  // <-- Position 1
*                             // ];
*/

var states = [ ['OH','TX'], ['ID','IL'] ][cert]; // [ ['OH','TX'], ['ID','IL'] ][0];
                                                // ['OH','TX']

 

With the correct list array of states assigned to the 'states' variable, I can use '.indexOf' which returns the position of an element in an array. So, in this case I'm searching the 'states' array for the value of the 'State' field. If the state exists in the array, the posistion is returned, if it's not found, -1 is returned.

I compare the value of the 'indexOf' method to -1 which gives us a boolean value (true/false). Adding 1 to the boolean will interpret it as a 1/0 instead of true/false. Thus: if the state exists in the 'states' array use the "_2" frame (suppressing everything else). Here's another illustration using the same values from earlier:

// Assuming 
//   Field("Cert ID") == '1'
//   Field("State") == 'OH'
var cert = Int(Field('Cert ID') - 1);                 // 0
var states = [ ['OH','TX'], ['ID','IL'] ][cert];      // ['OH','TX']

var frame = states.indexOf(Field('State'));           // 0 // <-- Notice that 'OH' is the 0th position of the states array
frame = (frame > -1);                                 // true
frame = (frame + 1);                                  // true + 1 == 1 + 1 == 2
frame = 'Cert' + Field('Cert ID') + '_' + frame;      // Cert1_2

And lastly, now that we know which frame is being used, it's just a matter of suppressing the ones that aren't that frame:

FindTextFrame(s).suppress = s != frame; 

 

So to answer your question regarding adding new variables, you'll need to add the new frames to the inital array. After that, you'd just assign the list of states to the corresponding position in the 'states' array. For example, if 'Cert3_2' should be used for "IL" and "ID," your 'states' array would be modified to look like this:

var states = [ ['OH','TX'], ['ID','IL'][color="Red"], ['ID','IL'][/color] ][cert]; 

Good luck!

Edited by step
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...