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>




 

 

Link to comment
Share on other sites

#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?!!!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>


Link to comment
Share on other sites

"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

Link to comment
Share on other sites

"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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.