W
W
WebforSelf2021-07-07 20:15:18
PHP
WebforSelf, 2021-07-07 20:15:18

Where is the error in PHP array?

There is a function that collects information on the product in order to collect it later in EXCEL

private function prepareProducts($filter = array()) {
    $products = array();
        foreach($this->products->get_products($filter) as $p) {
            $products[$p->id] = $p;
        }
  

   
        if(!empty($products)) {
            
      $products_ids = array_keys($products);
      foreach($products as $product) {
        $product->variants = array();
        $product->options = $product->id;
      }
      
   
      $variants = $this->variants->get_variants(array('product_id'=>$products_ids));

      foreach($variants as $variant) {
        $products[$variant->product_id]->variants[] = $variant;
      }

      foreach($products as $product) {
                if(isset($product->variants[0])) {
                    $product->variant = $product->variants[0];
                }
      }
      
    //Получим свойства
    $options = $this->features->get_product_options($product->id);

    foreach ($options as $option) {
      if(empty($products[$option->product_id]->options))
    $products[$option->product_id]->options = $option->name.' - '.$option->value;	
          else
        $products[$option->product_id]->options .= ', '.$option->name.' - '.$option->value;
    }	
            
      
    // Изображения товаров
 		$images = $this->products->get_images(array('product_id'=>$products_ids));
 		foreach($images as $image)
 		{
 			// Добавляем изображения к товару чезер запятую 
 			if(empty($products[$image->product_id]->images))
 				$products[$image->product_id]->images = $this->config->root_url.'/files/originals/'.$image->filename;
 			else
 				$products[$image->product_id]->images .= ', '.$this->config->root_url.'/files/originals/'.$image->filename;
 		}
      

 		}
        return $products;
    }


The important element is // Get properties and // Product images

There we get an array of photo titles and property names.

the function we call to get the properties

public function get_product_options($product_id)
  {
    $query = $this->db->placehold("SELECT f.id as feature_id, f.name, po.value, po.product_id FROM __options po LEFT JOIN __features f ON f.id=po.feature_id
                    WHERE po.product_id in([email protected]) ORDER BY f.position", (array)$product_id);

    $this->db->query($query);
    $res = $this->db->results();

    return $res;
  }


image acquisition function

function get_images($filter = array())
  {		
    $product_id_filter = '';
    $group_by = '';

    if(!empty($filter['product_id']))
      $product_id_filter = $this->db->placehold('AND i.product_id in([email protected])', (array)$filter['product_id']);

    // images
    $query = $this->db->placehold("SELECT i.id, i.product_id, i.name, i.filename, i.position
                  FROM __images AS i WHERE 1 $product_id_filter $group_by ORDER BY i.product_id, i.position");
    $this->db->query($query);
    return $this->db->results();
  }


Then I display the photo and properties

$file = $product->images;
      $po = $product->options;			
      $sheet->setCellValue('F'.$i, strval($file));														   
          $sheet->setCellValue('G'.$i, strval($po));


As a result, the images are displayed as needed, but the properties are displayed as needed, but only at the end
60e5e0e3db9d6003296921.png

This part is responsible for collecting the properties
//Получим свойства
    $options = $this->features->get_product_options($product->id);

    foreach ($options as $option) {
      if(empty($products[$option->product_id]->options))
    $products[$option->product_id]->options = $option->name.' - '.$option->value;	
          else
        $products[$option->product_id]->options .= ', '.$option->name.' - '.$option->value;
    }


and the get_product_options function, but I don’t know why it only displays the last product

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-07-07
@WebforSelf

What would you like if:

//Получим свойства
    $options = $this->features->get_product_options($product->id);

?
$products_idsprobably meant?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question