1

I have an array with arrays in it:

Array (
  [0] => Array (
    [qty] => 2
    [name] => thing 1
    [model] => 70001
    [price] => 129.00
    [weight] => 75.00
    [id] => 139
  )
  [1] => Array (
    [qty] => 1 
    [name] => thing 2
    [model] => 70002
    [price] => 199.00
    [weight] => 45.00
    [id] => 53
  )
)

I need to pull the weight of each product compare it to a MySQL DB (I can do this) multiply that by the qty and return a cost; then do the same to the next and so on.

Final Update (with complete class function):

function quote($method = '') {
        global $order, $cart, $shipping_weight, $shipping_num_boxes;

        $order->delivery['postcode'] = strtoupper($order->delivery['postcode']);
        $order->delivery['postcode'] = str_replace (' ', '', $order->delivery['postcode']);

    foreach($order->products as $value){//loop through arrays within arrays
        $weight = $value['weight'];
        $qty = $value['qty'];
        $id = $value['id'];

        if($weight <= 25)//find rate
            $weight_class = 'w25';
        elseif(25 < $weight && $weight <= 50)
            $weight_class = 'w50';
        elseif(50 < $weight && $weight <= 100)
            $weight_class = 'w100';
        elseif(100 < $weight && $weight <= 150)
            $weight_class = 'w150';
        else
            $weight_class = 'w300';

        //strip postal code to fsa
        $postalcode = strtoupper(substr($order->delivery['postcode'],0,3));
        //query database for cost
        $postal_query = 'SELECT ' . $weight_class . ' FROM postal_codes WHERE fsa = "' . $postalcode . '"';
        $result = mysql_query($postal_query) or die(mysql_error());

        while($row = mysql_fetch_array($result)){
            $rate = $row[$weight_class].'<br>';
        }
        $cost = $rate * $qty;//multiple by number of products
        $shipping_total[$id] = $cost;//add total cost for this product(s) to array
    }
    $shipping_rate = array_sum($shipping_total);//sum array to total(s)

        $this->quotes = array(  'id' => $this->code,
                                'module' => MODULE_SHIPPING_DLY3_TEXT_TITLE,
                                'methods' => array(array(   'id' => $this->code,
                                                            'title' => MODULE_SHIPPING_DLY3_TEXT_WAY,
                                                            'cost' =>  $shipping_rate)));
        if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title);
        if ($this->tax_class > 0) {
            $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
        }
        return $this->quotes;
    }

I'll update if i figure out a way to reduce the number of queries..

Jesse

2
  • 1
    It sounds like you will end up doing one query per each item in the loop. This is inefficient and you should cut down on queries. Why can't the initial query that is used to build the array do this on its own? Commented Mar 12, 2011 at 0:49
  • The array is actually with another array built by another module of the framework (its OSC) and I do not know enough about the creation class to mess with it. Commented Mar 12, 2011 at 0:57

2 Answers 2

3
//Go over all products
foreach($array as $value){
    $weight = $value['weight'];
    // query goes here
}
1
  • My last update is working... I'm going to try to find away to reduce the queries... Commented Mar 13, 2011 at 9:48
1

I feel like I'm not understanding what you're asking but why not try

foreach($items as $id => $item)
{
    $cost = $item['weight'] * $item['qty'];
}
1
  • -1 You completely ignored the requirement to retrieve rate from the database. Commented Mar 12, 2011 at 1:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.