Search code examples
phpif-statementpromotion-code

Multiple Promo Codes


Complete n00b here so please be forgiving. I inherited a website and it has code in it to reduce the price of a one year membership by $10. I need to add another discount, "LEO" that would reduce the price by $15. I thought I could mimic the original code and everything would be OK but when I try it, it does not work properly. I'm hoping it is just a syntax thing?

Here is the original code:

  <tr>
    <td class='bottomborder'><div align='center'>
    <span id='bobcontent1-title'><input type='radio' name='MembershipTerm' class='join_form_bullet' value='";
      if($promocode == 'CIG9237'){echo("CigarFest Individual 1 Year");}
      else {echo("Individual 1 Year");}
      print"' checked='checked'></span>
    </div></td>
    <td class='bottomborder'>&nbsp;1 Year</td>
    <td class='bottomborder'>";
      if($promocode == 'CIG9237'){echo("$25");}
        else {echo("$35");}                                     
    print"</td>
    <td class='bottomborder'><span class='text_red'>";
    if($promocode == 'CIG9237'){echo("$10.00!");}
      else {echo(" ");}
    print"</span></td>
    <td class='bottomborder'>One Time Charge</td>
  </tr>

And here is what I tried without luck

  <tr>
    <td class='bottomborder'><div align='center'>
    <span id='bobcontent1-title'><input type='radio' name='MembershipTerm' class='join_form_bullet' value='";
      if($promocode == 'CIG9237'){echo("CigarFest Individual 1 Year");}
      if($promocode == 'LEO'){echo("Law Enforcement 1 Year");}
      else {echo("Individual 1 Year");}
      print"' checked='checked'></span>
    </div></td>
    <td class='bottomborder'>&nbsp;1 Year</td>
    <td class='bottomborder'>";
      if($promocode == 'CIG9237'){echo("$25");}
      if($promocode == 'LEO'){echo("$20");}
        else {echo("$35");}                                     
    print"</td>
    <td class='bottomborder'><span class='text_red'>";
    if($promocode == 'CIG9237'){echo("$10.00!");}
    if($promocode == 'LEO'){echo("$15.00!");}
      else {echo(" ");}
    print"</span></td>
    <td class='bottomborder'>One Time Charge</td>
  </tr>

When I add the LEO line then the pricing displays $25$35 for the original promo code of CIG9237 but seems to look correct for the LEO code.


Solution

  • Your first set of if statements isn't executing as you expect. The else condition is matching to the second if statement so if $promocode is 'CIG9237' then the first if statement executes and the else statement will execute too since $promocode is not equal to 'LEO'.

    Basically, where you have this:

    if($promocode == 'CIG9237'){echo("$25");}
    if($promocode == 'LEO'){echo("$20");}
    else {echo("$35");}
    

    You should have this:

    if($promocode == 'CIG9237'){echo("$25");}
    elseif($promocode == 'LEO'){echo("$20");}
    else {echo("$35");}
    

    To link all the statements together (now only one of the statements will be executed).

    And the same goes for further down, it should be this:

    if($promocode == 'CIG9237'){echo("$10.00!");}
    elseif($promocode == 'LEO'){echo("$15.00!");}
    else {echo(" ");}