开发者

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

开发者 https://www.devze.com 2023-04-02 14:37 出处:网络
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 exe

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']."'");
}
?>


make 2 columns in your DB as Left count and Right count for each and every user . then with the reference id u can get the count of each right and each left legs . hope this works !!!


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. : )


Just use MySQL Limts with your queries.

Limit is used to limit your MySQL query results to those that fall within a specified range. You can use it to show the first X number of results, or to show a range from X - Y results. It is phrased as Limit X, Y and included at the end of your query. X is the starting point (remember the first record is 0) and Y is the duration (how many records to display).

Examples:

SELECT * FROM your_table LIMIT 0, 10 --// This will display the first 10 results from the database. SELECT * FROM your_table LIMIT 5, 5 --// This will show records 6, 7, 8, 9, and 10


This is a complex problem that will require knowledge of the database structure and test data to help debug. I've done this exact same thing, but the code can very depending on how you structure things.


public function getCountChilds()
	{
	    $count = 0;
	      $this->db->where('user_id',$this->session->userdata('user_id'));
	     $sponcer_code= $this->db->get('registration')->row();



	    	$this->db->from('registration');
	        $this->db->where('referel', $sponcer_code->sponcer_id);
	       $query =  $this->db->get();
	       //echo count($query);
	      $count2=count($query->result());
	       //die();
	       $count=0;

	       
	        foreach($query->result() AS $objChild)
	        {
				$this->db->from('registration');
				$this->db->where('referel', $objChild->sponcer_id);
				$query2 =  $this->db->get();
				$count=$count+count($query2->result()); 

	        }
	       return $count+$count2;
	}


I wrote this code and this returned me a valid data I wanted for my mlm software.

function getDlink($parent, $level){
        $users = $this->db->query('SELECT * FROM `member` WHERE parent_id="parent_id"')->result_array();
        static $arr = array();
        if ($users != null){
            foreach ($users as $user){
                $this->db->where('id', $user['id']);
                $u_data = $this->db->get('test')->row_array();
                array_push($arr, $user['id']);
                $parent_child = $user['id'];
                $this->getDlink($parent_child, 0);
            }
        }
        return $arr;
    }

this will return you an array and then count an element of an array for total count

0

精彩评论

暂无评论...
验证码 换一张
取 消