Jump to content

Trying to pass arguments into function (one arg is missing)


n1concepts

Recommended Posts

I need someone to explain how to loop through an array ($products) and grab the three values (name, price, & shipping) and have them sent to "all_products" function - see line 13 then 61 thru 64.

 

I currently have the script partially working by using array_shift which I know is removing the 1st element (name or $product value).

Note: this is the problem (I need $product included in the call to the "all_products" function along with $price & $shipping so that the final output looks as follow:

---------------------------------

 

Checkout

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

 

    * Candle Holder: $12.95

    * Coffee Table: $99.5

    * Lamp: $42.99

 

 

Total (including tax and shipping): $182.12

------------------------------------------------------

 

HERE'S THE SCRIPT FOR REVIEW & COMMENT:

===============================

<B>Checkout</B><br>

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

<?php

 

/*

Fix this code by creating a function to determine sub-totals for each product. Then add the sub-totals together to arrive at the grand total.The product name, & price should display for each item.

 

?><ul><?

 

/* "all_products" function was created to query then calculate the sub-totals for each prodcut, then add each sub-total togehter to form grand total - outside the function */

 

 

function all_products($prod_array) {

extract($prod_array);

 

#tax rate is constant

$tax = 0.08;

$total_price = 0;

$total_tax = 0;

$total_shipping = 0;

 

// Calculations per product with returned sum sent outside to $grand_total variable

$total_price += $price;

$total_tax += $tax * $price;

$total_shipping += $shipping * $price;

$sub_total = ($total_price + $total_tax + $total_shipping);          # This calculates the sub-total (or Grand total) for each product

echo "<li>".$product.": $".$price."<br />"; # THE PRODUCT NAME & PRICE SHOULD DISPLAY HERE (NAME IS NOT SHOWING WHICH IS PROBLEM)

# I had echo statement to confirm calculation on product was correct

# echo "Grand Total is $".$sub_total."<br />";

return $sub_total;  # This breaks out of the function when Candle Holder IF statement is true; returning value

}

 

/*

Keep the functions, and add a two-dimensional array that acts as a product database -- it should contain the name, price, and shipping price for every product -- and use list() and each() to traverse the database.

*/

 

// Products array with List / Each configuration to pass arguements into function

$products = array('Candle Holder' => array('price' => '12.95','shipping' => '0.00'),

'Coffee Table'=>array('price' => '99.50','shipping' => '0.10'),

'lamp' =>array('price' => '99.50','shipping' => '0.10'));

 

 

while (list($k, $v) = each($products)) {

$product = $k;                                                                                                                                           

# This is individual product name extracted from array

#echo "Here's the price & shipping cost for ".$product.":<br/>";

while (list($key, $value) = each($v)) {

if (array_key_exists('price',$v)) {

$price = $value;

#echo "The product ".$key." is: $".$value."<br />";

}

if (array_key_exists('shipping',$v)) {

$shipping = $value;

#echo "The ".$key." cost is: ".$value."<br />";

}

 

}

}

 

# THE BELOW THREEE LINES TRAVERSE THE ARRAY BUT STRIPPING THE 1ST ELEMENT WHICH IS NOT WHAT I WANT; I NEED THE INCLUDED TO DISPLRAY BEFORE PRICES

$sub_total1 = all_products(array_shift($products));

$sub_total2 = all_products(array_shift($products));

$sub_total3 = all_products(array_shift($products));

 

 

echo "RESULTS OUTSIDE FUNCTION: ".$sub_total1."<br />";

echo "RESULTS OUTSIDE FUNCTION: ".$sub_total2."<br />";

echo "RESULTS OUTSIDE FUNCTION: ".$sub_total3."<br />";

// Add total sum for each product to define grand total for all products including shipping & taxes

 

$grand_total = $sub_total1 + $sub_total2 + $sub_total3;                             

 

?>

</ul>

<hr>

<br>

<?php

# Below is the "GRAND TOTAL" for all three project - displayed outside the function which should be $182.12 USD

?>

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

 

[attachment deleted by admin]

Link to comment
Share on other sites

Using array_shift() you are essentially passing the array('price' => '12.95','shipping' => '0.00') elements into the function. The key ('Candle Holder') is basically tossed out.  Since the product is not in the array you passed in, you're not going to have it inside the function.  Since the function appears to be for calculation purposes, you may not actually need it there.  I might suggest that instead of this:

$sub_total1 = all_products(array_shift($products));
$sub_total2 = all_products(array_shift($products));
$sub_total3 = all_products(array_shift($products));


echo "RESULTS OUTSIDE FUNCTION: ".$sub_total1."<br />";
echo "RESULTS OUTSIDE FUNCTION: ".$sub_total2."<br />";
echo "RESULTS OUTSIDE FUNCTION: ".$sub_total3."<br />";
// Add total sum for each product to define grand total for all products including shipping & taxes

$grand_total = $sub_total1 + $sub_total2 + $sub_total3;                             

you use something like this:

$grand_total = 0;
foreach ($products as $product => $values) {
  $sub_total = all_products($values);
  $grand_total += $sub_total;
  echo '<li>' . $product . ': $' . $sub_total . '<br />';  
}?>
<B>Total (including tax and shipping): $<?php echo number_format($grand_total, 2); ?></B>

(not tested)

 

If you absolutely need the product name inside the function, you will have to change the function definition to something like:

function all_products($product, $prod_array) {

and then pass it in like:

$sub_total = all_products($product, $values);

inside the loop.

 

There are other alternatives, such as changing your array structure.

$products = array(0 => array('product' => 'Candle Holder', 'price' => '12.95','shipping' => '0.00'),
            1=>array('prodcut'=>'Coffee Table','price' => '99.50','shipping' => '0.10'),
            2=>array('product'=>'lamp', 'price' => '99.50','shipping' => '0.10'));

But I don't know how much effort that would be for you since you might have other code/scripts using this array.

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.