Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.
    Last edited by sweetpeet; 12-04-2012 at 04:44 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    sweetpeet (12-05-2012)

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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!

  • #4
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •