OK, I think I see where you're going. It appears that you want to compute a score for each member, where the score may depend on the score of other members. You might want to create a variation of a [no such wiki, Topological_sort], as it's fairly easy and lets you compute things in the database for speed. The essence of it is this: You'll make several passes over the table, and on each pass, you'll perform the computation for all members whose referrals are already computed. In pseudocode, it would look something like this:
# mark members with no referrals as done, an everyone else as not done +. # NOTE: I'm assuming a new column named is_done as a work column. If +you prefer # not to have such a column in your database, you can use a temporary +table instead. $DB->do("update registered_users set is_done=0") $DB->do("update registered_users set is_done=1 where MemberID not in ( +select referrer_id from registered_users"); while (1) { # Do we have any members that aren't done yet? my @tmp = $DB->selectrow_array("select count(*) from registered_use +rs where is_done=0"); last unless $tmp[0] > 0; # Perform computation for all members whose referrals are all "done +" $DB->do(qq{ UPDATE registered_users SET is_done=1, <<<your calculation goes here>>> FROM registered_users WHERE is_done=0 AND MemberID not in ( SELECT referrer_id WHERE is_done=0 ) }); }
This way, you'll only need a few passes through the database to compute all your members, and you don't have to worry about recursive algorithms. Please note that this method can fail if your database has an error in it: If you have any loops in your database (e.g., Tom refers Dick who refers Tom or a longer loop) then the members in that loop, and any of their parents won't get computed. This shouldn't happen in your database, but if the method fails, you may want to look for an error in your data similar to this.
...roboticus
In reply to Re^5: Recursive programming question
by roboticus
in thread Recursive programming question
by ukndoit
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |