Trying to unload goods from the store's basket using PHPExcel

  $export_cart_products = $_SESSION['cart']['products']; foreach ($export_cart_products as $key => $row) { $phpexcel->getActiveSheet()->setCellValue("A$i", $row['product_id']); $string = $row['product']; $phpexcel->getActiveSheet()->setCellValueExplicit("B$i", $string, PHPExcel_Cell_DataType::TYPE_STRING); $phpexcel->getActiveSheet()->setCellValue("C$i", $row['price']); $phpexcel->getActiveSheet()->setCellValue("D$i", $row['amount']); $result_array = array(); foreach ($row['product_options'] as $option_id => $variant_id) { $option_name = db_get_fields("SELECT option_name FROM ?:product_options_descriptions WHERE option_id = ?i", $option_id, $variant_id, CART_LANGUAGE); $variant_name = db_get_fields("SELECT variant_name FROM ?:product_option_variants_descriptions WHERE variant_id = ?i", $variant_id, $option_id, CART_LANGUAGE); }; 

__ placeholders?: replaced by the value of cscart_

in the second foreach I try to display options for the product, options come in the form of an array

 array = [4] => 34, [3] => 12 

each key and each value is the name of "color", "size", "XXL (large size)", "Blue", "Red", etc. accordingly, you need a query to the database in order to display them (I suspect that my query to the database is not entirely correct). Now we only get the version with one pair "Color: Blue" or "size: XL", but the product has several such values ​​(color, size ...).

How to make it so that it displays all the options of the goods.

Completely confused, but the project must be done. xls is correctly stored, only without options, and options are needed.

    1 answer 1

     foreach ($row['product_options'] as $option_id => $variant_id) { $option_name = db_get_fields("SELECT option_name FROM ?:product_options_descriptions WHERE option_id = ?i", $option_id, $variant_id, CART_LANGUAGE); $variant_name = db_get_fields("SELECT variant_name FROM ?:product_option_variants_descriptions WHERE variant_id = ?i", $variant_id, $option_id, CART_LANGUAGE); }; 

    Here the values ​​are overwritten in the cycle and only one output will be received, whatever one may say.

    An array must be stored if an array is needed. For example:

      foreach ($row['product_options'] as $option_id => $variant_id) { $option_name[] = db_get_fields("SELECT option_name FROM ?:product_options_descriptions WHERE option_id = ?i", $option_id, $variant_id, CART_LANGUAGE); $variant_name[] = db_get_fields("SELECT variant_name FROM ?:product_option_variants_descriptions WHERE variant_id = ?i", $variant_id, $option_id, CART_LANGUAGE); }; 

    But it may be better to use an associative array with foreach to write values ​​or a for loop with indices with an array.

    Here is an example with an associative one:

      foreach ($row['product_options'] as $option_id => $variant_id) { $option_name = db_get_fields("SELECT option_name FROM ?:product_options_descriptions WHERE option_id = ?i", $option_id, $variant_id, CART_LANGUAGE); $variant_name = db_get_fields("SELECT variant_name FROM ?:product_option_variants_descriptions WHERE variant_id = ?i", $variant_id, $option_id, CART_LANGUAGE); $options[] = ['option_name' => $option_name, 'variant_name' => $variant_name]; }; 

    Here, depending on how you need to further process the array.

    • Thank you, but unfortunately for some reason it still gets one key pair => value, i.e. color => blue, and this product also has size, so I’ll not bring them out together. - Eugene Starkov
    • How do you verify that the array is output after processing? - Oleg Dmitrochenko
    • this array comes in when printing $ fn_print_die ($ row ['product_options']); Array ([4] => 18, [3] => 13) where 4 is the color, 3 is the size, respectively 18 and 13 values - Evgeny Starkov
    • checking fn_print_die ($ options) - Eugene Starkov
    • Difficult to answer without seeing the code after processing the loop if you haven’t solved the problem yet, write the cycle in full or at least those places where full processing of the data takes place and the product criteria are saved - Oleg Dmitrochenko