php mysql get all parents -


i'm trying parents of children. have simple query select sections sectionid = 6 database structure simple

sectionid parent 1     0 2     1 3     1 4     2 5     4 6     5 

now i'm trying parents sectionid 6, result should string 0/2/4/5/6. children might not have lot of parents, might have 1 result should different. i.e 5/6.. hope understand i'm trying do. have tried couple recursive functions i've found on internet, suck @ , wondering if me on right track. p.s i'm using php , mysql

well, far can see have 2 choices, both known.

1) make recursive function, ones have been trying. there tons of them around , won't put him in here.

2) far favourite, database pattern modern orm use, it's called nested set model.

basically create few more columns on table, should one:

create table nested_category (         category_id int auto_increment primary key,         name varchar(20) not null,         lft int not null,         rgt int not null );  insert nested_category values(1,'electronics',1,20),(2,'televisions',2,9),(3,'tube',3,4),  (4,'lcd',5,6),(5,'plasma',7,8),(6,'portable electronics',10,19),(7,'mp3 players',11,14),(8,'flash',12,13),  (9,'cd players',15,16),(10,'2 way radios',17,18);  select * nested_category order category_id;  +-------------+----------------------+-----+-----+ | category_id | name                 | lft | rgt | +-------------+----------------------+-----+-----+ |           1 | electronics          |   1 |  20 | |           2 | televisions          |   2 |   9 | |           3 | tube                 |   3 |   4 | |           4 | lcd                  |   5 |   6 | |           5 | plasma               |   7 |   8 | |           6 | portable electronics |  10 |  19 | |           7 | mp3 players          |  11 |  14 | |           8 | flash                |  12 |  13 | |           9 | cd players           |  15 |  16 | |          10 | 2 way radios         |  17 |  18 | +-------------+----------------------+-----+-----+ 

if notice there no parent_id column. able search it's childrens for, let's row 5 query like:

select * nested_category left > 7 , left < 8 order left asc, bring no results.

for row number 1 result bring entire tree.

i have no php script autocreating columns on computer, there plenty around. im afraid recursive.

you find lots of info around searching "nested set model", this or theorical exaplanations of model like one

and way duplicated question (i can not put duplicated)

some other answers:

you should reread forum rules before posting, asked questions.

hope helps.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -