Page 1 of 1

how to simplify my code - I will have tooo many lines - plea

Posted: Wed Jan 21, 2009 9:21 am
by kabucek
hi all,

I have form in which people renew their agreements online.
they can renew up to 5 agreements.
I need to check if the expiration date is older than today, late fee will be added.
if exp. date is equal or newer - no late fee.
I have something like this so far and I can go further with this
but my question is, if there is a way to simplify this code a little bit,
or to make it shorter?

Code: Select all

 
 $Today=date('m/d/y');
 
 if  ($selectedProdCode="agreem" and $errorArray['agr1expdate'] < $Today)
          {
            $selectedProdCode=// code with 1 agr and late fee
            }
           else {    //1 agr
                  if  ( $selectedProdCode="agreem" and $errorArray['agr1expdate'] > $Today)
                    {
                      $selectedProdCode=// code with 1 agr and no late fee
                    }
                 else
                  {
                        if ($selectedProdCode="agreem" and $errorArray['agr1expdate'] < $Today)
                        {
                           $selectedProdCode=// code with 1 agr and late fee
                        }
                   }
                 else
                  {
                        if ($selectedProdCode="agreem" and $errorArray['agr1expdate'] == $Today)
                        {
                           $selectedProdCode=// code with 1 agr and no late fee
                        }
                   }
                   
               /////------------------------------------------------------------------------------------------------------
                   //2 agr
                     else
                  {
                        if ( $selectedProdCode="agreem2" and $errorArray['agr1expdate'] == $Today and $errorArray['agr2expdate'] == $Today )
                        {
                           $selectedProdCode=// code with 2 certs and no late fee
                        }
                   }
 
                   else
                  {
                        if ($selectedProdCode="agreem2" and $errorArray['agr1expdate'] > $Today and $errorArray['agr2expdate'] > $Today
                        {
                           $selectedProdCode=// code with 2 agr and no late fee
                        }
                   }
                   
                   else
                  {
                        if ($selectedProdCode="agreem2" and $errorArray['agr1expdate'] == $Today and $errorArray['agr2expdate'] > $Today
                        {
                           $selectedProdCode=// code with 2 agr and no late fee
                        }
                   }
                   
                   else
                  {
                        if ($selectedProdCode="agreemn2" and $errorArray['agr1expdate'] > $Today and $errorArray['agr2expdate'] == $Today
                        {
                           $selectedProdCode=// code with 2 certs and no late fee
                        }
                   }
                   
                   else
                  {
                        if ($selectedProdCode="agreem2" and $errorArray['agr1expdate'] < $Today and $errorArray['agr2expdate'] == $Today
                        {
                           $selectedProdCode=// code with 2 certs and plus 1x late fee
                        }
                   }
 
                   else
                  {
                        if ($selectedProdCode="agreem2" and $errorArray['agr1expdate'] == $Today and $errorArray['agr2expdate'] < $Today
                        {
                           $selectedProdCode=// code with 2 agr and plus 1x late fee
                        }
                   }
                   
                   else
                  {
                        if ($selectedProdCode="agreem2" and $errorArray['agr1expdate'] < $Today and $errorArray['agr2expdate'] > $Today
                        {
                           $selectedProdCode=// code with 2 certs and plus 1x late fee
                        }
                   }
                   
                   else
                  {
                        if ($selectedProdCode="agreem2" and $errorArray['agr1expdate'] > $Today and $errorArray['agr2expdate'] < $Today
                        {
                           $selectedProdCode=// code with 2 agr and plus 1x late fee
                        }
                   }
 





Thanks

Re: how to simplify my code - I will have tooo many lines - plea

Posted: Wed Jan 21, 2009 9:36 am
by requinix
For one, you can bring those {s back up a line.

Code: Select all

if ($selectedProdCode="agreem" and $errorArray['agr1expdate'] < $Today) {
$selectedProdCode=// code with 1 agr and late fee
}
If there's only one statement you don't even need the {}s

Code: Select all

if ($selectedProdCode="agreem" and $errorArray['agr1expdate'] < $Today)
$selectedProdCode=// code with 1 agr and late fee
Which also means that one statement could be on the line above it too.

Code: Select all

if ($selectedProdCode="agreem" and $errorArray['agr1expdate'] < $Today) $selectedProdCode=// code with 1 agr and late fee
But if you want to use the style you have now (totally your choice) look into the switch construct. It won't save you as many lines but the code will certainly look cleaner.

Re: how to simplify my code - I will have tooo many lines - plea

Posted: Wed Jan 21, 2009 9:37 am
by Burrito
please use PHP tags when posting code in the forums.

Re: how to simplify my code - I will have tooo many lines - plea

Posted: Wed Jan 21, 2009 9:55 am
by mintedjo
The way you are doing it you are retesting lots of variables when you don't need to.
Say we have 3 boolean variables a,b,c.
In total we have 2^3 possibilities which means we could do 8 if statements

Code: Select all

if(a  and b and c){//dostuff}
else if(a and b and NOT c){//dostuff}
else if(a and NOT b and c){//dostuff}... etc
If you look you notice that the value for a is true for 4 of the 8 possible combinations and within those we can apply the priciple to b.
All 8 possible combinations can be covered using this kindof layout instead of testing each variable every time.

Code: Select all

if(a){
    if(b){
        if(c){// a and b and c} 
        else {// a and b and NOT c}
    } else {
        if(c){// a and NOT b and c} 
        else {// a and NOT b and NOT c}
    }
} else {
    if(b){
        if(c){// NOT a and b and c} 
        else {// NOT a and b and NOT c}
    } else {
        if(c){// NOT a and NOT b and c} 
        else {// NOT a and NOT b and NOT c}
    }
}
It is also slightly more efficient. With 8 individual if statements testing every variable each time there is a chance that the computer has to check 24 values just to find out what to do. Using this nested approach the most comparisons required is 3.