CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Need help with this PHP code (http://www.codingforums.com/showthread.php?t=283508)

sweetpeet 12-04-2012 04:29 AM

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.

Fou-Lu 12-04-2012 04:49 AM

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:

switch ($Add_on1)
{
    case 
'plain only-$0.95':
        print 
0.95;
        break;
    
//...
    
case 'Chicken with egg & cheese - $2.50':
        print 
2.5;
        break;


And so forth.
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:

$aParts explode('-'$Add_on1);
$sDescription trim($aParts[0]);
$dPrice trim($aParts[1], ' $'); 

Now you can pick the description and price out of the $Add_on1 without needing to if/elseif or switch them.

sweetpeet 12-04-2012 05:19 AM

Quote:

Originally Posted by Fou-Lu (Post 1297061)
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:

switch ($Add_on1)
{
    case 
'plain only-$0.95':
        print 
0.95;
        break;
    
//...
    
case 'Chicken with egg & cheese - $2.50':
        print 
2.5;
        break;


And so forth.
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:

$aParts explode('-'$Add_on1);
$sDescription trim($aParts[0]);
$dPrice trim($aParts[1], ' $'); 

Now you can pick the description and price out of the $Add_on1 without needing to if/elseif or switch them.

I just now got it to work with the === but thanks for the help. ill more than likely use your method in the future!

thanks!

idalatob 12-04-2012 10:48 AM

Although this is already resolved, I thought I would chime in with some advice as to how I would solve it.

PHP Code:

/**
* A list of addOns available and the associated price
*/
$addOns = array("ham and eggs"=> 1.5,
                       
"eggs and ham" => 2.0,
                       
"spam spam spam" => 0);

$addOn trim($_POST['Breakfast_Biscuit_Add_on']);

if (isset(
$addOns[$addOn])) {
    echo(
"$" $addOns[$addOn]);
} else {
   echo(
"Please select an addOn");


The benifit of this, is you dont have to add a new switch/else if statement whenever a new addon is available.


All times are GMT +1. The time now is 02:27 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.