It is impossible to select all the child categories of the main category. The category table structure: category table structure

Each category has parent_id. If parent_id = 0, this means parent. You need to select all the category_id of the parent in one array. Here is a piece of code I worked with. But it does not display 3-4-5 categories nesting: $ filter_category_id here is the parent

if ($filter_category_id) { $this->load->model('catalog/category'); $cats = array($filter_category_id); $cats = $this->model_catalog_category->getChildCategories($filter_category_id, $cats); $cats_merge = array(); if ($cats) { foreach ($cats as $cat) { $catsa = $this->model_catalog_category->getChildCategories($cat); $cats_merge = array_merge($cats_merge, $catsa); } } $cats_merge = array_merge($cats_merge, $cats); $cats_merge = array_unique($cats_merge); return $cats_merge; } 

And then refer to the table:

 public function getChildCategories($id, $cats = array()){ $sql = "SELECT * FROM category WHERE parent_id = " . $id; $result = $this->db->query($sql); $rows = $result->rows; if($rows){ foreach($rows as $row){ $cats[] = $row['category_id']; $this->getChildCategories($row['category_id'], $cats); } } return $cats; } 

Found a solution:

  public function getCategoriesandSubs($parent_id = 0) { static $cat_branch = array(); $results = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND c.status = 1"); $num_rows = count($results->rows); if($num_rows != 0) { foreach ($results->rows as $result) { $cat_branch[] = $result['category_id']; $this->getCategoriesandSubs($result['category_id']); } } if(count($cat_branch) == 0) { $cat_branch[] = $parent_id; } return $cat_branch; 

}

  • Immediately, probably, you can’t offer anything individually, BUT, I see a defect here: $this->getChildCategories($row['category_id'], $cats); , you have this function performed, but the result is lost because it is not saved anywhere, maybe it makes sense to do so? return $this->getChildCategories($row['category_id'], $cats); - Add return - Manitikyl

1 answer 1

Naturally, this request does not give out subcategories, since it is not spelled out in it.
Here it seems that the answer is: https://stackoverflow.com/questions/16697446/how-do-i-represent-a-tree-structure-in-a-mysql-table
Does not open. Well, then: https://www.sitepoint.com/hierarchical-data-database/ - this opens, there are examples, just MySQL + PHP.
Although I would still think about changing the database and the way the tree is represented, if this is not critical.

  • Хотя я б таки подумал над сменой БД - what kind of Хотя я б таки подумал над сменой БД are you going to change? - Manitikyl
  • So I’m not on any;) <br/> In general, Postgres has user data types. You can write a lot of interesting things on them. - Pitch Chuchelko
  • As a variant of the representation of a tree for which Postgres is not needed: we represent nodes as bit strings. The string is the path to the node — 01 from the node to the first son, 10 from the node to the younger brother, 00 is the way completed. Then all descendants are selected by a simple prefix request. Yes, forgot to add that Postgres is able hierarchical queries to your tables. - Pitch Chuchelko
  • Recursive queries are available everywhere for 10 years, as well as working with bits. - Manitikyl
  • In MySQL 8.0 is not found. The documentation suggests a recursive procedure. In any case, other methods simply must be faster and more convenient (personal opinion). - Pitch Chuchelko