Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-04-2012, 04:29 AM   PM User | #1
sweetpeet
New to the CF scene

 
Join Date: Dec 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
sweetpeet is an unknown quantity at this point
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..
sweetpeet is offline   Reply With Quote
Old 12-04-2012, 04:49 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,751
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
sweetpeet (12-05-2012)
Old 12-04-2012, 05:19 AM   PM User | #3
sweetpeet
New to the CF scene

 
Join Date: Dec 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
sweetpeet is an unknown quantity at this point
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!
sweetpeet is offline   Reply With Quote
Old 12-04-2012, 10:48 AM   PM User | #4
idalatob
Regular Coder

 
Join Date: Sep 2007
Location: Grahamstown, South Africa
Posts: 237
Thanks: 6
Thanked 17 Times in 17 Posts
idalatob is on a distinguished road
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.
idalatob is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:37 PM.


Advertisement
Log in to turn off these ads.