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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kabucek
Forum Commoner
Posts: 50
Joined: Thu Sep 04, 2008 2:20 pm

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

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

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

Post by Burrito »

please use PHP tags when posting code in the forums.
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

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

Post 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.
Post Reply