Need Help Finishing a Script; Function To Determine Rank

Hello sitepoint!

It’s been a while since I’ve been on here - and in the past this was always a great place to come from time to time when I hit a wall; That’s what brings me back here now ---- hit a wall while working on a Script I’m configuring for a Website that functions as an evolving community for a sales organization - There are leadership ranks that are achieved by referrals - customer acquisition goals; and overall team production. I’ve got the first half of the script done, which can accurately read whether or not a team member is ranked as a ‘customer’, or a ‘team member’ - but now I need this script to determine the remaining leadership ranks —

Here’s what I have so Far

//define each level arrarys
$freelevel = array('IboToolBox'=>$row_rankingspage['ibotoolbox_jid'],'Netspend'=>$row_rankingspage['netspend_idnum'],'ImFacePlate'=>$row_rankingspage['imfaceuser'],'BigCrumbs'=>$row_rankingspage['bigcrumbs_username'],);
$bonuslevel = array('Amazon'=>$row_rankingspage['amazonuser'],'ZNZ'=>$row_rankingspage['zipnada'],'AdzLy'=>$row_rankingspage['adzly_idnum'],'Ebates'=>$row_rankingspage['ebatesidnum'],'FourCorners'=>$row_rankingspage['bidernet_tng']);
$tenlevel = array('GDI'=>$row_rankingspage['crazygood_username'],'ILA'=>$row_rankingspage['gvo_username']);
$fiftylevel = array('Stiforp'=>$row_rankingspage['stiforp_username'],'MCA'=>$row_rankingspage['mca_username'],'LevelOneNetwork'=>$row_rankingspage['levelone_tng'],'DSDom'=>$row_rankingspage['solavei_name']);
$hunnidlevel = array('WakeUpNow'=>$row_rankingspage['tng_wun']);

// check free level array to see if it's empty or full
$checkfreelevel = 'unlocked';
if (empty($freelevel['IboToolBox']) or empty($freelevel['Netspend']) or empty($freelevel['ImFacePlate']) or empty($freelevel['BigCrumbs'])) {
$checkfreelevel = 'locked';
} 
// check bonus level array 
$checkbonuslevel = 'unlocked';
if (empty($bonuslevel['Amazon']) && empty($bonuslevel['ZNZ']) && empty($bonuslevel['AdzLy']) && empty($bonuslevel['Ebates'])) {
$checkbonuslevel = 'locked';
}
//check $10 level arrary to see if it's empty or if at least one program is unlocked
$checktenlevel = "unlocked";
if (empty($tenlevel['GDI']) && empty($tenlevel['ILA'])) {
$checktenlevel = "locked";
}
// check $50 level array
$checkfiftylevel = 'unlocked';
if (empty($fiftylevel['MCA']) && empty($fiftylevel['Stiforp']) && empty($fiftylevel['LevelOneNetwork']) && empty($fiftylevel['DSDom'])) {
$checkfiftylevel = 'locked';
}
// check $100 level array
$checkhunnidlevel = 'unlocked';
if (empty($hunnidlevel['WakeUpNow'])) { 
$checkhunnidlevel = 'locked';
}
// determine the Rank!
$ranktng = "lead";
if ($checkfreelevel == "unlocked") { 
$ranktng = "customer";
}
if ($ranktng == "customer" && $checktenlevel == "unlocked") {
$ranktng = "team member";
}
//end of rank

//determine the Number of Referrals

The Remaining Ranks are based on production - So I was thinking that First, I would need a Function that could Determine the Number of Referrals; and then secondly, Determine How many are ranked Customers (display $numberofcustomers (for example) - ) — and also How many of those referrals are Ranked as Team Members

— So that was the first wall I was hitting; Tried several things and none of them seemed to get me the result I was looking for; if anyone can help with that aspect of the script; that would be awesome — and very beneficial to the final part of the script.

I figure If I can manage to get a Function that look through the database, which stores all of this information; determine how many peeople were referred by a User; and how many of those referrals are customers, and how many are team members; that it would make the final steps pretty simple (in theory of course — I’m having difficulty with it so; perhaps not so simple?) ----

If a user has 5 Referrals who are Team Members; then their $ranktng needs to be set at The next rank, Team Leader
If a user has 3 Referrals who are Team Leaders; then their $ranktng needs to be set at The Next Rank; Team director

— and I’m assuming that if it’s possible to get this script to update the $ranktng - To the Next Rank ---- that I can pretty much write in the Remaining Ranks; as they’d all pretty much look similar to these last two. I hope this makes sense; and I hope it’s possible to pull this off. I’ve read a bunch of theories on ‘what I think’ are ways to pull this off; Possibly an Inner Join — something that might store the $rankTNG to make it easier to read and calculate; but again — that’s why I’m here; Can’t seem to figure out exactly how to pull this off.

