循环内的计算确实没有结果

时间:2016-09-28 22:12:16

标签: php

我不明白为什么,但是当我写这个函数时,我不知道这个函数的循环。工作

你有什么想法吗?

谢谢

$qty由客户制作

$id:产品的ID

$products_price产品价格

public function getProductsNewPriceByDiscountQuantity($id, $qty, $products_price) {
  $OSCOM_Db = Registry::get('Db');
  $OSCOM_Customer = Registry::get('Customer');
$QprodutsQuantityDiscount= $OSCOM_Db->prepare('select discount_quantity, 
                                                            discount_customer
                                                      from :table_products_discount_quantity
                                                      where products_id = :products_id
                                                      and customers_group_id = :customers_group_id
                                                      and discount_quantity <> 0
                                                    ');
      $QprodutsQuantityDiscount->bindInt(':products_id', $id );
      $QprodutsQuantityDiscount->bindInt(':customers_group_id', $OSCOM_Customer->getCustomersGroupID());

  $QprodutsQuantityDiscount->execute();

  while ($QprodutsQuantityDiscount->fetch()) {
    $discount_quantity[] = $QprodutsQuantityDiscount->valueInt('discount_quantity');
    $discount_customer[] = $QprodutsQuantityDiscount->valueDecimal('discount_customer');
  }

  $nb_discount = count($discount_quantity); //  
  $n = $nb_discount -1; // 0,1,2 for the table indice

//将最重要的内容循环到不太重要的

      for ($n; $n < 0; $n--) {
//        print_r('qty : ' .$qty . '<br />');
//        print_r('discount : ' .$discount_quantity[$n] . '<br />');

        if($qty >= $discount_quantity[$n]){
          $new_price = $products_price - ($products_price * ($discount_customer[$n] / 100));
        }
      }

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的解决方案

  $nb_discount = count($discount_quantity); // dans ton exemple 3 discounts
  $i = $nb_discount -1; // 0,1,2 pour les indices des tableaux de ton exemple

  for ($i; $i > 0; $i--) {
    if($qty > $discount_quantity[$i]) {
      $new_discount_price  = ($products_price - ($products_price * ($discount_customer[$i] / 100))) * $qty;
      break;
    }
  }

  return $new_discount_price;