Jump to content

[SOLVED] please, i need help with selecting items and displaying them with prices?


djcrisp22

Recommended Posts

ok, i am doing this to learn PHP.  ok, now user has four choices available to select and if they select one choice and it should display the price for that chioce.  if user selects multiple choices then the total should be those choices.  how can i fix this.  for example, if a user selects pepperoni and the total should be for pepperoni.  or if user selects beef and pepperoni, then the total should be beef and pepperoni.  any help is appreciated. thanks in advance :-)

 

 

php code

 

<html>
<head>
<title>Pizza Hut</title>
</head>
<body>
<h3>Pizza Hut</h3>


<?

$pepperoni = '1.50';
$beef = '2.00';
$sausage = '1.25';
$olives = '1.75';


$total = 0;

if (!empty($pepperoni)){
print ("You chose Pepperoni <br> \n");
$total = $total + $pepperoni;
} // end if 

if (!empty($beef)){
print ("You chose Beef <br> \n");
$total = $total + $beef;
} // end if

if (!empty($sausage)){
print ("You chose Sausage <br> \n");
$total = $total + $sausage;
} // end if


if (!empty($olives)){
print ("You chose Olives <br> \n");
$total = $total + $olives;
} // end if

print "The total cost is \$$total \n";

?>

</body>
</html>

---------------------

here is my html code

 

 

<html>
<head>
<title>Pizza Hut</title>
</head>
<body>

<h1>Pizza Hut</h1>



<form action="checkdemo.php">

<h3>What would you like with your order?</h3>

<ul>
  <li><input type="checkbox" name="pepperoni" value="1.50">Pepperoni</li>

  <li><input type="checkbox" name="beef" value="2.00">Beef</li>

  <li><input type="checkbox" name="sausage" value="1.25">Sausage</li>

  <li><input type="checkbox" name="olives" value="1.75">Olives</li>
</ul>

<input type="submit">

</form>
</body>
</html>




 

 

#1) In your HTML, remove the value="###" part, as it is note needed.

 

#2) In your PHP, lines like this:

if (!empty($pepperoni)){

should be

if (!empty($_GET['pepperoni'])){

 

i did what you asked me to do.  now what?  i ran the script. but still it gives me $0 amount.  what am i doing wrong?!!!

or

 

<?php
if ($_POST) {
    foreach ($_POST as $item => $price)
    {
        echo "$item : $price<br/>";
    }
    echo "Total price : " . number_format(array_sum($_POST), 2);
} 
?>
<html>
<head>
<title>Pizza Hut</title>
</head>
<body>

<h1>Pizza Hut</h1>



<form action="" method="POST">

<h3>What would you like with your order?</h3>

<ul>
  <li><input type="checkbox" name="pepperoni" value="1.50">Pepperoni</li>

  <li><input type="checkbox" name="beef" value="2.00">Beef</li>

  <li><input type="checkbox" name="sausage" value="1.25">Sausage</li>

  <li><input type="checkbox" name="olives" value="1.75">Olives</li>
</ul>

<input type="submit">

</form>
</body>
</html>

never mind. i figured it out!.  just had to concentrate harder and play with it.

 

<html>
<head>
<title>Pizza Hut</title>
</head>
<body>

<?php
$pepperoni = 2.85;
$beef = 1.75;
$sausage = 1.50;
$olives = 1.25;

$total = 0;

if (isset($_POST['pepperoni'])){
print ("You chose Pepperoni <br> \n");
$total = $total + $pepperoni;
} // end if 

if (isset($_POST['beef'])){
print ("You chose Beef <br> \n");
$total = $total + $beef;
} // end if

if (isset($_POST['sausage'])){
print ("You chose Sausage <br> \n");
$total = $total + $sausage;
} // end if

if (isset($_POST['olives'])){
print ("You chose Olives <br> \n");
$total = $total + $olives;
} // end if

print "The total cost is \$$total \n";

?>


</body>
</html>


"never mind" says to me "Your wasting your time, I'm doing it this way anyway"

 

If he's going to disregard the help, why post? Let's face it, his code isn't exactly optimal. Can you imagine it with 50+ items on the menu.

 

I'm not saying he's bad, just that I'll not bother again.

"never mind" says to me "Your wasting your time, I'm doing it this way anyway"

 

If he's going to disregard the help, why post? Let's face it, his code isn't exactly optimal. Can you imagine it with 50+ items on the menu.

 

I'm not saying he's bad, just that I'll not bother again.

 

i apologize, if my words offended you.  i was trying to say that i found a solution.  and thank you for your help.  i was just saying that i was able to find a solution. and also i clicked on problem solved. so that no one else will post to this thread anymore. sorry if i have offended u.  i tried to do the other code. but it was not working to solve my problem. it kept giving me errors. so that is why i tried it this way.  hth

"never mind" says to me "Your wasting your time, I'm doing it this way anyway"

 

If he's going to disregard the help, why post? Let's face it, his code isn't exactly optimal. Can you imagine it with 50+ items on the menu.

 

I'm not saying he's bad, just that I'll not bother again.

 

yes, your correct.  but i am just trying to learn the basics.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.