Hi,

> I'm having a slight problem trying to figure out some logic. I have a
> mySQL table that contains 3 columns (child, category, parent).
> What I like to do is retrieve all the childrens of the parent and store
> them in an php array.

I assume you want to get all children for all parents (somehow buched
together) and not only to get the children for a single (known) parent.

I have a very vague idea of how php arrays really work but this might get
you started:

(I'm assuming there is a fourth id column here. What do you use for
references otherwise? Btw. what does the child column contain?)

SELECT * FROM the_table a
LEFT JOIN the_table b ON a.id = b.parent

This will effectively get you the children for each parent and the parent
itself (the parent parts will be repetead for each child). You'll have to do
some parsing when outputting/putting into the php array but that should be
of little problem. The left-join 'technique' can be added on and on and will
work as long as you know the maximum depth before the select is done. Eg.

SELECT * FROM the_table a
LEFT JOIN the_table b ON a.id = b.parent
LEFT JOIN the_table c ON b.id = c.parent

If the depth is fixed at design time you could add another column which you
have to keep close track of. That column would be one which if used to ORDER
BY would make certain each child comes after each parent. If the depth is
small you could easely use a float to make that happen.

--
Aigars




---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to