There are 3 tables:

  1. store (id,name) - table of stores

  2. category (id,parent_id,name) - table of categories of unlimited nesting

  3. store_category_rel (id, store_id, category_id) - the link table store-> category

It is necessary to display the branch to the subcategory, which is listed in the store_category_rel table.

 $id_shop=10; // id нужного магазина $cats=array(); $result=array(); function build_tree($cats,$pid,$only_parent = false){ if(is_array($cats) and isset($cats[$pid])){ $tree = '<ul>'; if($only_parent==false){ foreach($cats[$pid] as $cat){ $tree .= '<li>'.$cat['name']; $tree .= build_tree($cats,$cat['id']); $tree .= '</li>'; } }elseif(is_numeric($only_parent)){ $cat = $cats[$pid][$only_parent]; $tree .= '<li>'.$cat['name']; $tree .= build_tree($cats,$cat['id']); $tree .= '</li>'; } $tree .= '</ul>'; } else return null; return $tree; } function find_parent($tmp, $cur_id){ if($tmp[$cur_id][0]['pid']!=0){ return find_parent($tmp,$tmp[$cur_id][0]['pid']); } return (int)$tmp[$cur_id][0]['id']; } $row=$base->query("SELECT * FROM category"); foreach($row AS $row){ $cats_ID[$row['id']][] = $row; $cats[$row['pid']][$row['id']]= $row; } $row=$base->query("SELECT * FROM store_category_rel WHERE store_id='$id_shop'"); foreach($row AS $row){ $res=build_tree($cats,0,find_parent($cats_ID,$row['bsc_id'])); if(!in_array($res,$result)){ $result[]=$res; } } foreach($result AS $result){ print $result; } 

An example of how it should be: such an array is displayed from categories enter image description here

Using the link table should result in the following tree array enter image description here

  • And what have you done to solve the problem? What exactly did not work out? - Dmitriy Simushev
  • Added to the description. - user3203523
  • Well, and what does not work? What exactly is not working? How does the error occur? How to play it? - Dmitriy Simushev
  • Explain exactly what you see the problem, how to reproduce it, what you want to get as a result, etc. - Nicolas Chabanovsky

0