Home > database >  About getting sum of price
About getting sum of price

Time:03-16

Hi I am a beginner who just stated to study php.I found the question pizza making website from random webpage and I am trying to do that. I made my code for pizza and I want to get the sum of my pizza toppings price, however the price is not following the result. I guess the code $total_price = $topping_one $topping_two; below bacon => 3.5 is the problem, but I have no Idea with that. The result is coming out as Array. Could some one give me some tip for my code?

<?php 


$toppings_array = array(
"Pepperoni" => 3,
"Mushrooms" => 2,
"Onions" => 2,
"Sausage" => 4,
"Bacon" => 3.5 );

$total_price = $topping_one   $topping_two;

if($_SERVER["REQUEST_METHOD"] == "POST")
enter code here



if(isset($_POST['topping_one'])){
     $topping_one = $_POST['topping_one'];
     foreach ($toppings_array as $topping => $price) {

          if($_POST['topping_one'] == $topping)
          {
              $total_price= $total_price   $price;
          }
        }
        }
else{
    $err = "Topping is not selected. ";
}


if(isset($_POST['topping_two'])){
     $topping_one = $_POST['topping_two'];
     foreach ($toppings_array as $topping => $price) {

          if($_POST['topping_one'] == $topping)
          {
              $total_price= $total_price   $price;
          }
        }
        }
else{
    $err = "Topping is not selected. ";
}

if(err==0)
{
if(err==0)
{
    echo "<h2>Total price is:".$total_price." €" ;
}
else{
    echo "<h2>".$err."</h2>";
}

}

?>

CodePudding user response:

replace this:

$total_price = $topping_one   $topping_two;

with:

$total_price = 0;

P.S: remove "enter code here" under checking the $_SERVER method.

use $err instead of err!

please note that your posted data have to be Case Sensitive

OR replace the total code with code below:

<?php 

$toppings_array = array(
"Pepperoni" => 3,
"Mushrooms" => 2,
"Onions" => 2,
"Sausage" => 4,
"Bacon" => 3.5 );

$total_price = 0;
$err = '';
if($_SERVER["REQUEST_METHOD"] == "POST"){

if(isset($_POST['topping_one'])){
     $topping_one = $_POST['topping_one'];
     foreach ($toppings_array as $topping => $price) {

          if($topping_one == $topping)
          {
              $total_price  =$price;
          }
        }
        }
else{
    $err .= "Topping is not selected. ";
}


if(isset($_POST['topping_two'])){
     $topping_two = $_POST['topping_two'];
     foreach ($toppings_array as $topping2 => $price2) {

          if($topping_two == $topping2)
          {
              $total_price  = $price2;
          }
        }
        }
else{
    $err .= PHP_EOL."Topping is not selected. ";
}

if(!$err)
{
    echo "<h2>Total price is:".$total_price." €" ;
}
else{
    echo "<h2>".$err."</h2>";
}

}

?>

CodePudding user response:

You have too much code and it will be unwieldy if you add more toppings. It would be easier for you and the user if you use checkboxes for the toppings. Define an array of checkboxes with the topping as the key:

<input type="checkbox" name="toppings[Bacon]">  Bacon
<input type="checkbox" name="toppings[Sausage]"> Sausage
etc...

Then compute the intersection with your price array and sum what remains. So all that is needed:

if(!empty($_POST['toppings'])) {
    $total_price = array_sum(array_intersect_key($toppings_array, $_POST['toppings']));
} else {
    $err .= "Topping is not selected. ";
}
  •  Tags:  
  • php
  • Related