Jump to content

Trying to calculate a $grand_total.


dms

Recommended Posts

Everything in this code seems to be working except for one desired result - I can not figure out why the total for $grand_total does not calculate. I'm trying to learn some php and I'm at a loss. Thanks in advance, Mark

 

<?php

$tax = 0.08; #tax rate is constant

$sub_total = 0.0;

 

?>

<?php

//two-dimensional array... to hold the product name, price, shipping.

$name = array(

"Candle Holder" => array(

"price" => 12.95,

"shipping" => 0.0),

"Coffee Table" => array(

"price" => 99.50,

"shipping" => 0.10),

"Lamp" => array(

"price" => 42.99,

"shipping" => 0.10));

 

 

//function to populate the sub_total to include... price, tax, shipping.

function tally($price, $shipping, $tax = 0.08) {

$sub_total = ($price + ($tax * $price) + $shipping);

return $sub_total;

}

 

/*using list() and each() to traverse the array and capturing the function return and setting the $sub_total value.

while (list($key, $value) = each($name)) {

echo "$key: ". "$value[price]". "\n";

$tally_up=tally($price, $shipping);

$sub_total += $tally_up;

}

$grand_total = $sub_total;

 

?>


 

 

Total (including tax and shipping): $<? echo $grand_total; ?>

Link to comment
Share on other sites

Two things:

 

-- the third comment needs to start with a "//" rather than "/*" (or, you need to close the comment with a */ at the end of the line.) Currently, without the closing comment, the rest of the code after that point becomes one long comment

-- This line: "$tally_up=tally($price,$shipping);" needs to be "$tally_up = tally("$value[price]", "$value[shipping]");"

 

If you aren't already, I'd highly suggest you use a text editor with some basic code highlighting functionality (Notepad++, Eclipse, etc). It'll help you catch small mistakes like the first issue above

Link to comment
Share on other sites

I have a related PHP question - something I didn't understand, even though I was able to get the code example working...

 

In the line I changed:

 

"$tally_up = tally("$value[price]", "$value[shipping]");"

 

Why do I have to place double quotes around the two $value[] variables? I get errors without them, and I am not totally sure why. Perhaps someone who has a bit more experience than I do with PHP can give me an answer?

 

These were the errors (technically, I suppose these are notices, rather than errors?) I get when I don't include the quotes:

Notice: Use of undefined constant price - assumed 'price' in /Applications/MAMP/htdocs/Experiments/error.php on line 27

Notice: Use of undefined constant shipping - assumed 'shipping' in /Applications/MAMP/htdocs/Experiments/error.php on line 27

Notice: Use of undefined constant price - assumed 'price' in /Applications/MAMP/htdocs/Experiments/error.php on line 27

Notice: Use of undefined constant shipping - assumed 'shipping' in /Applications/MAMP/htdocs/Experiments/error.php on line 27

Notice: Use of undefined constant price - assumed 'price' in /Applications/MAMP/htdocs/Experiments/error.php on line 27

Notice: Use of undefined constant shipping - assumed 'shipping' in /Applications/MAMP/htdocs/Experiments/error.php on line 27

Link to comment
Share on other sites

Oh, and one other thing I should mention... This code is a bit overly complicated:

 

while (list($key, $value) = each($name)) {

echo "$key: ". "$value[price]". "\n";

$tally_up=tally("$value[price]", "$value[shipping]");

$sub_total += $tally_up;

}

 

You could easily do it this way, which is a bit easier to read and understand if you haven't worked on the project for a while and have to make edits.

 

foreach ($name as $key => $value) {

echo "$key: ". "$value[price]". "\n";

$tally_up=tally("$value[price]", "$value[shipping]");

$sub_total += $tally_up;

}

Link to comment
Share on other sites

This code is part of a lesson, where using "each() and list()" were required. Thanks for explaining how to clean up the code using the foreach loop.

 

I"m not sure why I placed the double quotes around the two $value[] variables - I think I used an example from one of my books. I did notice that by adding single quotes around the [key] you can remove the double quotes and it works fine.

 

Thanks Ben!

Link to comment
Share on other sites

I asked my question on a more PHP oriented forum, and they said I'm getting the errors because I am forgetting to use quotes around the key values.

 

So, for example, this is invalid:

 

$tally_up=tally($value[price], $value[shipping]);

 

but this is valid and I get no errors (quotes around price and shipping):

 

$tally_up=tally($value["price"], $value["shipping"]);

 

-----

 

EDIT: Looks like that's what you said above.

Link to comment
Share on other sites

I guess it doesn't really matter, but I assume that single quotes around the [key] is preferred over double. When using a number for the [key], I don't think that you use quotes?

Link to comment
Share on other sites

I'm not sure, but you're probably right. Most likely, PHP checks for variables when the key is placed inside double quotes, while it assumes it is just a string if placed inside single quotes. And yes, no quotes if they key is a number -- unless, you set up your array like this, where the key is a string that happens to be a number:

 

$name = array(

"Candle Holder" => array(

"1" => 12.95,

"2" => 0.0),

"Coffee Table" => array(

"1" => 99.50,

"2" => 0.10),

"Lamp" => array(

"1" => 42.99,

"2" => 0.10));

 

Why someone would do that though, I'm not sure.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...