Jump to content

Need help with function


david212

Recommended Posts

Here is the code :

 

<?php


$var_tax = 0.08;
$var_total_price = 0;
$total_tax = 0;
$var_total_shipping = 0;
$var_g_total = 0;
?><ul><?

$product = "a1";
$var_price = 12.95;
$var_shipping = 0;
echo "<li>".$product.": $".$var_price;


$var_total_price += $var_price;
$total_tax += $var_tax * $var_price;
$var_total_shipping += $var_shipping * $var_price;
$var_g_total = ($var_total_price + $total_tax + $var_total_shipping);

$product = "a2";
$var_price = 99.50;
$var_shipping = 0.10;
echo "<li>".$product.": $".$var_price;


$var_total_price += $var_price;
$total_tax += $var_tax * $var_price;
$var_total_shipping += $var_shipping * $var_price;
$var_g_total = ($var_total_price + $total_tax + $var_total_shipping);

$product = "a3";
$var_price = 42.99;
$var_shipping = 0.10;
echo "<li>".$product.": $".$var_price;


$var_total_price += $var_price;
$total_tax += $var_tax * $var_price;
$var_total_shipping += $var_shipping * $var_price;
$var_g_total = ($var_total_price + $total_tax + $var_total_shipping);

?>

 

I have to change  this code by creating a function to determine sub-totals for each product.Then i have to add sub-total to get the $var_g_total. Can anyone help me and explain how can i create this function? I can do it separately but in functions im pretty new like in all php and i got confused and need more detailed explication.

 

I tried to create function in this way:

 

function myfunction( $price, $shipping,$tax )

{

$var_total_price += $price;

$total_tax += $var_tax * $var_price;

$var_total_shipping += $shipping * $price;

$var_g_total = ($var_total_price + $total_tax + $var_total_shipping);

return $var_g_total ;

}

 

But it doesn't work to determine sub-totals for each product  :confused:

 

Link to comment
Share on other sites

You can store the values in an array like this:

 

$a = array(
     'product1'  => array(
          'price' => '5.50',
          'shipping' => '10.40'
     ),
     'product2'  => array(
          'price' => '10.00',
          'shipping' => '.10'
      ),
      ...
);

// keeps track of the total price
$price = 0;

// keeps track of the total shipping
$shipping = 0;

// then loop
foreach ($a as $key => $value) {
     $price += $value['price'];
     $shipping += $value['shipping'];
}

echo sprintf("Price: %s<br />Shipping: %s<br />", $price, $shipping);

Link to comment
Share on other sites

Thank you Ken2k7 but i find the solution in this way:

 

function subtotals($price, $tax, $shipping){
$var_total_price += $price;
$total_tax += $tax * $price;
$var_total_shipping += $shipping * $price;
$var_g_total = $var_total_price + $total_tax + $var_total_shipping;

$result += $var_g_total;

return $result;
}

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.