Thanx for reply , I m facing still this problem I m sending you source code and structure of database, kindly look at that and help me
Table: customer
Field Type Attributes Null Default
login_id varchar(20) No
child1 varchar(20) No
child2 varchar(20) No
Table: payment_detail
Field Type Attributes Null Default Extra
pin_code int(20) No 0
login_id varchar(20) No
child_id varchar(20) No
direction int(1) No 0
paid_cheque int(1) No 0
As I told in beginning that this is a MLM project in which each person have to conveyance other to register and make a tree for himself, and if a person refer 9 person it means he had completed 1 step, so he’ll be paid for this 1 step. Ther are some rules for the step to be complete ie
1. There must be at least 2 direct referral 1 on left and 1 on right
2. step should follow the following combination
3 : 6
6 : 3
4 : 5
5 : 4
for each direct referral other then his 2 direct referral,, that person will be get a bonus.
I have solve this problem using the follwing code but it fails in case of more then 100 records.
I have a link Generate check when I click to this it calls the php page (generate_cheques.php) in which it calculate (strting from parent “shams”) each nodes left and right then make a binary combination of each from table “customer” and put this combination in another table “payment_detail”
Here is the code of php
generate_cheques.php
<?
require("functions.php");
$par = "shams";
insert($par);
$mainNodes = traverseSubtree($par,true);
for($i=0;$i<count($mainNodes);$i++)
{
$m=$mainNodes[$i];
insert($m);
}
$counter = insert_direction($par);
function insert_direction($par)
{
$post_with_threads_query = mysql_query("SELECT * FROM payment_detail WHERE paid_cheque='0' order by login_id") or die("no");
while($post_with_threads = mysql_fetch_array($post_with_threads_query))
{
$ch1 = $post_with_threads['child_id'];
$id = $post_with_threads['login_id'];
if($ch1)
{
if(is_left($id,$ch1)>0)
{
$leg_query = "update payment_detail set direction='1' where login_id='$id' and child_id='$ch1'";
}
else{
$leg_query = "update payment_detail set direction='0' where login_id='$id' and child_id='$ch1'";
}
mysql_query($leg_query);
}
}
}
$mainNodes = traverseSubtree1($par,true);
for($z=0;$z<count($mainNodes);$z++)
{
$left_total = left_count($mainNodes[$z]);
$right_total = right_count($mainNodes[$z]);
$steps = total_steps($mainNodes[$z],$left_total,$right_total,$counter=0);
if($steps>0)
{
paid($mainNodes[$z],$steps);
}
}
header("Location:[url]http://[/url]$HTTP_HOST/admin/acct_report.php");
?>
functions.php
<?
require("connection.php");
function traverseSubtree($node,$flag)
{
static $dir = array();
static $childNodes = array();
static $a=0;
if ($flag == true){
$childNodes = array();
$a = 0;
}
$post_with_threads_query = mysql_query("SELECT * FROM customer WHERE login_id ='$node'") or die("no");
while ($post_with_threads = mysql_fetch_array($post_with_threads_query))
{
$ch1 = $post_with_threads['child1'];
$ch2 = $post_with_threads['child2'];
if($ch1)
{
$childNodes[$a] = $ch1;
$a++;
traverseSubtree($ch1,false);
}
if($ch2)
{
$childNodes[$a] = $ch2;
$a++;
traverseSubtree($ch2,false);
}
}
//$childNodes[$a] = $node;
return $childNodes;
}
function is_left_child($parent,$chld)
{
$left;
$right;
$q="SELECT child1 FROM customer WHERE login_id ='$parent' and child1='$chld'";
$post_with_threads_query = mysql_query($q);
$rows = mysql_fetch_array($post_with_threads_query);
$ch = $rows['child1'];
if($ch)
{
if($ch==$chld)
{
$value=1;
}
else{
$value = threads($ch,$chld);
}
}
return $value;
}
function threads($childParent,$child)
{
$q2 = "SELECT * FROM customer WHERE login_id ='$childParent'";
$post_with_threads_query = mysql_query($q2);
while($post_with_threads = mysql_fetch_array($post_with_threads_query))
{
$ch1 = $post_with_threads['child1'];
$ch2 = $post_with_threads['child2'];
if($ch1)
{
if($ch1==$child)
{
$value=1;
}
else{
$value = threads($ch1,$child);
}
}
if($ch2)
{
if($ch2==$child)
{
$value=1;
}
else{
$value = threads($ch2,$child);
}
}
}
return $value;
}
function is_right_child($parent,$chld)
{
$q="SELECT child2 FROM customer WHERE login_id ='$parent' and child1='$chld'";
$post_with_threads_query = mysql_query($q);
$rows = mysql_fetch_array($post_with_threads_query);
$ch = $rows['child2'];
if($ch)
{
$value = threads($ch,$chld);
}
}
function is_left($id,$check)
{
$a;
$nodes=left_child_parents($id);
for($j=0;$j<count($nodes);$j++)
{
if($nodes[$j]==$check)
{
$a++;
}
}
return $a;
}
function left_child_parents($parent)
{
$post_with_threads_query = mysql_query("SELECT child1 FROM customer WHERE login_id ='$parent'") or die("no");
$post_with_threads = mysql_fetch_array($post_with_threads_query);
$ch1 = $post_with_threads['child1'];
if($ch1)
{
$nodes = threadsL($ch1,$parent,true);
}
return $nodes;
}
function traverseSubtree1($node,$flag)
{
static $dir = array();
static $childNodes = array();
static $a=0;
if ($flag == true){
$childNodes = array();
$a = 0;
}
$post_with_threads_query = mysql_query("SELECT * FROM customer WHERE login_id ='$node'") or die("no");
while ($post_with_threads = mysql_fetch_array($post_with_threads_query))
{
$ch1 = $post_with_threads['child1'];
$ch2 = $post_with_threads['child2'];
if($ch1)
{
$childNodes[$a] = $ch1;
$a++;
traverseSubtree1($ch1,false);
}
if($ch2)
{
$childNodes[$a] = $ch2;
$a++;
traverseSubtree1($ch2,false);
}
}
$childNodes[$a] = $node;
return $childNodes;
}
function total_steps($id,$left,$right)
{
$counter = 0;
while($left+$right>=9&&$left>4&&$right>3)
{
$counter++;
$left = $left-5;
$right = $right-4;
update_payment_detail($id,5,4);
}
while($left+$right>=9&&$left>5&&$right>2)
{
$counter++;
$left = $left-6;
$right = $right-3;
update_payment_detail($id,4,5);
}
while($left+$right>=9&&$left>3&&$right>4)
{
$counter++;
$left = $left-4;
$right = $right-5;
update_payment_detail($id,6,3);
}
while($left+$right>=9&&$left>2&&$right>5)
{
$counter++;
$left = $left-3;
$right = $right-6;
update_payment_detail($id,3,6 );
}
return $counter;
}
?>