Jump to content

old code


dan_t

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/149391-old-code/
Share on other sites

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>

 

 

Link to comment
https://forums.phpfreaks.com/topic/149391-old-code/#findComment-784659
Share on other sites

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.