<?php

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

	print "<tr>
				<td>Sr No</td>
				<td>Member Name</td>
				<td>Member ID</td>
				<td>Purchase Date</td>
				<td>PV</td>
			</tr>";

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

	$data='';
	$i=1;

	$referID = $_REQUEST['refer_id'];


	$data = getDirect($referID, $db);

	$total = 0; 

	foreach($data as $row)
	{

		echo "<tr>";
		echo "<td>" . $i++  ."</td>";
		echo "<td>" . $row['name']  ."</td>";
		echo "<td>" . $row['refer_id']  ."</td>";
		echo "<td>" . $row['date_time']  ."</td>";
		echo "<td>" . $row['pv']  ."</td>";
		echo "</tr>";
		$total += $row['pv'];
	
	}

		echo "<tr>";
		echo "<td colspan='3'></td>";
		echo "<td>TOTAL</td>";
		echo "<td>" . $total  . "</td>";
		echo "</tr>";

	   


	function getDirect($referID, $db)
	{
		try{
			$data = array();
			
			$sql = "SELECT CONCAT(a.name,  ' ', a.last_name ) AS name, a.refer_id, date_format(b.payout_start_date, '%d-%m-%Y') as date_time, b.purchase_pv as pv FROM my_registration 
			as a right join my_direct_payout as b on a.refer_id=b.refer_id where b.refer_id='$referID'
			 AND (b.payout_start_date between '2019-08-15 00:00:00' AND '2019-09-13 23:59:59')";

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

			return $result;
	
		}


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

		}

	}

	

	
	
?>