codewalkz 0 Junior Poster in Training

I am currently working on an MLM System which supports Unilevel with Dynamic Compression. So far, though the below code looks really messy, it was able to get what I want which is to get the Unilevel 8 Levels Deep. The system should run this way:

1 is sponsor of 2 & 3 so UNILEVEL 1 has 2 childs ( 2 and 3) Now, the Level 2 Starts with Children 2 and 3 and so on up to 8th level. The code below did it.

My problem now is, if a parent, like for example Number 2 is INACTIVE ( Active or Inactive ), its child should replace his position.

If you know anything or if you can refer me to something related, please do.

Thank you so much!

<?php
  <?php
mysql_connect("localhost", "myhygeia_jun", "xxxxxxxc") or die(mysql_error());
mysql_select_db("myhygeia_agents") or die(mysql_error());

//GET THE ID OF ROOT NODE
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$query = "SELECT id FROM agents WHERE username = '$username'";
$result = mysql_query($query) or die('Error :On Right Head ' . mysql_error());
$row = mysql_fetch_array($result);
$parent = $row['id'];

//----------------------------------------------------------------------------------------------------------

//GET THE TOTAL OF LEVEL 1
$result = mysql_query("SELECT COUNT(*) FROM agents WHERE sponsor = '$parent'")or die(mysql_error());  
$output = array();
while ($row = mysql_fetch_array($result)) {
$lvl1 = $row['COUNT(*)'];
echo "<b>Total Level 1 Downlines</b> : ";
echo " <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>$lvl1 <br><br>";
}
//GET THE ID FIRST LEVEL FRONTLINES AND PASS TO GET 2ND LEVEL
$result = mysql_query("SELECT id, lastname FROM agents WHERE sponsor = '$parent'")or die(mysql_error());  
$output = array();
while ($row = mysql_fetch_array($result)) {
  $output[] = "sponsor = '".$row['id']."' ";
}
$level1 = implode(' or ', $output); 

//----------------------------------------------------------------------------------------------------------

//GET THE TOTAL OF LEVEL 2
$result = mysql_query("SELECT COUNT(*) FROM agents WHERE ($level1) ")or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
$lvl2parent = $row['COUNT(*)'];
echo "<b>Total Level 2 Downlines</b> : ";
echo "$lvl2parent <br><br>"; 
} 
//GET THE ID SECOND LEVEL FRONTLINES AND PASS TO GET 3rD LEVEL
$result = mysql_query("SELECT id, lastname FROM agents WHERE ($level1)")or die(mysql_error());  
$output = array();
while ($row = mysql_fetch_array($result)) {
  $output[] = "sponsor = '".$row['id']."' ";
}
$level2 = implode(' or ', $output); 

//-----------------------------------------------------------------------------------------------------------

//GET THE TOTAL OF LEVEL 3
$result = mysql_query("SELECT COUNT(*) FROM agents WHERE ($level2) ")or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
$lvl3parent = $row['COUNT(*)'];
echo "<b>Total Level 3 Downlines</b> : ";
echo "$lvl3parent <br><br>"; 
} 
//GET THE ID THIRD LEVEL FRONTLINES AND PASS TO GET 4th LEVEL
$result = mysql_query("SELECT id, lastname FROM agents WHERE ($level2)")or die(mysql_error());  
$output = array();
while ($row = mysql_fetch_array($result)) {
  $output[] = "sponsor = '".$row['id']."' ";
}
$level3 = implode(' or ', $output); 

//-----------------------------------------------------------------------------------------------------------

//GET THE TOTAL OF LEVEL 4
$result = mysql_query("SELECT COUNT(*) FROM agents WHERE ($level3) ")or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
$lvl4parent = $row['COUNT(*)'];
echo "<b>Total Level 4 Downlines</b> : ";
echo "$lvl4parent <br><br>"; 
} 
//GET THE ID FOURTH LEVEL FRONTLINES AND PASS TO GET 5TH LEVEL
$result = mysql_query("SELECT id, lastname FROM agents WHERE ($level3)")or die(mysql_error());  
$output = array();
while ($row = mysql_fetch_array($result)) {
  $output[] = "sponsor = '".$row['id']."' ";
}
$level4 = implode(' or ', $output); 

//-----------------------------------------------------------------------------------------------------------

//GET THE TOTAL OF LEVEL 5
$result = mysql_query("SELECT COUNT(*) FROM agents WHERE ($level4) ")or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
$lvl5parent = $row['COUNT(*)'];
echo "<b>Total Level 5 Downlines</b> : ";
echo "$lvl5parent <br><br>"; 
} 
//GET THE ID FIFTH LEVEL FRONTLINES AND PASS TO GET 6TH LEVEL
$result = mysql_query("SELECT id, lastname FROM agents WHERE ($level4)")or die(mysql_error());  
$output = array();
while ($row = mysql_fetch_array($result)) {
  $output[] = "sponsor = '".$row['id']."' ";
}
$level5 = implode(' or ', $output); 

//-----------------------------------------------------------------------------------------------------------

//GET THE TOTAL OF LEVEL 6
$result = mysql_query("SELECT COUNT(*) FROM agents WHERE ($level5) ")or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
$lvl6parent = $row['COUNT(*)'];
echo "<b>Total Level 6 Downlines</b> : ";
echo "$lvl6parent <br><br>"; 
} 
//GET THE ID SIXTH LEVEL FRONTLINES AND PASS TO GET 7TH LEVEL
$result = mysql_query("SELECT id, lastname FROM agents WHERE ($level5)")or die(mysql_error());  
$output = array();
while ($row = mysql_fetch_array($result)) {
  $output[] = "sponsor = '".$row['id']."' ";
}
$level6 = implode(' or ', $output); 

//-----------------------------------------------------------------------------------------------------------

//GET THE TOTAL OF LEVEL 7
$result = mysql_query("SELECT COUNT(*) FROM agents WHERE ($level6) ")or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
$lvl7parent = $row['COUNT(*)'];
echo "<b>Total Level 7 Downlines</b> : ";
echo "$lvl7parent <br><br>"; 
} 
//GET THE ID 7TH LEVEL FRONTLINES AND PASS TO GET 8TH LEVEL
$result = mysql_query("SELECT id, lastname FROM agents WHERE ($level6)")or die(mysql_error());  
$output = array();
while ($row = mysql_fetch_array($result)) {
  $output[] = "sponsor = '".$row['id']."' ";
}
$level7 = implode(' or ', $output); 

//-----------------------------------------------------------------------------------------------------------

//GET THE TOTAL OF LEVEL 8
$result = mysql_query("SELECT COUNT(*) FROM agents WHERE ($level7) ")or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
$lvl8parent = $row['COUNT(*)'];
echo "<b>Total Level 8 Downlines</b> : ";
echo "$lvl8parent <br><br>"; 
} 

?>