P
P
PoodingRex2017-04-11 16:22:55
opencart
PoodingRex, 2017-04-11 16:22:55

Why does foreach (opencart) only return the last result?

I welcome everyone. Please tell me what I'm doing wrong. I'm trying to display the following menu:
-Category 1
---manufacturer 2
---manufacturer 6
-Category 2
---manufacturer 3
---manufacturer 5
---manufacturer 2
-Category 3
---manufacturer 1
i.e. the manufacturers contained in it should appear under the category.
And almost everything turns out, except for one thing - in all categories, manufacturers belonging to the last category are displayed.
In the controller it looks like this:

$categories = $this->model_catalog_category->getCategories(0);

    foreach ($categories as $category) {
      if ($category['top']) {
        

                // Level 2
        $children_data = array(); //1

        $children = $this->model_catalog_category->getCategories($category['category_id']); //2

        foreach ($children as $child) { //3
          $filter_data = array(
            'filter_category_id'  => $child['category_id'],
            'filter_sub_category' => true
          );

          $children_data[] = array(
            'name'  => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
            'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
          );
        }
                
        // Level 1
        $data['categories'][] = array(
          'name'     => $category['name'],
          'children' => $children_data,
          'column'   => $category['column'] ? $category['column'] : 1,
          'href'     => $this->url->link('product/category', 'path=' . $category['category_id'])
        );
                
                
                //manufacturers menu
                
                $manufacturers = $this->model_catalog_category->getManufacturers($category['category_id']); //2
                
                $data['manufacturers'] = array(); //1
                foreach ($manufacturers as $manufacturer)  { //3
                       $data['manufacturers'][] = array (
                       'manufacturer_id' => $manufacturer['manufacturer_id'],
                       'thumb' => $manufacturer['image'],
                       'name' => $manufacturer['name'],
                       'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '&manufacturer_id=' . $manufacturer['manufacturer_id'])
                        );
                }
                 
                
      }
    }

In view like this:
<?php foreach ($categories as $category) { ?>
            <b><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a></b> <br> 
            <?php foreach ($manufacturers as $manufacturer) { ?>
                     ----    <a href="<?php echo $manufacturer['href'] ?>"><?php echo $manufacturer['name'] ?> (<?php echo $manufacturer['thumb'] ?>)</a> <br>
            <?php } ?>
        <?php } ?>

If I do the same for level 2, everything works fine. With manufacturers - nothing =(
Thank you in advance for your help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question