david212 Posted May 2, 2010 Share Posted May 2, 2010 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 Link to comment https://forums.phpfreaks.com/topic/200434-need-help-with-function/ Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 You want to store the values into an array and use a loop rather than a function because of variable scope. Link to comment https://forums.phpfreaks.com/topic/200434-need-help-with-function/#findComment-1051823 Share on other sites More sharing options...
david212 Posted May 2, 2010 Author Share Posted May 2, 2010 this is my hometask, and right now i don't know how to use arrays . It's just an introduction to functions and i don't know how to store correctly all values to get subtotal for each product Link to comment https://forums.phpfreaks.com/topic/200434-need-help-with-function/#findComment-1051830 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 Do you know how to use loops? Link to comment https://forums.phpfreaks.com/topic/200434-need-help-with-function/#findComment-1051832 Share on other sites More sharing options...
david212 Posted May 2, 2010 Author Share Posted May 2, 2010 yes Link to comment https://forums.phpfreaks.com/topic/200434-need-help-with-function/#findComment-1051845 Share on other sites More sharing options...
xcoderx Posted May 2, 2010 Share Posted May 2, 2010 for loop :-) Link to comment https://forums.phpfreaks.com/topic/200434-need-help-with-function/#findComment-1051846 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 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 https://forums.phpfreaks.com/topic/200434-need-help-with-function/#findComment-1051860 Share on other sites More sharing options...
david212 Posted May 2, 2010 Author Share Posted May 2, 2010 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 https://forums.phpfreaks.com/topic/200434-need-help-with-function/#findComment-1051866 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.