A question about PHP Function

Discussion in 'PHP' started by sunnyboy1984, Mar 1, 2007.

  1. #1
    Can anyone fix this code by creating as many functions as needed to eliminate repetition and utilize code reusability? Please comment where neccessary. Thank you.


    <B>Checkout</B><br>
    Below is a summary of the products you wish to purchase, along with totals:
    <?php
    
    #tax rate is constant
    $tax = 0.08;
    $total_price = 0;
    $total_tax = 0;
    $total_shipping = 0;
    $grand_total = 0;
    ?><ul><?
    
    $product = "Candle Holder";
    $price = 12.95;
    $shipping = 0;  //free shipping
    echo "<li>".$product.": $".$price;
    
    //tally totals
    $total_price += $price;
    $total_tax += $tax * $price;
    $total_shipping += $shipping * $price; 
    $grand_total += ($total_price + $total_tax + $total_shipping);
    
    $product = "Coffee Table";
    $price = 99.50;
    $shipping = 0.10;  //shipping as a percentage of price
    echo "<li>".$product.": $".$price;
    
    //tally totals
    $total_price += $price;
    $total_tax += $tax * $price;
    $total_shipping += $shipping * $price; 
    $grand_total += ($total_price + $total_tax + $total_shipping);
    
    $product = "Lamp";
    $price = 42.99;
    $shipping = 0.10;  //shipping as a percentage of price
    echo "<li>".$product.": $".$price;
    
    //tally totals
    $total_price += $price;
    $total_tax += $tax * $price;
    $total_shipping += $shipping * $price; 
    $grand_total += ($total_price + $total_tax + $total_shipping);
    
    ?>
    </ul>
    <hr>
    Subtotal: $<? echo number_format($total_price, 2);  ?><br>
    Tax: $<? echo number_format($total_tax, 2); ?><br>
    Shipping: $<? echo number_format($total_shipping, 2); ?><br>
    <B>Total: $<? echo number_format($grand_total, 2); ?></B>
    PHP:

     
    sunnyboy1984, Mar 1, 2007 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'd probably make a 'product' class/object and do the calculations internally.
     
    exam, Mar 1, 2007 IP
  3. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #3
    The following line of code is wrong.
    It should be
    $grand_total = ($total_price + $total_tax + $total_shipping);
    PHP:
     
    Aragorn, Mar 1, 2007 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    But you are going to erase the grand_total?

    You can use the original code:

    
    $grand_total += ($total_price + $total_tax + $total_shipping);
    
    PHP:
    or

    
    $grand_total = $grand_total + $total_price + $total_tax + $total_shipping;
    
    PHP:
    Peace,
     
    Barti1987, Mar 1, 2007 IP
  5. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Here is an object oriented approach
    
    <?php
    // Begin Class Definition
    class calcTax{
    	var $tax = 0.08;
    	var $totalPrice = 0;
    	var $totalTax = 0;
    	var $totalShipping = 0;
    	var $grandTotal = 0;
    	// Class constructor
    	function calcTax($tax = FALSE){
    		if($tax !== FALSE){
    			$this->tax = $tax;
    		}
    	}
    	// Use the fourth variable to decide whether or not to print the price
    	function newProduct($product, $price, $shipping = 0, $print = TRUE){
    		$this->totalPrice += $price;
    		$this->totalTax += $price * $this->tax;
    		$this->totalShipping += $price * $shipping;
    		$this->grandTotal = $this->totalPrice + $this->totalTax + $this->totalShipping;
    		if($print){
    			echo "<li>$product : \$$price</li>";
    		}
    	}
    	// Print the sub total
    	function subTotal(){
    		echo 'Subtotal: $' . number_format($this->totalPrice, 2) . '<br/>';
    		echo 'Tax: $' . number_format($this->totalTax, 2) . '<br/>';
    		echo 'Shipping: $' . number_format($this->totalShipping, 2) . '<br/>';
    		echo '<b>Total: $' . number_format($this->grandTotal, 2) . '</b><br/>';
    	}
    } // End Class Definition
    
    
    $ct = new calcTax(0.08);
    $ct->newProduct("Candle Holder", 12.95);
    $ct->newProduct("Coffee Table", 99.5, 0.10);
    $ct->newProduct("Lamp", 42.99, 0.1);
    echo '<hr/>';
    $ct->subTotal();
    ?>
    
    PHP:
     
    Aragorn, Mar 1, 2007 IP
  6. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Here is a little more simpler, less generic code.

    <?php
    
    function newProduct($product, $price, $shipping = 0){
    	global $tax, $totalPrice, $totalTax, $totalShipping, $grandTotal;
    	$totalPrice += $price;
    	$totalTax += $price * $tax;
    	$totalShipping += $price * $shipping;
    	$grandTotal = $totalPrice + $totalTax + $totalShipping;
    	echo "<li>$product : \$$price</li>";
    }
    // Print the sub total
    function subTotal(){
    	global $totalPrice, $totalTax, $totalShipping, $grandTotal;
    	echo 'Subtotal: $' . number_format($totalPrice, 2) . '<br/>';
    	echo 'Tax: $' . number_format($totalTax, 2) . '<br/>';
    	echo 'Shipping: $' . number_format($totalShipping, 2) . '<br/>';
    	echo '<b>Total: $' . number_format($grandTotal, 2) . '</b><br/>';
    }
    
    $tax = 0.08;
    $totalPrice = 0;
    $totalTax = 0;
    $totalShipping = 0;
    $grandTotal = 0;
    
    newProduct("Candle Holder", 12.95);
    newProduct("Coffee Table", 99.5, 0.10);
    newProduct("Lamp", 42.99, 0.1);
    echo '<hr/>';
    subTotal();
    ?>
    PHP:
    Hope the code is clear. Feel free to ask otherwise :)
     
    Aragorn, Mar 1, 2007 IP
  7. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #7
    You are wrong :)

    The grand total is the sum of the Total Price + Total Tax + Total Shipping
    or
    Grand Total + Price of last item + Tax for last item + Shipping charge for last item.

    Hope you got the difference. You should use '+=' either with $grand_total or ($total_price & $total_tax & $total_shipping). Check the output of the original code. It is
    
    Subtotal: $155.44
    Tax: $12.44
    Shipping: $14.25
    Total: $327.51
    
    Code (markup):
    With such calculation you are not going to stay in business for long :D
     
    Aragorn, Mar 1, 2007 IP