Jump to content

New to PHP - Nee sum of total quantities for shipping..


kikonyc

Recommended Posts

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]

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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.