calculation inside a loop does'nt make result

Tags: php
By : kurama
Source: Stackoverflow.com
Question!

I don't understand exactly why but when I write this function, the loop for doesn' work.

Do you have an idea ?

thank you

$qty is make by a customer

$id : id of the product

$products_price price of the product

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

// Loop the most important to less important

      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));
        }
      }
By : kurama


Answers

There the solution i found to resolve the problem

  $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;
By : kurama


It sounds like you're looking for a write-once-run-anywhere solution.You can look at:

  • web-based solutions (e.g. progressive web apps)
  • hybrid solutions (e.g. Ionic)
  • near-native solutions (e.g. React Native, NativeScript)
  • cross-compiling solutions (eg. Intel XDK)

...and probably a few other categories.

If none of those suit your needs, you will have to:

  • embed the logic in Firebase security rules and/or
  • write an intermediary or auxiliary server (see this article on application architectures) and/or
  • duplicate logic into apps written in the native technology of iOS and Android


If you don't need to pass any variable after creating the Fragment instance, pass the parameter in the constructor like this.

PersonDetailFragment fragment = new PersonDetailFragment(personID);

Being the constructor of the Fragment like this:

public PersonDetailFragment(String personID) {
        // Required empty public constructor
    }

If you want to communicate constantly with the activity you can implement interfaces. Read this article, It's a good point to start with them, listeners.

https://developer.android.com/training/basics/fragments/communicating.html

By : Enrique


This video can help you solving your question :)
By: admin