Is there anyone willing to help out with this?

who knows; someone else may be attempting to do something similar and this thread can serve as a Open-source discussion; to help them do this too; thanks in advance to more skilled php-coders than I; hoping someone or a few someones are willing to help me tackle this.

:smile:

First of all… holy mixed up arrays batman.

Do you use any of these arrays other places? If not, clean them up.
You’d actually (believe it or not) do better with the folllowing logic:

Stuff everything into a single multidimensional array.
Foreach the overallarray (as $key => $array);
Each element: $unlocks[$key] = count($array) == count(array_filter($array));
Endforeach
$rank = ($unlocks[10] && $unlocks[free]) ? “team member” : (($unlocks[free]) ? “customer” : “lead”);

As far as referral based ranking… how (or rather, when) are you evaluating this? Where’s the relation made to determine who referred who?

1 Like

Everything is stored in a Database; A Mysql Database.

I actually left the opening part of the code out; where I’m connecting to the database; and pulling information out based on the Username of the Person — which is where the whole $row_rankingspage variable came from —

Sorry for all the arrays; Honestly I was trying to think of a way that would be flexible enough; in case programs are removed from this system in the future and replaced with something else. I’ll be honest; the array you listed looks a WHOLE lot simpler; but I don’t really fully understand all of it; so I’ll be working on that tonight (thanks for your input)

That being said; the Values Stored in the Database, include the Username of the person; (who’s rank we want to determine with the $ranktng variable) — their name, phone number, what programs they’re participating in; and also the Username of the person Who referred them; ‘referredbyusername’ (along with their email, phone etc…)

so, this is what I was ‘attempting to do’ to figure out the Number of Referrals

//number of referrals starts
$numberofreferrals = "SELECT COUNT(*) FROM TableName WHERE referbyusername = '".$_GET['tngusername']."'";
$totalreferrals = mysql_query($numberofreferrals, $systemonedb) or die (mysql_error());
$num_referrals = mysql_fetch_assoc($totalreferrals);

Let me know if this makes any more sense —

Also with the shorter array you listed; thanks again for that; gonna test a few things out and try to grasp what that code is doing; I like to understand what the code is doing; as it helps me with future scripts I will attempt!

Does this really need to be generated dynamically? My impulse is to say this should be handled on a daily basis and the result be stored in the database…

1 Like

hmmm; So, I should add some more tables to the database that’ll store the team ranks; and focus instead on a script that updates those ranks? I hadn’t considered that.

This really has my wheels turning; i think you are right; A script that runs daily, checks each members in the database, and then stores a ‘result’ in the same database probably would make this a whole lot easier; I see how if the results are stored in the database (lead, customer, team member, etc…) — then I suppose the script would just look for how many referred users, are at a ‘certain rank’ – to determine the daily rank of the person — If you’d like to make any suggestions on where I can get started to look into everything I need to make this work; feel free – You’ve been a big help so far already!

In thinking about it, i’d do it on a as-they-come basis because of the scale-up-the-tree style of referals, but i’m not sure where you’re getting info for $row_rankingspage. Is that external data or internal?

There’s actually a Total of 9 Ranks; which I didn’t really get into detail on yet; it’s easier to visualize So if It’s ok to include some images of what each Rank “looks like” – it’ll make a lot more sense in terms of what I’m trying to do; I’ll try to explain it first though; breaking down just a few of the 9 additional ranks;

The 3 ranks I’ve named so far are really just the starting positions; which are based on Participation ::: measurements that do need to be tracked and calculated to determine the actual “Earned Ranks” which are based on Production; — for example; The first ‘earned’ rank is Team Leader; this is someone who has referred 5 Team Members; or 5 people who are participating at the $10 level. Let’s say one of their Customers Refers someone who becomes a Team Member; well that would Count towards the ‘qualification’ for Team Leader. (Will post a diagram below)

So I’m trying to track who is a Lead, a Customer, or a Team Member - for the purpose of determining what Rank Each Person is; based on how many of their total referrals — or even their referrals achieve a Rank; and total team production.

