内容选择分开,如何只有一个?

时间:2018-02-10 22:51:50

标签: php

代码显示分开选择。我认为循环内部有问题。我必须只有一个内在的地方。

你知道问题的来源吗?

感谢。

内容元素

array (size=7)
  0 => 
    array (size=7)
      'products_option_id' => string '220' (length=3)
      'products_option_value' => 
        array (size=1)
          0 => 
            array (size=13)
              ...
      'option_id' => string '40' (length=2)
      'name' => string 'Taille' (length=6)
      'type' => string 'select' (length=6)
      'value' => null
      'required' => null
  1 => 
    array (size=7)
      'products_option_id' => string '219' (length=3)
      'products_option_value' => 
        array (size=2)
          0 => 
            array (size=13)
              ...
          1 => 
            array (size=13)
              ...
      'option_id' => string '40' (length=2)
      'name' => string 'Taille' (length=6)
      'type' => string 'select' (length=6)
      'value' => null
      'required' => null

现在循环

      foreach ($options_array as $key => $option) {
        if ($option['type'] == 'select') {

          if ($option['products_option_value'][$key] !== null) {
            $products_options_content_display .= '<div class="form-group">'; // required or not
            $products_options_content_display .= '<label class="control-label ProductsInfoOptionSelect" for="input-option' . $option['products_option_id'] . '">' . $option['name'] . '</label>';
            $products_options_content_display .= '<select name="products_option_id' . $option['products_option_id'] . '" id="input-option' . $option['products_option_id'] . '" class="form-control">';

            $products_options_content_display .= '<option value="">' . OSCOM::getDef('text_select') . '</option>';

//的var_dump($选项[&#39; products_option_value&#39;] [$键]);

            if (!is_null($option['products_option_value'][$key]['products_option_model'])) {
              $model = $option['products_option_value'][$key]['products_option_model'];
            }

            $products_options_content_display .= '<option value="' . $option['products_option_value'][$key]['products_option_value_id'] . '">' . $model . ' - ' . $option['products_option_value'][$key]['name'];

            if ($option['products_option_value'][$key]['price'] > 0) {
              $products_options_content_display .= $option['products_option_value'][$key]['price_prefix'] . ' ' . $option['products_option_value'][$key]['price'];
            }

            $products_options_content_display .= '</option>';
            $products_options_content_display .= '</select>';
            $products_options_content_display .= ' </div>';
          }
        }
      }

你可以在那里看到结果:https://jsfiddle.net/wx0gvf6L/

添加信息

var_dump($option['products_option_value'][$key]);

    array (size=13)
      'products_option_value_id' => string '175' (length=3)
      'option_value_id' => string '180' (length=3)
      'name' => string 'M' (length=1)
      'image' => null
      'quantity' => string '100' (length=3)
      'subtract' => string '0' (length=1)
      'price' => string '0.0000' (length=6)
      'price_prefix' => string '+' (length=1)
      'weight' => string '0.00' (length=4)
      'weight_prefix' => string '+' (length=1)
      'customers_group_id' => string '99' (length=2)
      'products_option_model' => string 'SizeM' (length=5)
      'option_tax_class_id' => string '0' (length=1)
    /home/www/clicshopping_test/boutique/sources/template/Default/modules/modules_products_info/pi_products_info_options_new.php:92:
    array (size=13)
      'products_option_value_id' => string '174' (length=3)
      'option_value_id' => string '179' (length=3)
      'name' => string 'S' (length=1)
      'image' => null
      'quantity' => string '100' (length=3)
      'subtract' => string '0' (length=1)
      'price' => string '1.0000' (length=6)
      'price_prefix' => string '+' (length=1)
      'weight' => string '0.00' (length=4)
      'weight_prefix' => string '+' (length=1)
      'customers_group_id' => string '99' (length=2)
      'products_option_model' => string 'SizeS' (length=5)
      'option_tax_class_id' => string '0' (length=1)


