<?php

	/*header('Content-Type: application/json');
	ini_set('display_startup_errors',1);
	ini_set('display_errors',1);
	error_reporting(1);
	ini_set("log_errors", 1);
	ini_set("error_log", "php-error.log");
	date_default_timezone_set('Asia/Kolkata');

	require_once("database.php");
    $data = json_decode(trim(file_get_contents("php://input")),true);*/
	


	/*print "<table border='1' width='50%' align='center'>";

	print "<tr>
				<td>Sr No</td>
				<td>Member ID</td>
				<td>Status</td>
    	  </tr>";*/

	$db = new PDO('mysql:host=localhost;dbname=mybooster;', 'root', 'root');
	$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    $db->beginTransaction();


	$data = getProfile($db);
	$memberIDIndex = 1;


    foreach($data as $row)
	{
        if(addNetwork($row['refer_id'], $db))
		{

			echo "<tr>";
			echo "<td>" . $memberIDIndex++  . "</td>";
			echo "<td>" . $row['refer_id']   . "</td>";
			echo "<td>" . ''  . "</td>";
			echo "</tr>";
		}

		else
		{

			echo "<tr>";
			echo "<td>" . '' . "</td>";
			echo "<td>" . $row['refer_id']   . "</td>";
			echo "<td>" . ''  . "</td>";
			echo "</tr>";
		}
	
	
	}


	function getProfile($db)
	{
		try{

			//$sql = "SELECT refer_id from my_registration order by date_entered ASC Limit 8000, 1000";
			$sql = "SELECT refer_id from my_network  order by id ASC Limit 8000, 1000 ";
			//echo $sql . "<br/>";
			$stmt= $db->prepare($sql);
			$stmt->execute();
    		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			return $result;
		

		}


		catch(Exception $e)
		{
			$e->getMessage();
    		$db->rollBack();
			return false;

		}

	}


   
	function addNetwork($referID, $db)
	{

		try{

				$sql = "SELECT refer_id from my_network where refer_id = '$referID'";
				$stmt= $db->prepare($sql);
				$stmt->execute();
				$result = $stmt->fetch(PDO::FETCH_ASSOC);

				if($result['refer_id']==NULL || $result['refer_id']=='')
				{
					$sql = "INSERT IGNORE INTO my_network(refer_id) values(?)";
					$stmt = $db->prepare($sql);
					$stmt->execute(array($referID));
				}


				else
			    {

					addMembers($referID, $referID, $db);


			    }

				return true;
	
		}


		catch(Exception $e)
		{
			$e->getMessage();
			$db->rollBack();
			return false;

		}

	}



	function addMembers($referID, $sponsorID, $db)
	{

		if($referID=='123123629' || $referID == '233791797')
			return true;

		echo "\n" . "<br/>";
		echo "Member ID:" . $referID;


		try{
                if($referID == $sponsorID)
					$sql = "SELECT sponsor_id, left_node, right_node from my_registration where refer_id = '$referID'";

				else
					$sql = "SELECT sponsor_id, left_node, right_node from my_registration where refer_id = '$sponsorID'";

				$stmt= $db->prepare($sql);
				$stmt->execute();
				$result = $stmt->fetch(PDO::FETCH_ASSOC);

				$sponsorID = $result['sponsor_id'];

				if($result['sponsor_id']=='' || $result['sponsor_id']==NULL)
					return true;
				
				else
			    {
					if($result['left_node'] == 1)
					{
						$sql = "Update my_network set leftNetwork=CONCAT(leftNetwork, CONCAT('$referID', ',')) where refer_id='$sponsorID'";
						//echo $sql;
						$stmt = $db->prepare($sql);
						$stmt->execute();
					}


					else
					{
						$sql = "Update my_network set rightNetwork=CONCAT(rightNetwork, CONCAT('$referID', ',')) where refer_id='$sponsorID'";
						//echo $sql;
						$stmt = $db->prepare($sql);
						$stmt->execute();

					}


			    }

					echo "\n" . "<br/>";
		            //echo "sponsor ID:" . $result['sponsor_id'];



				if($result['sponsor_id'] == '' || $result['sponsor_id'] == NULL || $result['sponsor_id'] == '123123629' || $result['sponsor_id'] == '233791797')
					return true;

				else
					addMembers($referID, $result['sponsor_id'], $db);




	
		}


		catch(Exception $e)
		{
			$e->getMessage();
			$db->rollBack();
			return false;

		}

	}


	$db->commit();


?>