kikonyc Posted April 4, 2012 Share Posted April 4, 2012 the PHP below counts the number of items but not the total quantity (quantity_1) to calculate the value of $shipping. any ideas? link to form: http://crsecrets.com/test01/order.html FORM <TD>Herbal Hair Rejuvenator, Unscented <FONT SIZE="-2">(4oz. jar)</FONT><input type="hidden" name="item_name_1" value="Herbal Hair Rejuvenator, Unscented (4oz. jar)" /> </TD> <TD align="center" id="table"><input name="quantity_1" type="text" value="" size="6" maxlength="2" /></TD> <TD align="center">$13.50<input type="hidden" name="amount_1" value="13.50" /></TD> [m]<?php $products = array(); $url = "?business=*********$handling_cart=7.00"; foreach ($_POST as $k=>$v) { preg_match("/\_(\d{1,3})$/",$k,$match); $key = $match[1]; $products[$key][$k] = $v; } $i=1; foreach ($products as $k=>$v) { if ($v['quantity_'.$k] > 0) { foreach ($v as $k1=>$v1) { $key = explode("_",$k1); switch($key[0]) { case 'item': $var = "item_name_" . $i; break; case 'quantity': $var = "quantity_" . $i; break; case 'amount': $var = "amount_" . $i; break; } //get shipping for additional items above 3 if ($i <= 2) { $shipping = "0.00"; } else { $shipping = "1.00"; } $shipping = ($shipping * $v['quantity_' . $k]); $url .= "&" . $var . "=" . urlencode($v1); } $url .= "&shipping_" . $i . "=" . $shipping; $i++; } } #echo $url; header("Location: https://www.paypal.com/cgi-bin/webscr" . $url); exit; ?>[/m] Quote Link to comment https://forums.phpfreaks.com/topic/260337-new-to-php-nee-sum-of-total-quantities-for-shipping/ Share on other sites More sharing options...
smerny Posted April 4, 2012 Share Posted April 4, 2012 not sure if i'm following your code right or if i fully understand what you are trying to do... but possibly this? $total_qty = 0; //suggested $i=1; foreach ($products as $k=>$v) { if ($v['quantity_'.$k] > 0) { $total_qty += $v['quantity_'.$k]; //suggested foreach ($v as $k1=>$v1) { $key = explode("_",$k1); and then putting your shipping calculations AFTER the loops, based on $total_qty? Quote Link to comment https://forums.phpfreaks.com/topic/260337-new-to-php-nee-sum-of-total-quantities-for-shipping/#findComment-1334314 Share on other sites More sharing options...
AyKay47 Posted April 4, 2012 Share Posted April 4, 2012 I would use an input array for this, set the input name to an array format: <input name="quantity[]" type="text" value="" size="6" maxlength="2" /> Then iterate the field values, adding the value to the previous value each iteration: $total_quantity = 0; foreach($_POST['quantity'] as $value) { $value = intval($value); //some sanitization to make sure the value is an integer $total_quantity += $value; } This eliminates the need for a regex and multiple loops. Quote Link to comment https://forums.phpfreaks.com/topic/260337-new-to-php-nee-sum-of-total-quantities-for-shipping/#findComment-1334317 Share on other sites More sharing options...
smerny Posted April 4, 2012 Share Posted April 4, 2012 This eliminates the need for a regex and multiple loops. and all the concatenation. i agree with aykay, much simpler Quote Link to comment https://forums.phpfreaks.com/topic/260337-new-to-php-nee-sum-of-total-quantities-for-shipping/#findComment-1334318 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.