2016-09-28 9 views
0

Ich verstehe nicht genau warum, aber wenn ich diese Funktion schreibe, funktioniert die Schleife für doesn '.Berechnung innerhalb einer Schleife macht nicht Ergebnis

Haben Sie eine Idee?

danke

$qty ist von einem Kunden

$id machen: id des

$products_price Preis des Produkts

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 

// Schleife am wichtigsten weniger Produkt wichtig

+1

$ new_price wird auf jeder Schleifeniterationslatenzzeit überschrieben. – nogad

+0

Welche Datenbank ist das und welche Datenbank-API verwenden Sie? – RiggsFolly

+0

Wenn dies die MYSQL PDO API ist, würde ich sagen, dass die Abfrage fehlschlagen sollte. Testen Sie das Ergebnis der Vorbereitung und die Ausführung – RiggsFolly

Antwort

0

Dort wird die Lösung, die ich gefunden, das Problem zu lösen

$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; 
Verwandte Themen