1

I have a table in db with the fields

-----------------------
Id | Value | Path | Parent
-----------------------

1  | Asia  | 1    | 0 
2  | India | 1/2  | 1
3  | Goa   | 1/2/3| 2

The list has all the cities of the World. So its Quite a big list.

The Issue

If My product has all the parents selected for a leaf. [eg, ID 3, 2, 1 for ID:3] Than I can get generate a tree based on the ParentID using AJAX function.

But

I select a product which gives me the leaf of the tree. [eg, ID:3 ] So I need to parse to generate the tree using Path which sounds too bad an idea [Parsing string will be slow.]

Is there any generic solution to this thing? - Can I create a view and store the corresponding parents r.f leaf.? - Using PEAR Tree?

Or any other better solution involving the DB directly.?

EDIT

Tree structure:

Asia
 >India
   >Goa

EDIT

Recursive functions by parsing the PATH is not suitable for this job. That will be slow.

Umesh Moghariya
  • 3,063
  • 6
  • 21
  • 23
  • Just a suggestion, just store the parent id like for india parent id would be 1 and for Goa parent id should be 2. And impelement this parent id in your ajax :-) – Rohit Choudhary May 07 '12 at 05:50
  • Ok, but can you elaborate on getting the parentID. Currently I have only parentID of leaf, I need parentID uptill the top. – Umesh Moghariya May 07 '12 at 05:53
  • I think there would be root node like in this let Asia, Europe are parellel root node the dont have any parents so their parent ID would ID is 0, Then when you display elements look for their parent id. show all nodes having 0 as root node and click on them load nodes which have corresponding parent node eg when i clikc on asia, load all countries which have asia ie 1 as their parent id. – Rohit Choudhary May 07 '12 at 06:02
  • Yes @RohitKumarChoudhary that is fine and doable. But I have to move Backwards. I have leaf and need to create a tree[Its filtered] so the issue. – Umesh Moghariya May 07 '12 at 06:04
  • Sorry!this is not really clear to me. Can you elaborate it? – Rohit Choudhary May 07 '12 at 06:08
  • @RohitKumarChoudhary I give you an Id[3] and ask you to construct a tree based on `only` the info [3 | Goa | 1/2/3| 2]. I have all i need here to do the same using path[1/2/3] but Its too slow. Is it clear? – Umesh Moghariya May 07 '12 at 06:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10951/discussion-between-rohit-kumar-choudhary-and-cloudyturtle) – Rohit Choudhary May 07 '12 at 06:22
  • @cloudYturtle: Suggest you have a read of [this answer](http://stackoverflow.com/a/192462/623041). – eggyal May 08 '12 at 18:17

1 Answers1

0

PEAR's Tree package has been made for this specific task. Try this proven package instead of inventing your own code.

cweiske
  • 30,033
  • 14
  • 133
  • 194