The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Basically, a user logs in and then by their member_id I want to track who they have refered.

I read the entire database into an array, but the iterative part is giving me real problems. Can anyone help with this, or point me to an example they may have done before? Happy to clarify if required.

I read the entire database into an array, but the iterative part is giving me real problems. Can anyone help with this, or point me to an example they may have done before? Happy to clarify if required.

That doesn't sound very scalable. A basic algorithm would go something like:

SELECT
fir.name AS firstName, fir.refered_by AS firstReferer,
sec.name AS secName, sec.refered_by AS secReferer,
thi.name AS thirdName, thi.refered_by AS thirdReferer,
fou.name AS FourthName, fou.refered_by AS fourthReferer
FROM members AS fir
INNERJOIN members AS sec
ON sec.refered_by=fir.member_id
INNERJOIN members AS thi
ON thi.refered_by=sec.member_id
INNERJOIN members AS fou
ON fou.refered_by=thi.member_id

Then I would like my first level referals to display
James, Tom and Phil

Second Level referals should display as
John, Bob (who were refered by James)

Third Level referals should display as
Ken (who was refered by Bob)

Fourth Level referals should be displayed as
Steve (refered by Ken)

This is where the iteration should stop, because I am only concerned about the 4 levels immediately beneath the starting point... in this case, Dave. Andy would only feature in the results if we were starting further down the chain.

This is why I initially looked at reading the database into an array... it is expected that the database will contain hundreds of people and I thought it would be more efficient to do all the tree-building from an array rather that a sql statement.