djcrisp22 Posted December 1, 2008 Share Posted December 1, 2008 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> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 1, 2008 Share Posted December 1, 2008 #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'])){ Quote Link to comment Share on other sites More sharing options...
djcrisp22 Posted December 1, 2008 Author Share Posted December 1, 2008 #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?!!! Quote Link to comment Share on other sites More sharing options...
Barand Posted December 1, 2008 Share Posted December 1, 2008 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> Quote Link to comment Share on other sites More sharing options...
djcrisp22 Posted December 1, 2008 Author Share Posted December 1, 2008 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> Quote Link to comment Share on other sites More sharing options...
Barand Posted December 1, 2008 Share Posted December 1, 2008 A bit late to say "never mind" whan a couple of us have already wasted our time trying to help. Username noted. Quote Link to comment Share on other sites More sharing options...
awpti Posted December 1, 2008 Share Posted December 1, 2008 So, because he found the issue within the 2 hours between the last response, he's a bad person for it? Ouch. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 1, 2008 Share Posted December 1, 2008 "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. Quote Link to comment Share on other sites More sharing options...
djcrisp22 Posted December 2, 2008 Author Share Posted December 2, 2008 "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 Quote Link to comment Share on other sites More sharing options...
djcrisp22 Posted December 2, 2008 Author Share Posted December 2, 2008 "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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.