I have a three-tier MLM tree with four levels: Basic, Gold, Silver, and Bronze. Gold, Silver, and Bronze are parallel to the Basic level. Every third member added under anyone becomes a Gold member.
For example, if I have a Gold member and there’s a Gold member under my Gold member, that person becomes my Silver member. A Gold member under a Silver member becomes my Bronze member. All members under my Gold member who are not Gold members of any other user are considered my Gold members. Similarly, the Gold members of these Gold members become my Silver members, and the pattern continues for Silver and Bronze levels.
I do not have any connections with users who are Gold members of my Bronze members. At the Basic level, my concern is only up to two levels deep. Within these two levels, I only focus on members who are not Gold members of anyone else.
<?php
include "db/db_config.php";
// Properly include the Bootstrap CSS
echo "<link rel='stylesheet' href='../admin/css/bootstrap.css'>";
if(isset($_POST['submit'])){
$userid = $_POST['userid'];
}
else{
$userid = 1;
}
// Fetch all customers under the user and categorize them
$gold_members = [];
$silver_members = [];
$basic_members = [];
$bronze_members = [];
$trash_members = [];
$current_state = "gold";
echo "<div class='row p-0 m-0'>";
$sql_basic = mysqli_query($con, "SELECT * FROM `customers` WHERE `under_user`='$userid' AND `isgold`=0");
while ($row_basic = mysqli_fetch_array($sql_basic)) {
$basic_members[] = $row_basic['customer_id'];
$userid_2 = $row_basic['customer_id'];
$sql_basic_2 = mysqli_query($con, "SELECT * FROM `customers` WHERE `under_user`='$userid_2' AND `isgold`=0");
while ($row_basic_2 = mysqli_fetch_array($sql_basic_2)) {
$basic_members[] = $row_basic_2['customer_id'];
}
}
function categorize_customer($cust_id)
{
global $con;
global $gold_members;
global $silver_members;
global $bronze_members;
global $current_state;
// Initialize the state stack as an empty array
global $state_stack;
$state_stack = [];
// Function to handle categorization
function add_to_category($cust_id, $isgold)
{
global $gold_members, $silver_members, $bronze_members, $current_state;
if ($isgold == 1) {
if ($current_state == "gold") {
$silver_members[] = $cust_id;
$current_state = "silver";
} elseif ($current_state == "silver") {
$bronze_members[] = $cust_id;
$current_state = "bronze";
}
// If the current state is bronze, do nothing
} else {
if ($current_state == "gold") {
$gold_members[] = $cust_id;
} elseif ($current_state == "silver") {
$silver_members[] = $cust_id;
} elseif ($current_state == "bronze") {
$bronze_members[] = $cust_id;
}
}
}
function process_customers($cust_id)
{
global $con, $current_state, $state_stack;
$sql = mysqli_query($con, "SELECT * FROM `customers` WHERE `under_user`='$cust_id'");
while ($row = mysqli_fetch_array($sql)) {
$this_cust_id = $row['customer_id'];
// Save current state
array_push($state_stack, $current_state);
add_to_category($this_cust_id, $row['isgold']);
// Recursive call
process_customers($this_cust_id);
// Restore previous state
$current_state = array_pop($state_stack);
}
}
// Start processing with the initial customer ID
process_customers($cust_id);
}
$sql = mysqli_query($con, "SELECT * FROM `customers` WHERE `under_user`='$userid' AND `isgold`='1'");
while ($row = mysqli_fetch_array($sql)) {
$cust_id = $row['customer_id'];
$gold_members[] = $cust_id;
categorize_customer($cust_id);
}
function categorize_trash($cust_id)
{
global $gold_members, $silver_members, $bronze_members, $basic_members, $trash_members;
global $con;
$sql = mysqli_query($con, "SELECT * FROM `customers` WHERE `under_user`='$cust_id'");
while ($row = mysqli_fetch_array($sql)) {
if (!(in_array($row['customer_id'], $gold_members))) {
if (!(in_array($row['customer_id'], $silver_members))) {
if (!(in_array($row['customer_id'], $bronze_members))) {
if (!(in_array($row['customer_id'], $basic_members))) {
$trash_members[] = $row['customer_id'];
}
}
}
}
categorize_trash($row['customer_id']);
}
}
$sql = mysqli_query($con, "SELECT * FROM `customers` WHERE `under_user`='$userid'");
while ($row = mysqli_fetch_array($sql)) {
if (!(in_array($row['customer_id'], $gold_members))) {
if (!(in_array($row['customer_id'], $silver_members))) {
if (!(in_array($row['customer_id'], $bronze_members))) {
if (!(in_array($row['customer_id'], $basic_members))) {
$trash_members[] = $row['customer_id'];
}
}
}
}
categorize_trash($row['customer_id']);
}
?>
<form acion='' method=POST>
<input type=text name=userid>
<input type=submit name=submit>
</form>
<?php
// Display categorized members
echo "<div class='col-md-2'>
<br><br><br>
Basic Members" .
count($basic_members)
. "
<br><br>";
foreach ($basic_members as $basic) {
echo $basic . "<br>";
}
echo "</div><div class='col-md-2'>
<br><br><br>
Gold Members" .
count($gold_members)
. "
<br><br>";
foreach ($gold_members as $gold) {
echo $gold . "<br>";
}
echo "</div><div class='col-md-2'><br><br><br> Silver Members" .
count($silver_members)
. " <br><br>";
foreach ($silver_members as $silver) {
echo $silver . "<br>";
}
echo "</div><div class='col-md-2'><br><br><br> Bronze Members" .
count($bronze_members)
. " <br><br>";
foreach ($bronze_members as $bronze) {
echo $bronze . "<br>";
}
echo "</div>";
echo "<div class='col-md-2'><br><br><br> Trash Members" .
count($trash_members)
. " <br><br>";
foreach ($trash_members as $trash) {
echo $trash . "<br>";
}
echo "</div></div>";
Ali Usman Javed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.