1

PHPでは、このようなコードがあります

  $discounts =  $this->model_catalog_product->getProductDiscounts($product_id);
      $product_discounts[] = array();
      foreach($discounts as $discount) {
        $product_discounts[] = array(
          'quantity' => $discount['quantity'],
          'price'    => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')))
        );
      }

今私が作ったとき、それは私の配列をこのように示しました

Array ( [0] => Array ( [product_discount_id] => 483 [product_id] => 43 [customer_group_id] => 8 [quantity] => 2 [priority] => 1 [price] => 345.0000 [date_start] => 0000-00-00 [date_end] => 0000-00-00 ) [1] => Array ( [product_discount_id] => 484 [product_id] => 43 [customer_group_id] => 8 [quantity] => 4 [priority] => 2 [price] => 784.0000 [date_start] => 0000-00-00 [date_end] => 0000-00-00 ) [2] => Array ( [product_discount_id] => 485 [product_id] => 43 [customer_group_id] => 8 [quantity] => 5 [priority] => 3 [price] => 786.0000 [date_start] => 0000-00-00 [date_end] => 0000-00-00 ) )

しかし、配列から割引数量と割引価格の値を取得するようにコードを作成したとき

 <?php foreach ($discounts as $discount) {
    echo sprintf($text_discount, $discount['quantity'], $discount['price']); ?>
    } ?>

このようなエラーが発生しましたUndefined variable: discounts

4

3 に答える 3

1

これを交換してください

$product_discounts[] = array();

$product_discounts = array();
于 2012-11-24T07:59:26.707 に答える
0

$discounts定義されたことはありません。である必要があります$product_discounts。それがあなたが得ている理由undefined variable discountsです。

<?php
foreach ($product_discounts as $discount) {
    echo sprintf($text_discount, $discount['quantity'], $discount['price']); ?>
}?>
于 2012-11-24T08:02:12.310 に答える
0

これを試して

$discounts =  $this->model_catalog_product->getProductDiscounts($product_id);
  $product_discounts = array();
  foreach($discounts as $discount) {
    $product_discounts = array(
      'quantity' => $discount['quantity'],
      'price'    => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')))
    );
  }
于 2012-11-24T08:18:46.700 に答える