我有一个包含数组的数组:
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
)
)
我需要将每个产品的重量与MySQL DB(我可以这样做)相比较,将其乘以qty并返回成本;然后对下一个做同样的事情,等等。
最终更新(具有完整的班级功能):
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;
}
如果我找到减少查询次数的方法,我会更新。
杰西
答案 0 :(得分:3)
//Go over all products
foreach($array as $value){
$weight = $value['weight'];
// query goes here
}
答案 1 :(得分:1)
我觉得我不明白你在问什么,但为什么不试试
foreach($items as $id => $item)
{
$cost = $item['weight'] * $item['qty'];
}