A
A
Alexey2020-05-23 17:08:09
PHP
Alexey, 2020-05-23 17:08:09

How to compare and display data correctly?

Good to everyone!

Prompt as it is correct to compare and deduce in an array the data from a DB.

How to compare fields $category['category_id'] and $category['parent_id'] and if they match then add them to $child array.

There is already an array like this:

[62] => Array
        (
            [category_id] => 62
            [image] => 
            [parent_id] => 0
            [top] => 1
            [column] => 1
            [sort_order] => 0
            [status] => 1
            [date_added] => 2020-05-23 13:47:51
            [date_modified] => 2020-05-23 14:47:26
            [language_id] => 1
            [name] => Металлопрокат
            [description] => 
            [meta_title] => 
            [meta_h1] => 
            [meta_description] => 
            [meta_keyword] => 
            [store_id] => 0
            [href] => http://test.bbden.ru/index.php?route=product/category&path=62
            [childs] => Array
                (
                    [63] => Array
                        (
                            [category_id] => 63
                            [image] => 
                            [parent_id] => 62
                            [top] => 0
                            [column] => 1
                            [sort_order] => 0
                            [status] => 1
                            [date_added] => 2020-05-23 13:48:32
                            [date_modified] => 2020-05-23 13:48:32
                            [language_id] => 1
                            [name] => Труба металическая
                            [description] => 
                            [meta_title] => 
                            [meta_h1] => 
                            [meta_description] => 
                            [meta_keyword] => 
                            [store_id] => 0
                            [href] => http://test.bbden.ru/index.php?route=product/category&path=63
                            [childs] => Array
                                (
                                    [64] => Array
                                        (
                                            [category_id] => 64
                                            [image] => 
                                            [parent_id] => 63
                                            [top] => 0
                                            [column] => 1
                                            [sort_order] => 0
                                            [status] => 1
                                            [date_added] => 2020-05-23 13:48:58
                                            [date_modified] => 2020-05-23 13:49:12
                                            [language_id] => 1
                                            [name] => Труба электросварная
                                            [description] => 
                                            [meta_title] => 
                                            [meta_h1] => 
                                            [meta_description] => 
                                            [meta_keyword] => 
                                            [store_id] => 0
                                            [href] => http://test.bbden.ru/index.php?route=product/category&path=64
                                        )

                                )

                        )

                )

        )


I get it like this:

<?php
class ControllerExtensionModuleCategorywallhome extends Controller {
  
  protected function debug($data){
    echo '<pre>'. print_r($data, true) .'</pre>';
  }

  public function index() {
    $this->load->language('extension/module/categorywallhome');

    $data['heading_title'] = $this->language->get('heading_title');

    if (isset($this->request->get['path'])) {
      $parts = explode('_', (string)$this->request->get['path']);
    } else {
      $parts = array();
    }

    if (!empty($parts)) {
      $data['active'] = end($parts);
    }else{
      $data['active'] = 0;
    }
    

    $this->load->model('catalog/categorywallhome');

    $data['categories'] = array();

    $categories = $this->model_catalog_categorywallhome->getwallCategories();
    
    foreach($categories as $id => $category){
      $categories[$id]['href'] =  $this->url->link('product/category', 'path=' . $category['category_id']);
    }
    $data['categories_tree'] = $categories = $this->model_catalog_categorywallhome->getMapTree($categories);
    $this->debug($categories);


    return $this->load->view('extension/module/categorywallhome', $data);
  }
}


The query itself in the database:

<?php
class ModelCatalogCategorywallhome extends Model {
  public function getwallCategories() {
    $query = $this->db->query(
      "SELECT * FROM " . DB_PREFIX . "category c 
      LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) 
      LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) 
      WHERE cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'  AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");

    $cats = [];
    foreach($query->rows as $row){
      $cats[$row['category_id']] = $row;
    }
    return $cats;
  }
  public function getMapTree($dataset){
    $tree = array();

    foreach ($dataset as $id => &$node) {
      if (!$node['parent_id']) {
        $tree[$id] = &$node;
      }else{
        $dataset[$node['parent_id']]['childs'][$id] = &$node;
      }
    }
    return $tree;
  }
}


At the output, you need to get such an array only with children in children:
Array
(
    [0] => Array
        (
            [category_id] => 62
            [name] => Металлопрокат
            [children] => Array
                (
                )

            [href] => http://test.bbden.ru/index.php?route=product/category&path=62
        )

    [1] => Array
        (
            [category_id] => 59
            [name] => Пиломатериалы
            [children] => Array
                (
                )

            [href] => http://test.bbden.ru/index.php?route=product/category&path=59
        )

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