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