ding Posted July 20, 2009 Share Posted July 20, 2009 Hi, Does PHP allow you to apply a variable to an individual array element. I looked at the different array functions, and as far I can see this is not possible. But maybe I'm missing something. Could someone please enlighten me as to do this. Thanks ding Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/ Share on other sites More sharing options...
Maq Posted July 20, 2009 Share Posted July 20, 2009 By apply you mean? Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-878695 Share on other sites More sharing options...
rhodesa Posted July 20, 2009 Share Posted July 20, 2009 can you give an example of the variable, the original array, and what you want the final array to be like? Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-878696 Share on other sites More sharing options...
ding Posted July 20, 2009 Author Share Posted July 20, 2009 Yes I can , I have a $discount variable which is $discount = calculatecoupon( getcoupon( ), $totalproductprice, $totalshipping, $totaltax ); And for the array $totalproductprice = $productprice * $record['quantity']; $Prices = array ($totalproductprice); foreach ($Prices as $label => $Price) { //code print_r ($Prices); } print_r returns a two dimensional array Array ( [0] => 975 ) Array ( [0] => 1567 ) Array ( [0] => 1669 ) Array ( [0] => 800 ) I would like to make the $discount take effect on an individual array element. ding Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-878718 Share on other sites More sharing options...
Daniel0 Posted July 20, 2009 Share Posted July 20, 2009 Then pass that individual element instead of the entire array... Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-878735 Share on other sites More sharing options...
rhodesa Posted July 20, 2009 Share Posted July 20, 2009 which array...this looks like code that is inside a loop. also, i don't see the purpose of this code: $Prices = array ($totalproductprice); $discount = 0.2; //20% discount $totalproductprice = $productprice * $record['quantity']; $dicountedprice = $totalproductprice * (1 - $discount); Is that not what you are looking to do? Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-878747 Share on other sites More sharing options...
ding Posted July 20, 2009 Author Share Posted July 20, 2009 What I'm working with is a shopping cart, which has the ability to have a discount on the total price of the cart. I'm trying to add the functionality to have a discount per item in the cart. I just started php, and decided to learn by using an open source project and add functionality I need Although this does work $discount = 0.2; //20% discount $totalproductprice = $productprice * $record['quantity']; $dicountedprice = $totalproductprice * (1 - $discount); It discounts every item. I have allready added a form with a selectbox o the HTML page, which calls the discount. When I select the discount for a specific product, all the products are discounted. That is basically where I get stuck. As for this $Prices = array ($totalproductprice); I figured if I add all the contents of the cart to an array, I could use foreach to go over the individual elements and see if there is a discount and then add the discount or if there isn't then show just the $totalproductprice. Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-878782 Share on other sites More sharing options...
rhodesa Posted July 20, 2009 Share Posted July 20, 2009 ok...i see where you are going with this...first i will say: $Prices = array ($totalproductprice); creates a new array each time. you would want to create the array before the loop then add the elements to it inside the loop: $Prices = array(); while(...){ $Prices[] = $totalproductprice; } how do you know which products get discounted? Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-878794 Share on other sites More sharing options...
ding Posted July 20, 2009 Author Share Posted July 20, 2009 ok...i see where you are going with this...first i will say: $Prices = array ($totalproductprice); creates a new array each time Aha! I did not see this. Thank you!! That was the part that actually got me stuck. My array was not being properly build, each price was in it's own array with a key of 0. Now it is properly build and indexed and I'm getting Array ( [0] => 975 [1] => 1567 [2] => 1669 [3] => 800 ) instead of Array ( [0] => 975 ) Array ( [0] => 1567 ) Array ( [0] => 1669 ) Array ( [0] => 800 ) As for how do you know which products get discounted? I didn't get that far yet, but I think I could make a function to get the position of the element and have it discounted. Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-878848 Share on other sites More sharing options...
ding Posted July 20, 2009 Author Share Posted July 20, 2009 I made progress (albeit little), I created a second array with the discounts. $Prices = array(); $Discounts = array(); while(...){ $Prices[] = $totalproductprice; $Discounts[] = $discountProduct; //variable $discountProduct is defined further up in the script } My logic is to compare the price array and the discount array If there is a value at a certain index in the discount array substract that from the value at the corresponding index at the price array. At this moment for every price array element a discount array element is created with value 0. So I have for the price array Array ( [0] => 975 [1] => 1567 [2] => 1669 [3] => 800 ) and for the discount array Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 ) When I select a discount for example for index [2] (third product in list) of the products, the entire discount array is filled with the percentage discount Array ( [0] => 195 [1] => 313.4 [2] => 333.8 [3] => 160 ) instead of just index [2] of discount array How can I make it so that just the corresponding index is updated and subtracted from both array's I hope I'm not confusing anyone, in my head it sound much more logical... really. Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-879055 Share on other sites More sharing options...
rhodesa Posted July 20, 2009 Share Posted July 20, 2009 Let's cover the meaning of some of these variables.... $discount = calculatecoupon( getcoupon( ), $totalproductprice, $totalshipping, $totaltax ); this code is run after the loop right? should $totalproductprice be the shopping cart total? once a $discount is determined, should that discount be applied to each item in the cart? Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-879072 Share on other sites More sharing options...
ding Posted July 20, 2009 Author Share Posted July 20, 2009 No it's not the cart total. $totalproductprice is the total price of one item, it's the $productprice * $record['quantity']. so you have donkey price = €75,- qty = 2 discount = select discount totalproductprice = €150,- goat price = €24,- qty = 1 discount = select discount totalproductprice = €24,- waterpump price = 34,- qty =1 discount = select discount totalproductprice = €34,- After re-reading your question I just noticed that the code is inside the while loop. This is also where the $totalproductprice is calculated. I'm trying to make the discount apply per item in the cart. So when someone chooses to donate money for a donkey (it's for a charity, i'm not a farmer ) and they select a discount so that they pay less but give more (through an affiliate) that discount is only applied to the price of €150,-. The fact that it's run inside the while loop is the issue right? Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-879101 Share on other sites More sharing options...
rhodesa Posted July 21, 2009 Share Posted July 21, 2009 you have a loop, which loops over all the items...that has been established. my question now is, when do we know what and how much the discount is? before the loop or not until after the loop? is the function that calculates the discount run on each item? if so, is there any reason we can't use it inside the loop? Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-879144 Share on other sites More sharing options...
ding Posted July 21, 2009 Author Share Posted July 21, 2009 Well, the discount itself is calculated in a different script. The getcoupon() function is defined later in the script. This is is the if statement which defines the $discount variable. if ( $_SESSION['CouponIsValid'] ) { require_once( $config['filepath']."modules/coupon.product.inc.php" ); $discount = calculatecoupon( getcoupon( ), $totalproductprice, $totalshipping, $totaltax ); } else { $discountProduct = 0; } This if statement is inside the while loop, just before where the $totalproductprice is calculated. Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-879266 Share on other sites More sharing options...
rhodesa Posted July 21, 2009 Share Posted July 21, 2009 how can it be before the totalproductprice is calculated if you are passing totalproductprice as an argument? Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-879394 Share on other sites More sharing options...
ding Posted July 21, 2009 Author Share Posted July 21, 2009 Wrong choice of words What I meant was it was before an if statement where it checks if tax option is selected. The totalproductprice is defined before the piece of code I posted before. It was late yesterday, just ignore my last post. This is the correct code $totalproductprice = $productprice * $record['quantity']; if ( $_SESSION['CouponIsValid'] ) { require_once( $config['filepath']."modules/coupon.product.inc.php" ); $discountProduct = CalculateCouponProduct( getcouponproduct( ), $totalproductprice, $totalshipping, $totaltax ); } else { $discountProduct = 0; } if ( $config['pricewithtax'] == "Y" ) { $optionvalue = getpricewithtax( $optionvalue, $record['taxid'] ); $productprice = getpricewithtax( $record['price'], $record['taxid'] ) + $optionvalue; $totalproductprice = $productprice * $record['quantity'] - $discountProduct; $Prices[]= $totalproductprice; $Discounts[]= $discountProduct; $prices = $Prices[0]; $discounts = $Discounts[0]; $grandtotal = $totalproductprice + $totalshipping; } else { $grandtotal = $totalproductprice + $totalshipping + $totaltax; } It is difficult to read code when your partially still asleep. Quote Link to comment https://forums.phpfreaks.com/topic/166638-apply-variable-to-array-element/#findComment-879461 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.