php - Select all items for category and all from its subcategories -


i'm trying build own webshop.
have 2 tables - products , categories. structure :


categories : id       name       parent_id   

products : id       title      category_id   

at moment when user clicks on main category i'm selecting products display :

url : www.mypage.com/?category_id=1 sql : 'select * products category_id = 1'   

the problem i'd make when user clicks on main category select products child categories. example category family sub category of category cars , in db looks

categories : id       name       parent_id   1        'cars'     0 2        'family'   1   3        'sport'    1    products : id       title      category_id   1        'ferrari'  3  2        'honda'    2   

as can see current select not select ferrari or honda because user looking @ category id=1... how modify select display products child categories of main category?

"select * products category_id = '$your_category_id' or category_id in (   select parent_id categories      id = '$your_category_id' )" 

note: example 2 level depth.

e.g.

ferrari > cars 

its not more 2 levels.

e.g.

ferrari child category > ferrari > cars 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -