2

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.

Barmar
  • 596,455
  • 48
  • 393
  • 495
Pedro
  • 23
  • 2
  • 2
    I don't think you can mix and match php code and html like this. I am not even sure if you're code is syntactically correct. What happens when you run it? Are you getting any error messages? Do you have error reporting turned on for your code ? – Maximus2012 Apr 17 '15 at 21:30
  • I think it's a copying error -- the first line looks like it's the middle of the argument to `echo`. – Barmar Apr 17 '15 at 21:32
  • @Maximus2012 The code supplied isn't complete - the html code is actually enclosed inside a `print` string, so it's ok. – Kvothe Apr 17 '15 at 21:56

3 Answers3

2

Change all the if($promocode == 'LEO') lines that you added to elseif($promocode == 'LEO').

What's happening is that the else clause after it is being run whenever the promo code is not LEO, instead of when neither of the promo codes matches.

Barmar
  • 596,455
  • 48
  • 393
  • 495
1

The second if statement will get executed even if the first condition is successful, in which case the condition of the second if statement echos the value in the else block. To avoid this, use an elseif statement instead. When an elseif is used, it will only be evaluated if the original condition in the first if statement is false.

Hazok
  • 4,687
  • 3
  • 33
  • 47
1

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(" ");}
Kvothe
  • 1,669
  • 2
  • 17
  • 34