dan_t Posted March 14, 2009 Share Posted March 14, 2009 I have an old book, I know it is very out dated but it 's the only one I have. The example says to use the <?php_track_vars?> which I think is no longer used Right? So how would I go about making this work nowadays. Here is the code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> <html> <head> <title>Bean Counter Results</title> </head> <body> <?php $price = ($_POST['price']); $beans = ($_POST['beans']); $sales_tax = .0825; $quantity = ($_POST['quantity']); $sub_total = $price * $quantity; $sales_tax_amount = $sub_total * $sales_tax; $sales_tax_pct = $sales_tax * 100; $grand_total = $sub_total + $sales_tax_amount; $fmt_price = sprintf("%0.2f",$price); $fmt_sub_total = sprintf("%0.2f",$sub_total); $fmt_sales_tax_amount = sprintf("%0.2f",$sales_tax_amount); $fmt_grand_total = sprintf("%0.2f",$grand_total); echo "<p>You ordered $quantity bags of $beans.</p>"; echo "<p>Bags of $beans are \$$fmt_price each.</p>"; echo "<p>Your subtotal is \$$fmt_sub_total.</p>"; echo "<p>Sales tax is $sales_tax_pct% in this location.</p>"; echo "<p>\$$fmt_sales_tax_amount has been added to your order.</p>"; echo "<p>You owe \$$fmt_grand_total for your coffee.</p>"; if ($beans == "Ethiopian Harrar") { $price = 14.25; } else if ($beans == "Kona") { $price = 16.25; } else if ($beans == "Sumatra") { $price = 13.00; } ?> </body> </html> Thanks Dan Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 14, 2009 Share Posted March 14, 2009 What does your question - "<?php_track_vars?> which I think is no longer used Right?" - have anything to do with the posted code? Quote Link to comment Share on other sites More sharing options...
dan_t Posted March 14, 2009 Author Share Posted March 14, 2009 It says to use that but it doesn't work. It's not posting anything for the price. It says its undefined. It was that or HTTP_GET_VARS. Those are the only things I did not copy from the book. Otherwise Shouldn't it work? Quote Link to comment Share on other sites More sharing options...
WolfRage Posted March 14, 2009 Share Posted March 14, 2009 http://bugs.php.net/bug.php?id=25748 Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 14, 2009 Share Posted March 14, 2009 The only apparent problem in the posted code is that your logic to test the type of beans and set the $price variable comes after the point in the code where the $price variable is being used to calculate the other values. Quote Link to comment Share on other sites More sharing options...
dan_t Posted March 14, 2009 Author Share Posted March 14, 2009 OK I tried running it with the ifelse statement in the middle and got this: Notice: Undefined variable: price in C:\wamp\www\tests\do_calculate.php on line 13 Notice: Undefined variable: price in C:\wamp\www\tests\do_calculate.php on line 17 You ordered 1 bags of Ethiopian Harrar. Bags of Ethiopian Harrar are $0.00 each. Your subtotal is $0.00. Sales tax is 8.25% in this location. $0.00 has been added to your order. You owe $0.00 for your coffee. I can't seem to define the $price variable within the statement. But I don't want to deviate from the book, that would take away the shortcut. Being assigning the amount inside the ifelse. ie.. $price = 14.65. I was able to define quantity because of the "name" in the form, but the $price in within the $bean variable. <form method="post" action="do_calculate.php"> <p>Select a bean type:</p> <select name="beans" size"1"> <option value="Ethiopian Harrar">Ethiopian Harrar - $14.95</option> <option value="Kona">Kona - $16.25</option> <option value="Sumatra">Sumatra - $13.00</option> </select> <p>How many bags would you like?</p> <select name="quantity" size="1"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <select> <input type="submit" value="Submit"> </form> Quote Link to comment Share on other sites More sharing options...
dan_t Posted March 14, 2009 Author Share Posted March 14, 2009 I just thought I would add, If you run the code you'll see it takes the type of bean, and amount. It just will not enter and calculate a price. I'll be a lot happier when I finally understand. 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.