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 `ranked1` = "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 `ranked2` = "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 further since that person will get all their organization coded to them later if($_level == 1) {# since this is their personal referrals make sure 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 ranked 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} && $_nregu->{$_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 work on their referrals... &buildTradRank($_rankedid,$_nregu->{MemberId},$_rank,$_level); } next; } return 1; }