Jump to content

Recommended Posts

The code below doesn't work. I believe the function is not executing, but I've been hunting and pecking for too long and need some input. Can anyone take time out to help with this?

Best, J.

 

 

<B>Checkout</B><br>

Below is a summary of the products you wish to purchase, along with totals:

<?php

 

#tax rate is constant

$price = 0;

$tax = 0.08;

$total_price = 0;

$total_tax = 0;

$shipping = 0;

$total_shipping = 0;

$grand_total = 0;

?><ul><?

 

function tallyTotals($grand_total) {

$total_price += $price;

$total_tax += $tax * $price;

$total_shipping += $shipping * $price;

$grand_total = ($total_price + $total_tax + $total_shipping);

}

 

$product = "Candle Holder";

$price = 11.95;

$shipping = 0; //free shipping

echo "<li>".$product.": $".$price;

 

tallyTotals($grand_total);

 

$product = "Coffee Table";

$price = 99.50;

$shipping = 0.10; //shipping as a percentage of price

echo "<li>".$product.": $".$price;

 

tallyTotals($grand_total);

 

$product = "Floor Lamp";

$price = 44.99;

$shipping = 0.10; //shipping as a percentage of price

echo "<li>".$product.": $".$price;

 

tallyTotals($grand_total);

 

?>

</ul>

<hr>

<br>

<B>Total (including tax and shipping): $<? echo number_format($grand_total, 2); ?></B>

Link to comment
https://forums.phpfreaks.com/topic/273926-total-newbie-needs-help/
Share on other sites

I created a new function to try and help you understand the concept. It is fully commented. Note that I left your function and calls in the script, but commented them out. I also added error_reporting to the script as well. You should really set error reporting in you php.ini though.

 


<?php
//turn on error_reporting
error_reporting(-1);
ini_set('display_errors',1);
#tax rate is constant
$price = 0;
$tax = 0.08;
$total_price = 0;
$total_tax = 0;
$shipping = 0;
$total_shipping = 0;
$grand_total = 0;
?><ul><?php

// function tallyTotals($grand_total) {
// $total_price += $price;
// $total_tax += $tax * $price;
// $total_shipping += $shipping * $price;
// $grand_total = ($total_price + $total_tax + $total_shipping);
// }

///////////////////Any variable created in a function, is only available to that function.
//////////////////To get information out of a function, you must return the information.
function totalPrice($price, $shipping, $percentage = FALSE) { //create new function with 3 arguments, first 2 MUST be supplied, the third is optional.
if($percentage == TRUE) { //if the 3rd argument is changed to true, then run this block of code.
return $price + ($price * $shipping); //and return the price + a percentage of the price for shipping charges.
}
return $price + $shipping; //if the shipping is not a percentage, then just add it to the price, and return the result.
}

$product = "Candle Holder";
$price = 11.95;
$shipping = 0; //free shipping
echo "<li>".$product.": $".$price;

$grand_total += totalPrice($price,$shipping); //Send the arguments to the function, notice there is no 3rd argument supplied, the function will use the pre-defined FALSE.  returned data will be stored in $grand_total.
//tallyTotals($grand_total);

$product = "Coffee Table";
$price = 99.50;
$shipping = 0.10; //shipping as a percentage of price
echo "<li>".$product.": $".$price;

$grand_total += totalPrice($price,$shipping,TRUE); //Another call to totalPrice, this time with 3 arguments, since we need to change the shipping to a percentage by setting the 3rd argument to TRUE.
//tallyTotals($grand_total);

$product = "Floor Lamp";
$price = 44.99;
$shipping = 0.10; //shipping as a percentage of price
echo "<li>".$product.": $".$price;

$grand_total += totalPrice($price,$shipping,TRUE); //same as above.
//tallyTotals($grand_total);

?>
</ul>
<hr>
<br>
<B>Total (including tax and shipping): $<?php echo number_format($grand_total, 2); ?></B> 

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.