That should climb down the trees of each person registered. I don't see any problems so can you point any out? I removed the finish() call in the subroutine in case that was killing all instances of the database call in the prepare statement...my $_countLevels = 0; my $_countRank1 = 0; my $_countRank2 = 0; my $_countAllNewlyAdded = 0; my $_countAllNewlyAddedFailures = 0; my $_highLevel = 0; my $_dbi_errors = ""; my $sth = $dbh->prepare(qq{select * from `registered_users` where `ran +ked1` = "1"}); $sth->execute(); while(my $_regu = $sth->fetchrow_hashref()) { $_countRank1++; &buildTradRank($_regu->{MemberId},$_regu->{MemberId},'rank1',0); } $sth->finish(); $sth = $dbh->prepare(qq{select * from `registered_users` where `ranked +2` = "1"}); $sth->execute(); while(my $_regu = $sth->fetchrow_hashref()) { $_countRank2++; &buildTradRank($_regu->{MemberId},$_regu->{MemberId},'rank2',0); } $sth->finish(); sub buildTradRank { ($_rankedid,$_startingid,$_rank,$_level); my $_fieldName = $_rank . '_traditional'; $_level++; $_countLevels++; $_highLevel = $_level if $_level > $_highLevel; # Once we reach someone else ranked, stop there do not go no furth +er since that person will get all their organization coded to them la +ter if($_level == 1) {# since this is their personal referrals make su +re they are all coded to them even if they are ranked... my $_addToSearch = ""; } else {# Now make sure it does not select anyone that is also ran +ked at this level my $_addToSearch = qq~ and `$_rank` = "0"~; } my $sth2 = $dbh->prepare(qq{select * from `registered_users` where + `referrer_id` = ?$_addToSearch}); $sth2->execute($_startingid); while($_nregu = $sth2->fetchrow_hashref()) { if(!$_nregu->{$_fieldName} || ($_nregu->{$_fieldName} && $_nre +gu->{$_fieldName} != $_rankedid)) { my $_updated = $dbh->do(qq{update `registered_users` set ` +$_fieldName` = ? where `MemberId` = ?}, undef, $_rankedid, $_nregu->{ +MemberId}); if($_updated) { $_countAllNewlyAdded++; } else { $_countAllNewlyAddedFailures++; $_dbi_errors .= $DBI::errstr . "\n" if $DBI::errstr; } } if(!$_nregu->{$_rank}) {# This person not ranked so add their +referred club members to this persons array # Has this person referred anyone? my $_countReferred = $dbh->selectrow_array(qq{select count +(*) from `registered_users` where `referrer_id` = ? and `$_rank` = "0 +"}, undef, $_nregu->{MemberId}); if(!$_countReferred) { next; # Had no referrals so just go to the next person +... } # If made it here then this person had referrals so go wor +k on their referrals... &buildTradRank($_rankedid,$_nregu->{MemberId},$_rank,$_lev +el); } next; } return 1; }
In reply to Recursive programming question by ukndoit
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |