PHP - 计算会话数组中的数量项

时间:2009-12-16 17:16:46

标签: php arrays session shopping-cart

如何计算item-qty和当前代码: -

$q = $_POST['item-qty'];
$i = count($q);
$k = 0;

while ($k < $i) {
  $select = 'SELECT * FROM location';
  $query = $db->rq($select);
  $price = $db->fetch($query);

  if ($_POST['item-qty'][$k] < 3) {

      $get = $price['normal_price'];
      $price = $get * $_POST['item-qty'][$k];

      $_SESSION['order'][$_POST['item-id'][$k]] = array(
      "item-id" => $_POST['item-id'][$k],
      "item-qty" => $_POST['item-qty'][$k],
      "item-name" => $_POST['item-name'][$k],
      "item-price" => $price,
       );

  } else {

      $get = $price['member_price'];
      $price = $get * $_POST['item-qty'][$k];

      $_SESSION['order'][$_POST['item-id'][$k]] = array(
      "item-id" => $_POST['item-id'][$k],
      "item-qty" => $_POST['item-qty'][$k],
      "item-name" => $_POST['item-name'][$k],
      "item-price" => $price,
      );
  }
}

这里是数组输出

Array
(
[order] => Array
    (
        [1] => Array
            (
                [item-id] => 1
                [item-qty] => 1
                [item-name] => Adidas
                [item-price] => 100
            )

        [2] => Array
            (
                [item-id] => 2
                [item-qty] => 1
                [item-name] => Nike
                [item-price] => 150
            )

    )

)

问题:

  1. 如果item-qty(在所有数组中)大于或等于3,如何实现其他代码将使用$price['member_price']
  2. 让我知道:)。

1 个答案:

答案 0 :(得分:1)

我猜你的意思是所有东西的总项目数量?

$qty_sum = 0
 foreach($_SESSION['order'] as $order){
     $qty_sum += $order['item-qty'];
}