The next rank after Team Leader is Team Director; they have referred 3 people who have earned the rank of Team Leader; (or even if they refer someone Else who becomes a Team Leader – in their referral tree (parent/childs I’m thinking) ---- > and again I’ll post a diagram for this.

the Next rank after that is Managing Director - has referred 3 people who have earned the rank of Team Director — each rank is based on the one below it; After (MD) is Executive Director; They have 3 people who are ranked at MD; or have helped someone rank up to MD; Ok; let me show those examples.

The first 'earned rank" - Team Leader (the next two diagrams should help this make sense - if not clear after)

So that diagram doesn’t really show how the ranks ‘should’ work; but the next Two are much Clearer and bring home what I really want this script to do; Here is Team Director - You’ll notice the difference - that the TL in one of the 3 required - is not direct; the same would be true for the above; with TM’s — for example. They could have 4 direct TM’s and a Customer that refers a TM; same as what you see below –

Team director rank illustrated -

Managing Director Rank

The qualification is at 3 for this one - and it’s the same going up the Ranks; Till ranks 8 and 9; When I really thought about all of this; the table does make a lot more sense; I can see how a script working to do this everytime a page loads would really require a lot of work on the servers; That being said; The $row_rankingspage - is being Pulled from the Current Database; which Stores all the information for each member.

It has their username, email, phone number; the username of the person that referred them; their email and phone number; and the database also stores which programs they are participating in; That is why I created all those “holy arrays batman” :smile:

I wanted to create a few simple variables that I could Call in my Pages; For example — The part of the script I posted initially; Does work; as far as determining who is a Lead, a Customer or a Team Member; that’s just as far as I got;

In theory - what I was trying to do next; before coming here — was create another Variable; that ran a query on the DB; to look for a ‘string’ - being the username - to see how many times it showed up In the ‘referredbyusername’ column, to call the total — I figured if I could do that; I could build on it; and use that to determine how many of those referrals were – leads, customers, tm’s ----

So all of that is still the goal; but with the table storing the info that seems like a more stable solution; Seems like it would make the job easier for the script to look through the database to see who’s at what rank; based on what’s stored in the database; and basically Run Perioidically throughout the day; simply Reading the ranks and Updating the Database ‘CurrentRank’ column for example; That’s what I wrote down at least; Might be able to take some of what is already done; but modify it to check and update a database ---- and go from there; also was thinking I’d need to add those ‘currentRank’ and any other related columns to the Current database first;

So i absolutely say that a table is going to be required for this. You do NOT want to have to generate this dynamically, for one very simple reason:

Evaluating a person’s rank dynamically requires a complete loop through the entire data set for EACH rank.

Let’s exclude the bottom two for the moment. In order to determine if someone is a Team Leader, you first have to know everybody’s rank below him, to a minimum of Team Member (NOTE: You CANNOT at this point know who else is a Team Leader). That means you first have to evaluate everyone below him’s rank; then count.

Now to see if he’s a team Director, you have to know how many people below him are Team Leaders. That requires evaluating all Team Members for their eligibility as a Team Leader, and then counting them…

Rinse, repeat, for every rank you add on top, and you can see that’s a massive processing load for something that could be as simple as “what rank does he have in the table”.

So, let’s assume you want to store the information in a table.
The trick to the evaluation process is a question of when things change. (Because you only really need to evaluate a person’s rank when something at-or-below them changes.)

This is why i asked about the first three ranks. If that data is being pulled on a scheduled basis, you’ll have to add the logic of change to the scheduled script call. If it’s being sent to you per-person-per-change, you can run a reactionary script.

For example.

Here’s my referral chain:

PersonX referred PersonY
PersonY referred 5 people (RSTQP).

RST have all reached Team Member level.

Event: Q reaches Team Member level.
Script: Update Member Q. Now check Q’s referrer (Y), to see if they too need an update.
Y doesnt need an update. But to be complete, check Y’s referrer to see if they need an update.
rinse/repeat this step all the way up the chain until you reach the top OR someone who is already the top rank. (By definition, someone who referred someone of the top rank will have earned the top rank also themselves, because their tree will contain everyone in that person’s tree.)

Event: P reaches Team Member level.
Script. Update Member P. Now check P’s referrer (Y).
Y needs an update. Check Y’s referrer
etc…

Because the change only ever flows upwards (Y becoming a TL has no effect on anyone below Y), this will work. Also note that the ONLY event that triggers an update check is someone becoming a Team Member.

Also, your diagram is impossible. A TM can never have a TD below them. A person can never* be above someone with a rank higher than them in your system. Its mathematically impossible.

[* and by never i mean excluding the two bottom participation ranks, which can be shifty]

sorry been a little busy here; but am super thankful for all your feedback up to this point; I’m actually talking with a few programmers as well; and am using this thread to show them what we’ve talked about so far; which has been helpful;

I did want to explain one thing about the TM/TD scenario you mentioned. A team member just has to be participating on our $10 level; and that rank is not based on any personal referrals or production from their end; Where as a T.D. is someone who’s referred 5 people, 3 of which are ranked as TL’s - and those TL’s - have referred 5 people who are TM’s

So, it actually is possible for a TM to refer someone, who becomes a TM; refers 5 TM’s and becomes a TL; and that person could continue to grow in ranks; achieving TD, or higher; Even if the person that referred this TD - remains as a TM ---- and does not refer anyone else — This is a bit complicated to explain and so I do apologize if that was not clear before. I know it’s possible because there are hundreds (or more) websites now that have algorithms set to determine ranks; I’ve just never built a script like that, myself.

The ranking system I’ve drawn out, fits with several others that are already out there.

So It’s possible; Just gotta figure out how to pull it off; that being said you’ve been extremely helpful as far as contributing ideas as to how to pull it off; It will need a table that tracks it; and stores ranks; b/c you’re right - that’s too much work for the script to do (on page load) ---- anyways; just wanted to acknowledge your help with this; and let you know I’m still around and still working on making this happen.

Am also going to see what a few others think of what we’ve discussed here so far.

Thanks again @StarLion

It’s impossible because of one thing: You evaluate all people under the target for rank evaluation.

Take your example.


Now; You say top guy is an MD because there are 3 TD’s ANYWHERE in the tree below him.

That’s fine. BUT:
If you go down to the node of the TM on the far right, and now make that your root:
There is a TD directly below him.
The ONLY way a TD can be directly below him means that below that TD, there are at least 3 people below him that have the tank of TL.
That also means there are at LEAST 5 people below those TL’s that are TM.
But… that also means there are at LEAST 5 people below our original TM that are TM’s. So he now qualifies as a TL.
But we’ve already stated that in order for that TD to be there, there must be at LEAST 3 people below him who are TL’s. So that means our original TM also has 3 TL’s below him. So now he qualifies as a TD.
You can never be a lower rank than someone you’ve recruited is, because that person’s entire tree is also in your tree. (again, excluding the case that you arent yet a TM)

There is no possible way you can draw that diagram in such a way that the TD below the TM qualifies as a TD, and the TM doesnt also.

If you can, i’ll give you 100$, and every set-theory mathematician in the world will bow down to you, because you’ve just invalidated their entire field of study. :stuck_out_tongue:

EDIT: Also the world would explode because you’d have proved a paradox: SetTD is a Subset of SetTM. SetTD contains an element that is not in SetTM.
∀x (x ∈ A → x ∈ B)
∃x (x ∈ A, x ∉ B)

1 Like

lol, I may have to hold you to that $100 challenge!! haha – but yes; your points are all duly noted; and again it’s all been helpful because I had already started to toss a few ideas back and forth with a programmer before bringing it here; and much of what you said matches up with what they spoke about as well; it’s definitely complicated - from what I can tell talking to programmers who are better at it than I am; however I promise it’s possible; I (we) just have to figure out how it’s done;

For example; the TD; below the TM; has referred 5 TM’s personally; Where as that TM above the TD; only referred 2 people; One of them became a TD; However, it seems like you’re thinking of it as the TD’s production counts for the TM’s production; that’s where the complication may be coming up; and that’s where I need the script to be able to read who’s done what; and then promote (the rank) based on what each has done.

Now; if a TM refers 3 people; who become TD’s - it’s going to promote the TM to a TL … and if those 3 TD’s become TL’s - it will push the (original TM, now TL, to MD; (yes, lol I know that sounds complicated; I should probably draw another diagram; give me a few (days) lol – got a lot i’m still working on; hopefully I can lay something out by tomorrow that will help this make more sense; but again; thanks for all your input so far; superb stuff.

Please be aware that the above is wide open to SQL injection attack as you’re letting data from the $GET array near the database without sanitizing or escaping it. Also you need to be aware that the mysql* extension is now deprecated as of version 5.5 of PHP and will likely be removed from the next version of PHP. You should by migrating over to using either the mysqli_* extension or PDO. Whether you choose to use PDO or the mysqli_* extension you should be using prepared statements to eliminate the risk of SQL injection attack. PDO has the advantage of allowing you to use named parameters with prepared statements.

What’s the current structure of the database tables concerned?

1 Like

yes; was reading up on that too; and am most likely gonna have to overhaul a lot of code there; to move over to the mysqli ---- I figured I’d turn my attention to that once I figure out this part first. Also from the way this has gone; it sounds like I’m going to need all new tables;

If you want to limit it to DIRECT referrals, then it certainly does change, but it also invalidates your previous diagram - the MD doesnt have 3 TD’s directly connected to him anymore.

So yep, figure out the system, and THEN try and code it :wink: We’ll be here.

1 Like

Once again, want to thank all who have shared their input so far; and who are interested in helping me figure out how to code this. What I realized from a few responses, is that the kinda script I’m talking about - has been done before. I just cannot figure out how to do it for myself. That being said, it does require some understanding of “how” the structure looks -

What we’re talking about here, is an MLM script really - a large % of MLM’s use dynamic scripts to determine the ‘leadership ranks’ based on the production - usually determined by the amount of sales volume each person is producing. However the rank is determined by where that production is coming from. I realized after the fact that my previous diagrams are not really that ‘clear’ for someone who doesn’t have an MLM background; and may never have seen this in action before.So… Finally I’ve finished up a diagram which I hope makes things clearer.


This diagram includes the ‘full rank chart’ that the team sees; along with what I hope is a better illustration of how it’s possible for a person ‘below’ someone to have a higher ‘rank’ - I think what may have come across in my initial explanation is that if a Person refers another Person; Who goes out and refers 5 people - That BOTH get promoted; when only ONE does; The person who referred 5; The person who Referred that person; Would also need to refer 5 people (separate people) - to qualify for a promotion.

This means that a person “under you” can out rank; and out produce that person; because it’s not the ‘group total’ we need to track; It’s more of a “Parent/Child” tracking; which I believe is possible through PHP; maybe what I have wrong is that I need another scripting language to make it all work; JS perhaps? Not sure; but that again, is why I am here; I know this can be done; as it’s been done for several years now; I just don’t know how to pull it off.

Thanks again to all who are trying to assist here; the effort is very much appreciated; I hope this example makes more sense - as to what I’m seeking to do with this unfinished script

Your chart is still semi-ambiguous as to the definition of a Leg. For example;

You have demonstrated that a person can have a higher rank and be below you.

Person A recruits 5 people, B C D E and F. Everyone except F is a TM. So A is still a TM.
F has a recruit; G. G’s become a TM now.

Is A a TM or a TL? (believe it or not, this statement determines the difficulty of your code evaluation, and is the final logic piece i believe needed to come up with an algorithm :P)

I am in no way saying this the answer but I thought I would play with some data.

<?php

//sample DB result fields `id`, `rid` (referring id), `name`
$people = array(
    0 => array('id' => '1','rid' => '0', 'name' => 'one'),
    1 => array('id' => '2','rid' => '1', 'name' => 'two'),
    2 => array('id' => '3','rid' => '1', 'name' => 'three'),
    3 => array('id' => '4','rid' => '1', 'name' => 'four'),
    4 => array('id' => '5','rid' => '2', 'name' => 'five'),
    5 => array('id' => '6','rid' => '2', 'name' => 'six'),
    6 => array('id' => '7','rid' => '2', 'name' => 'seven'),
    7 => array('id' => '8','rid' => '2', 'name' => 'eight'),
    8 => array('id' => '9','rid' => '2', 'name' => 'nine'),
    9 => array('id' => '10','rid' => '9', 'name' => 'ten'),
    10 => array('id' => '11','rid' => '9', 'name' => 'eleven'),
    11 => array('id' => '12','rid' => '9', 'name' => 'twelve'),
    12 => array('id' => '13','rid' => '11', 'name' => 'thirteen'),
    13 => array('id' => '14','rid' => '11', 'name' => 'fourteen'),
    14 => array('id' => '15','rid' => '11', 'name' => 'fifteen'),
    15 => array('id' => '16','rid' => '13', 'name' => 'sixteen'),
    16 => array('id' => '17','rid' => '13', 'name' => 'seventeen'),
    17 => array('id' => '18','rid' => '13', 'name' => 'eightteen'),
    18 => array('id' => '19','rid' => '13', 'name' => 'nineteen'),
    19 => array('id' => '20','rid' => '13', 'name' => 'twenty'),
    20 => array('id' => '21','rid' => '17', 'name' => 'twentyone'),
    21 => array('id' => '22','rid' => '17', 'name' => 'twentytwo'),
    22 => array('id' => '23','rid' => '17', 'name' => 'twentythree'),
    23 => array('id' => '24','rid' => '17', 'name' => 'twentyfour'),
    24 => array('id' => '25','rid' => '17', 'name' => 'twentyfive'),
    25 => array('id' => '26','rid' => '8', 'name' => 'twentysix'),
    26 => array('id' => '27','rid' => '8', 'name' => 'twentyseven'),
    27 => array('id' => '28','rid' => '8', 'name' => 'twentyeight'),
    28 => array('id' => '29','rid' => '8', 'name' => 'twentynine'),
    29 => array('id' => '30','rid' => '8', 'name' => 'thirthy'),
    30 => array('id' => '31','rid' => '7', 'name' => 'thirthyone'),
    31 => array('id' => '32','rid' => '7', 'name' => 'thirthytwo'),
    32 => array('id' => '33','rid' => '7', 'name' => 'thirthythree'),
    33 => array('id' => '34','rid' => '7', 'name' => 'thirthyfour'),
    34 => array('id' => '35','rid' => '7', 'name' => 'thirthyfive'),
    35 => array('id' => '36','rid' => '12', 'name' => 'thirthysix'),
    36 => array('id' => '37','rid' => '12', 'name' => 'thirthyseven'),
    37 => array('id' => '38','rid' => '12', 'name' => 'thirthyeight'),
    38 => array('id' => '39','rid' => '12', 'name' => 'thirthynine'),
    39 => array('id' => '40','rid' => '12', 'name' => 'fourty'),
    40 => array('id' => '41','rid' => '12', 'name' => 'fourtyone'),
    41 => array('id' => '42','rid' => '14', 'name' => 'fourtytwo'),
    42 => array('id' => '43','rid' => '14', 'name' => 'fourtythree'),
    43 => array('id' => '44','rid' => '14', 'name' => 'fourtyfour'),
    44 => array('id' => '45','rid' => '37', 'name' => 'fourtyfive'),
    45 => array('id' => '46','rid' => '37', 'name' => 'fourtysix'),
    46 => array('id' => '47','rid' => '37', 'name' => 'fourtyseven'),
    47 => array('id' => '48','rid' => '37', 'name' => 'fourtyeight'),
    48 => array('id' => '49','rid' => '37', 'name' => 'fourtynine')
);

//echo "<pre>";
//print_r($people); 
//echo "</pre>";
 
function aarsort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

// Sort people array (DB result) by referring id
// Note: this could be done in query
aarsort($people,'rid');

$referrals = array(); 
$referral_data = array();

// Group by referring ID to new array referrals
foreach($people as $arr):
    $referrals[$arr['rid']][$arr['id']] = $arr;
endforeach;    

//echo "<pre>";
//print_r($people); 
//print_r($referrals);
//echo "</pre>"; 

// Loop through referral array to get counts.  Set counts and status to referral_data array.
// Note: Only referring members have counts or status set.
foreach($referrals as $rid => $arr):
    //referral count and team leader status - TL_count goal: 5
    $referral_data[$rid]['TL_count'] = count($arr);
    $referral_data[$rid]['TL_status'] = (count($arr) >= 5 ? "true" : "false");
    //team director count and status
    if(!isset($referral_data[$rid]['TD_count'])){$referral_data[$rid]['TD_count'] = 0;}    
    $referral_data[$rid]['TD_count'] += ($referral_data[$rid]['TL_status'] === "true" ? 1 : 0);
    $referral_data[$rid]['TD_status'] = ($referral_data[$rid]['TD_count'] >= 3 ? "true" : "false");
    //managing director count and status
    if(!isset($referral_data[$rid]['MD_count'])){$referral_data[$rid]['MD_count'] = 0;}    
    $referral_data[$rid]['MD_count'] += ($referral_data[$rid]['TD_status'] === "true" ? 1 : 0);
    $referral_data[$rid]['MD_status'] = ($referral_data[$rid]['MD_count'] >= 3 ? "true" : "false");
    //executive director count and status
    if(!isset($referral_data[$rid]['ED_count'])){$referral_data[$rid]['ED_count'] = 0;}    
    $referral_data[$rid]['ED_count'] += ($referral_data[$rid]['MD_status'] === "true" ? 1 : 0);
    $referral_data[$rid]['ED_status'] = ($referral_data[$rid]['ED_count'] >= 3 ? "true" : "false");
    //regional director count and status
    if(!isset($referral_data[$rid]['RD_count'])){$referral_data[$rid]['RD_count'] = 0;}    
    $referral_data[$rid]['RD_count'] += ($referral_data[$rid]['ED_status'] === "true" ? 1 : 0);
    $referral_data[$rid]['RD_status'] = ($referral_data[$rid]['RD_count'] >= 3 ? "true" : "false");
    //regional vise president count and status
    if(!isset($referral_data[$rid]['RVP_count'])){$referral_data[$rid]['RVP_count'] = 0;}    
    $referral_data[$rid]['RVP_count'] += ($referral_data[$rid]['RD_status'] === "true" ? 1 : 0);
    $referral_data[$rid]['RVP_status'] = ($referral_data[$rid]['RVP_count'] >= 3 ? "true" : "false");
    //senior vise president count and status
    if(!isset($referral_data[$rid]['SVP_count'])){$referral_data[$rid]['SVP_count'] = 0;}    
    $referral_data[$rid]['SVP_count'] += ($referral_data[$rid]['RVP_status'] === "true" ? 1 : 0);
    $referral_data[$rid]['SVP_status'] = ($referral_data[$rid]['SVP_count'] >= 3 ? "true" : "false");
    //national senior vise president count and status
    if(!isset($referral_data[$rid]['NSVP_count'])){$referral_data[$rid]['NSVP_count'] = 0;}    
    $referral_data[$rid]['NSVP_count'] += ($referral_data[$rid]['SVP_status'] === "true" ? 1 : 0);
    $referral_data[$rid]['NSVP_status'] = ($referral_data[$rid]['NSVP_count'] >= 5 ? "true" : "false");
    //world wide vise president count and status
    if(!isset($referral_data[$rid]['WWVP_count'])){$referral_data[$rid]['WWVP_count'] = 0;}    
    $referral_data[$rid]['WWVP_count'] += ($referral_data[$rid]['NSVP_status'] === "true" ? 1 : 0);
    $referral_data[$rid]['WWVP_status'] = ($referral_data[$rid]['WWVP_count'] >= 5 ? "true" : "false");
    //ranks by count    
    $referral_data[$rid]['Rank'] = ($referral_data[$rid]['TL_count'] >= 5 ? "Team Leader" : "Team Member");                                             
    $referral_data[$rid]['Rank'] = ($referral_data[$rid]['TD_count'] >= 3 ? "Team Director" : $referral_data[$rid]['Rank']);                     
    $referral_data[$rid]['Rank'] = ($referral_data[$rid]['MD_count'] >= 3 ? "Managing Director" : $referral_data[$rid]['Rank']);
    $referral_data[$rid]['Rank'] = ($referral_data[$rid]['ED_count'] >= 3 ? "Executive Director" : $referral_data[$rid]['Rank']);
    $referral_data[$rid]['Rank'] = ($referral_data[$rid]['RD_count'] >= 3 ? "Regional Director" : $referral_data[$rid]['Rank']);                  
    $referral_data[$rid]['Rank'] = ($referral_data[$rid]['RVP_count'] >= 3 ? "Regional Vise President" : $referral_data[$rid]['Rank']);
    $referral_data[$rid]['Rank'] = ($referral_data[$rid]['SVP_count'] >= 3 ? "Senior Vise President" : $referral_data[$rid]['Rank']);
    $referral_data[$rid]['Rank'] = ($referral_data[$rid]['NSVP_count'] >= 5 ? "National Senior Vise President" : $referral_data[$rid]['Rank']);
    $referral_data[$rid]['Rank'] = ($referral_data[$rid]['WWVP_count'] >= 5 ? "World Wide Vise President" : $referral_data[$rid]['Rank']);
    //referral data
    $referral_data[$rid]['referrals'] =    $arr;                    

endforeach;

//echo "<pre>";
//print_r($referral_data); 
//echo "</pre>";    

// sample display
aarsort($people,'id');

foreach($people as $arr):
    //if found in referral data array, they are active, otherwise in "start position"
    $rank = (array_key_exists($arr['id'],$referral_data) ? $referral_data[$arr['id']]['Rank'] : "Start Position");
    $personal_referrals = (array_key_exists($arr['id'],$referral_data) ? $referral_data[$arr['id']]['TL_count'] : 0);
    echo "Name: " . $arr['name'] . "  ID: ". $arr['id'] ."<br>\r  
     Personal Referrals: ". $personal_referrals ."<br>\r
     Rank: ". $rank ."<br><br>\r
    ";
endforeach;
?>
1 Like

It’s a good idea, but relies upon immediate referral.

For non-immediate referral…

You’re going to have to tree the data from the table, as Drummin has done, but then you’ll need to recursively define a function to crawl down the tree.

Something like…

    function treedrill(startnode) {
      $toprank = startnode.rank;
      foreach(startnode.children AS child) {
         $toprank = max($toprank,treedrill(child));
      }
      return $toprank
    }

while($X != 0) {
foreach(X.children AS child) {
  $legs[treedrill(child)]++;
}

Note that we dont care what the makeup of the tree is; we only care what the top rank of the leg is, since multiple people in the same leg dont count, and someone of a higher rank counts as all lower ranks.

Then, do a comparison:
$rank = 1; //Assumed shortcutting somewhere if TM conditions not met
for(i = 1; i < MaxRank#; i++) {
if(array_sum(array_slice($legs,TargetRank)) > TargetNumber) { $rank = TargetRank; } else { break; }
}
X = x.parent;
}
or something of the like.

(Note: If you want to try getting fancy and storing the result of person X for person X’s parent to avoid re-counting, you could. Slice the key of the highest value of $legs, and feed it back as $legs for the next loop, with a way to tell your function to ignore a child if child = X.)

1 Like

In my first attempt I built a tree like array shown below with each individual leg but to be honest couldn’t figure out how to crawl down the leg to get any valuable data from it.

Array
(
    [0] => Array
        (
            [PR_count] => 1
            [TL_status] => false
            [TD_status] => false
            [MD_status] => false
            [ED_status] => false
            [RD_status] => false
            [RVP_status] => false
            [SVP_status] => false
            [NSVP_status] => false
            [WWVP_status] => false
            [Rank] => Team Member
            [referrals] => Array
                (
                    [1] => Array
                        (
                            [id] => 1
                            [name] => one
                            [referrals] => Array
                                (
                                    [2] => Array
                                        (
                                            [PR_count] => 5
                                            [TL_status] => true
                                            [TD_status] => false
                                            [MD_status] => false
                                            [ED_status] => false
                                            [RD_status] => false
                                            [RVP_status] => false
                                            [SVP_status] => false
                                            [NSVP_status] => false
                                            [WWVP_status] => false
                                            [Rank] => Team Leader
                                            [referrals] => Array
                                                (
                                                    [9] => Array
                                                        (
                                                            [id] => 9
                                                            [name] => nine
                                                        )

                                                    [8] => Array
                                                        (
                                                            [id] => 8
                                                            [name] => eight
                                                        )

                                                    [6] => Array
                                                        (
                                                            [id] => 6
                                                            [name] => six
                                                        )

                                                    [5] => Array
                                                        (
                                                            [id] => 5
                                                            [name] => five
                                                        )

                                                    [7] => Array
                                                        (
                                                            [id] => 7
                                                            [name] => seven
                                                            [referrals] => Array
                                                                (
                                                                    [8] => Array
                                                                        (
                                                                            [PR_count] => 5
                                                                            [TL_status] => true
                                                                            [TD_status] => false
                                                                            [MD_status] => false
                                                                            [ED_status] => false
                                                                            [RD_status] => false
                                                                            [RVP_status] => false
                                                                            [SVP_status] => false
                                                                            [NSVP_status] => false
                                                                            [WWVP_status] => false
                                                                            [Rank] => Team Leader
                                                                            [referrals] => Array
                                                                                (
                                                                                    [26] => Array
                                                                                        (
                                                                                            [id] => 26
                                                                                            [name] => twentysix
                                                                                        )

                                                                                    [27] => Array
                                                                                        (
                                                                                            [id] => 27
                                                                                            [name] => twentyseven
                                                                                        )

                                                                                    [28] => Array
                                                                                        (
                                                                                            [id] => 28
                                                                                            [name] => twentyeight
                                                                                        )

                                                                                    [29] => Array
                                                                                        (
                                                                                            [id] => 29
                                                                                            [name] => twentynine
                                                                                        )

                                                                                    [30] => Array
                                                                                        (
                                                                                            [id] => 30
                                                                                            [name] => thirthy
                                                                                        )

                                                                                    [9] => Array
                                                                                        (
                                                                                            [PR_count] => 3
                                                                                            [TL_status] => false
                                                                                            [TD_status] => false
                                                                                            [MD_status] => false
                                                                                            [ED_status] => false
                                                                                            [RD_status] => false
                                                                                            [RVP_status] => false
                                                                                            [SVP_status] => false
                                                                                            [NSVP_status] => false
                                                                                            [WWVP_status] => false
                                                                                            [Rank] => Team Member
                                                                                            [referrals] => Array
                                                                                                (
                                                                                                    [11] => Array
                                                                                                        (
                                                                                                            [id] => 11
                                                                                                            [name] => eleven
                                                                                                            [referrals] => Array
                                                                                                                (
                                                                                                                    [12] => Array
                                                                                                                        (
                                                                                                                            [PR_count] => 5
                                                                                                                            [TL_status] => true
                                                                                                                            [TD_status] => false
                                                                                                                            [MD_status] => false
                                                                                                                            [ED_status] => false
                                                                                                                            [RD_status] => false
                                                                                                                            [RVP_status] => false
                                                                                                                            [SVP_status] => false
                                                                                                                            [NSVP_status] => false
                                                                                                                            [WWVP_status] => false
                                                                                                                            [Rank] => Team Leader
                                                                                                                            [referrals] => Array
                                                                                                                                (
                                                                                                                                    [38] => Array
                                                                                                                                        (
                                                                                                                                            [id] => 38
                                                                                                                                            [name] => thirthyeight
                                                                                                                                        )

NOTE: had to remove half the array because of width limit.

1 Like