json format
[
   {
      "products_option_id": "240",
      "option_id": "41",
      "name": "test",
      "type": "select",
      "value": null,
      "required": null,
      "products_option_value": [
         {
            "products_option_value_id": "187",
            "option_value_id": "184",
            "name": "test2",
            "image": null,
            "quantity": "0",
            "subtract": "0",
            "price": "10.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         }
      ]
   },
   {
      "products_option_id": "239",
      "option_id": "41",
      "name": "test",
      "type": "select",
      "value": null,
      "required": null,
      "products_option_value": [
         {
            "products_option_value_id": "187",
            "option_value_id": "184",
            "name": "test2",
            "image": null,
            "quantity": "0",
            "subtract": "0",
            "price": "10.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "186",
            "option_value_id": "185",
            "name": "test12",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "20.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         }
      ]
   },
   {
      "products_option_id": "238",
      "option_id": "40",
      "name": "Taille",
      "type": "select",
      "value": null,
      "required": null,
      "products_option_value": [
         {
            "products_option_value_id": "187",
            "option_value_id": "184",
            "name": "test2",
            "image": null,
            "quantity": "0",
            "subtract": "0",
            "price": "10.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "186",
            "option_value_id": "185",
            "name": "test12",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "20.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         }
      ]
   },
   {
      "products_option_id": "237",
      "option_id": "40",
      "name": "Taille",
      "type": "select",
      "value": null,
      "required": null,
      "products_option_value": [
         {
            "products_option_value_id": "187",
            "option_value_id": "184",
            "name": "test2",
            "image": null,
            "quantity": "0",
            "subtract": "0",
            "price": "10.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "186",
            "option_value_id": "185",
            "name": "test12",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "20.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "185",
            "option_value_id": "179",
            "name": "S",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "1.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "SizeS",
            "option_tax_class_id": "0"
         }
      ]
   },
   {
      "products_option_id": "236",
      "option_id": "40",
      "name": "Taille",
      "type": "select",
      "value": null,
      "required": null,
      "products_option_value": [
         {
            "products_option_value_id": "187",
            "option_value_id": "184",
            "name": "test2",
            "image": null,
            "quantity": "0",
            "subtract": "0",
            "price": "10.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "186",
            "option_value_id": "185",
            "name": "test12",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "20.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "185",
            "option_value_id": "179",
            "name": "S",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "1.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "SizeS",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "184",
            "option_value_id": "180",
            "name": "M",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "0.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "SizeM",
            "option_tax_class_id": "0"
         }
      ]
   },
   {
      "products_option_id": "235",
      "option_id": "40",
      "name": "Taille",
      "type": "select",
      "value": null,
      "required": null,
      "products_option_value": [
         {
            "products_option_value_id": "187",
            "option_value_id": "184",
            "name": "test2",
            "image": null,
            "quantity": "0",
            "subtract": "0",
            "price": "10.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "186",
            "option_value_id": "185",
            "name": "test12",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "20.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "185",
            "option_value_id": "179",
            "name": "S",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "1.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "SizeS",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "184",
            "option_value_id": "180",
            "name": "M",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "0.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "SizeM",
            "option_tax_class_id": "0"
         }
      ]
   },
   {
      "products_option_id": "233",
      "option_id": "38",
      "name": "Couleur",
      "type": "radio",
      "value": null,
      "required": null,
      "products_option_value": [
         {
            "products_option_value_id": "187",
            "option_value_id": "184",
            "name": "test2",
            "image": null,
            "quantity": "0",
            "subtract": "0",
            "price": "10.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "186",
            "option_value_id": "185",
            "name": "test12",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "20.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "185",
            "option_value_id": "179",
            "name": "S",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "1.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "SizeS",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "184",
            "option_value_id": "180",
            "name": "M",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "0.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "SizeM",
            "option_tax_class_id": "0"
         }
      ]
   },
   {
      "products_option_id": "232",
      "option_id": "38",
      "name": "Couleur",
      "type": "radio",
      "value": null,
      "required": null,
      "products_option_value": [
         {
            "products_option_value_id": "187",
            "option_value_id": "184",
            "name": "test2",
            "image": null,
            "quantity": "0",
            "subtract": "0",
            "price": "10.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "186",
            "option_value_id": "185",
            "name": "test12",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "20.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "185",
            "option_value_id": "179",
            "name": "S",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "1.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "SizeS",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "184",
            "option_value_id": "180",
            "name": "M",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "0.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "SizeM",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "183",
            "option_value_id": "183",
            "name": "bleu",
            "image": "",
            "quantity": "50",
            "subtract": "0",
            "price": "0.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "REF-2",
            "option_tax_class_id": "0"
         }
      ]
   },
   {
      "products_option_id": "231",
      "option_id": "38",
      "name": "Couleur",
      "type": "radio",
      "value": null,
      "required": null,
      "products_option_value": [
         {
            "products_option_value_id": "187",
            "option_value_id": "184",
            "name": "test2",
            "image": null,
            "quantity": "0",
            "subtract": "0",
            "price": "10.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "186",
            "option_value_id": "185",
            "name": "test12",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "20.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "185",
            "option_value_id": "179",
            "name": "S",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "1.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "SizeS",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "184",
            "option_value_id": "180",
            "name": "M",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "0.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "SizeM",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "183",
            "option_value_id": "183",
            "name": "bleu",
            "image": "",
            "quantity": "50",
            "subtract": "0",
            "price": "0.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "REF-2",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "182",
            "option_value_id": "182",
            "name": "vert",
            "image": "",
            "quantity": "100",
            "subtract": "0",
            "price": "10.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "REF-1",
            "option_tax_class_id": "0"
         }
      ]
   },
   {
      "products_option_id": "234",
      "option_id": "39",
      "name": "Commentaires",
      "type": "textarea",
      "value": "0",
      "required": null,
      "products_option_value": [
         {
            "products_option_value_id": "187",
            "option_value_id": "184",
            "name": "test2",
            "image": null,
            "quantity": "0",
            "subtract": "0",
            "price": "10.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "186",
            "option_value_id": "185",
            "name": "test12",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "20.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "0",
            "products_option_model": "============",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "185",
            "option_value_id": "179",
            "name": "S",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "1.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "SizeS",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "184",
            "option_value_id": "180",
            "name": "M",
            "image": null,
            "quantity": "100",
            "subtract": "0",
            "price": "0.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "SizeM",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "183",
            "option_value_id": "183",
            "name": "bleu",
            "image": "",
            "quantity": "50",
            "subtract": "0",
            "price": "0.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "REF-2",
            "option_tax_class_id": "0"
         },
         {
            "products_option_value_id": "182",
            "option_value_id": "182",
            "name": "vert",
            "image": "",
            "quantity": "100",
            "subtract": "0",
            "price": "10.0000",
            "price_prefix": "+",
            "weight": "0.00",
            "weight_prefix": "+",
            "customers_group_id": "99",
            "products_option_model": "REF-1",
            "option_tax_class_id": "0"
         }
      ]
   }
]

1 个答案:

答案 0 :(得分:0)

更类似于此:

foreach ($options_array as $option) {
    if ($option['type'] == 'select') {
        $out .= '<label>' . $option['name'] . '</label>';
        $out .= '<select name="products_option_id' . $option['products_option_id'] . '" id="input-option' . $option['products_option_id'] . '" class="form-control">';
        $out .= '<option value="">' . OSCOM::getDef('text_select') . '</option>';
        foreach($option['products_option_value'] as $value) {
            $out .= '<option value="' . $value['products_option_value_id'] . '">' . $value['products_option_model'] . ' - ' . $value['name'];
            if ($value['price'] > 0) {
                $out .= $value['price_prefix'] . ' ' . $value['price'];
            }
            $out .= '</option>';
        }
        $out .= '</select>';
    }
}