How to count Total Downline , Give me any Idea (MLM project)

Anyone give me any Idea about How I can Count down-line for a MLM project (PHP/MySQL). Currently I’m using the below method. but when my database record become 300 or more, then it shows: maximum execution time of 30 second exceeded in line 51. (I think this happens for unlimited loop). please help me. what should be proper way?

MY CURRENT CODE:

<?php
require_once('configuration.php');

/// main function which will return Total Node Count
function nodecount($id) {
   $query = "SELECT * FROM ".memberlogtbl." WHERE locationid='".$id."' and topup >'0'";
   $result = mysql_query($query);
   $count = mysql_num_rows($result);

   $_SESSION['hackx8'] = $_SESSION['hackx8'] + $count;
   while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
      nodecount($row["id"]);
   }
   mysql_free_result($result);
   return "";
}

$q_l = mysql_query("SELECT `left`,id FROM ".memberlogtbl." WHERE `left` >'0' ORDER BY id ASC");
while($updt_tbl_l = mysql_fetch_row($q_l)) {
   unset($_SESSION['hackx8']);
   $total_l = nodecount($updt_tbl_l['0']).$_SESSION['hackx8'] + 1;
   mysql_query("UPDATE ".memberlogtbl." SET totalleft='$total_l' WHERE id='".$updt_tbl_l['1']."'");
}
/// Update database with letest Total right Count
$q_r = mysql_query("SELECT `right`,id FROM ".memberlogtbl." WHERE `right` >'0' ORDER BY id ASC");
while($updt_tbl_r = mysql_fetch_row($q_r)) {
   unset($_SESSION['hackx8']);
   $total_r = nodecount($updt_tbl_r['0']).$_SESSION['hackx8'] + 1;
   mysql_query("UPDATE ".memberlogtbl." SET totalright='$total_r' WHERE id='".$updt_tbl_r['1']."'");
}
?>

Answer

i’ve done a similar project. what i did was (after doing a lot of research/googling) use a left and right values for each node in the binary tree. i can’t remember where i read about this method but it works. if there are 6 nodes for example, the top/root node would get 1 & 12 as it’s LEFT & RIGHT values respectively. you can then get its total downline by (RIGHT – LEFT – 1) / 2, i think. i hope this could help you on your project. : )

Leave a Reply

Your email address will not be published. Required fields are marked *