I'm working on a system with affiliate logic behind, with up to 3 levels in the structure.
I've got it working for 2 layers perfectly already. Here is the code I used for 2 layers. I'm using fat free Framework as a PHP Framework, shouldn't matter too much there though (think the code should be understandable without knowledge of the framework as well)...
PHP:
$firstLevel = $db->exec('SELECT * FROM users WHERE sponsorID=:sponsorID', array(':sponsorID' => $_SESSION['userid']));
$affiliateTree = [];
foreach ($firstLevel as $fl) {
$secondLevel = $db->exec('SELECT * FROM users WHERE sponsorID=:sponsorID', array(':sponsorID' => $fl["ID"]));
$affiliateTree[$fl["username"]] = [];
foreach ($secondLevel as $sl) {
$affiliateTree[$fl["username"]][] = $sl["username"];
}
}
$f3->set('affiliateTree', $affiliateTree);
HTML:
<repeat group="{{ @affiliateTree }}" key="{{ @affKey }}" value="{{ @affVal }}">
<li>
<details open>
<summary>{{@affKey}}</summary>
<check if="{{@affVal}}">
<true>
<ul>
<repeat group="{{@affVal}}" key={{@newAffKey}} value="{{@affiliate}}">
<li>{{@affiliate}}</li>
</repeat>
</ul>
</true>
</check>
</details>
</li>
</repeat>
That results in a list how I'd want it, something like that:
-paul
-olaf
-jeanett
-christine
-john
-peter
-tom
-caroline
My database table looks something like that (although would be willing to modify if needed):
ID | SponsorID | username
1 | NULL | master
2 | 1 | paul
3 | 1 | olaf
4 | 3 | jeanettt
5 | 3 | christine
6 | 1 | john
7 | 6 | peter
8 | 6 | tom
9 | 6 | caroline
Now I've tried to modify the code for the third layer, but failed in that.. So basically, if caroline would refer somebody as well, that should be displayed as well (so just an additional layer)...
How can I modify the code (or write new code) to get this done properly? What I've tried was the following:
PHP
foreach ($firstLevel as $fl) {
$secondLevel = $db->exec('SELECT * FROM users WHERE sponsorID=:sponsorID', array(':sponsorID' => $fl["ID"]));
$affiliateTree[$fl["username"]] = [];
foreach ($secondLevel as $sl) {
$thirdLevel = $db->exec('SELECT * FROM users WHERE sponsorID=:sponsorID', array(':sponsorID' => $sl["ID"]));
$affiliateTree[$fl["username"]][] = [];
foreach ($thirdLevel as $tl){
$affiliateTree[$fl["username"]][$sl["username"]][] = $tl["username"];
}
}
}
HTML:
<repeat group="{{ @affiliateTree }}" key="{{ @affKey }}" value="{{ @affVal }}">
<li>
<details open>
<summary>{{@affKey}}</summary>
<check if="{{@affVal}}">
<true>
<ul>
<repeat group="{{@affVal}}" key="{{@newAffKey}}" value="{{@affiliate}}">
<li>
<details open>
<summary>{{@newAffKey}}</summary>
<check if="{{@affiliate}}">
<true>
<ul>
<repeat group="{{@affiliate}}" value="{{@thirdLiner}}">
<li>{{@thirdLiner}}</li>
</repeat>
</ul>
</true>
</check>
</details>
</li>
</repeat>
</ul>
</true>
</check>
</details>
</li>
</repeat>
However, that didn't work at all.. Besides having some list elements with "0" in it, lots of users where missing for some reason...
How can I get this working? Any advise? I'm also down to completely changing code and database, if there's a more efficient way...