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
https://forums.phpfreaks.com/topic/200434-need-help-with-function/
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);

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;
}

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.