![]() |
Need help with this PHP code
I'm running a PHP code, it is using if and else if but for some reason it only displays the first if statement even when I change the source selection.
the source comes from an html form method=post action=.../the php file.php heres what I have on the PHP file: (the reason that I have the 2 sets of code separated is b/c they are in a HTML table and are in diferent cells) <?php $Add_on1 = $_POST['Breakfast_Biscuit_Add_on']; ?> <?php if($Add_on1 = "plain only-$0.95"): $P1 = "0.95"; echo "" . $P1 . " "; elseif($Add_on1 = "plain with egg & cheese - $1.25"): $P1 = "1.25"; echo "" . $P1 . " "; elseif($Add_on1 = "Ham only - $1.50"): $P1 = "1.50"; echo "" . $P1 . " "; elseif($Add_on1 = "Ham with egg - $1.75"): $P1 = "1.75"; echo "" . $P1 . " "; elseif($Add_on1 = "Ham with cheese - $1.75"): $P1 = "1.75"; echo "" . $P1 . " "; elseif($Add_on1 = "Ham with egg - $1.75"): $P1 = "1.75"; echo "" . $P1 . " "; elseif($Add_on1 = "Ham with egg & cheese - $2.00"): $P1 = "2.00"; echo "" . $P1 . " "; elseif($Add_on1 = "Susage only - $1.50"): $P1 = "1.50"; echo "" . $P1 . " "; elseif($Add_on1 = "Sausage with egg - $1.75"): $P1 = "1.75"; echo "" . $P1 . " "; elseif($Add_on1 = "Sausage with cheese"): $P1 = "1.75"; echo "" . $P1 . " "; elseif($Add_on1 = "Sausage with egg & cheese - $2.00"): $P1 = "2.00"; echo "" . $P1 . " "; elseif($Add_on1 = "Bacon only - $1.50"): $P1 = "1.50"; echo "" . $P1 . " "; elseif($Add_on1 = "Bacon with egg - $1.75"): $P1 = "1.75"; echo "" . $P1 . " "; elseif($Add_on1 = "Bacon with cheese - $1.75"): $P1 = "1.75"; echo "" . $P1 . " "; elseif($Add_on1 = "Bacon with egg & cheese - $2.00"): $P1 = "2.00"; echo "" . $P1 . " "; elseif($Add_on1 = "Steak only - $2.00"): $P1 = "2.00"; echo "" . $P1 . " "; elseif($Add_on1 = "Steak with cheese - $2.15"): $P1 = "2.15"; echo "" . $P1 . " "; elseif($Add_on1 = "Steak with egg - $2.15"): $P1 = "2.15"; echo "" . $P1 . " "; elseif($Add_on1 = "Steak with cheese & egg - $2.50"): $P1 = "2.50"; echo "" . $P1 . " "; elseif($Add_on1 = "Chicken only - $2.00"): $P1 = "2.00"; echo "" . $P1 . " "; elseif($Add_on1 = "Chicken with cheese - $2.15"): $P1 = "1.75"; echo "" . $P1 . " "; elseif($Add_on1 = "Chicken with egg - $2.15"): $P1 = "2.15"; echo "" . $P1 . " "; elseif($Add_on1 = "Chicken with egg & cheese - $2.50"): $P1 = "2.50"; echo "" . $P1 . " "; elseif($Add_on1 = "Select One"): echo "Please choose an add on. "; endif; ?> let me know if anyone can come up with a solution. I have also tried changine the equals sign to == and === and got no change. |
No, there's no limit to the number of elseif statements beyond memory usage. Which is a lot.
The problem is this: if($Add_on1 = "plain only-$0.95"):, these are assignments, not comparisons. To compare use == or the strcmp function. As soon as you assign, it automatically is successful so only the first ever triggers and $Add_on1 is assigned this value.Using if/else with this many though would probably be easier to use a switch statement: PHP Code:
Depending on overall rules, you can use explode to break the text from the dollar amount by using the hyphen as the delimiter. PHP Code:
|
Quote:
thanks! |
Although this is already resolved, I thought I would chime in with some advice as to how I would solve it.
PHP Code:
|
| All times are GMT +1. The time now is 02:27 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.