<?php

require '.db.php';

echo '<title>Statistics</title>';

echo '<link rel="stylesheet" type="text/css" href="style.css">';

/****************
*     MENCS     *
****************/

$did = $_GET['did'];


echo '<center><h2><a style="color:#fff !important;text-decoration:none !important;" href="fstats.php">FULL STATS</a></h2><br>';

echo '<h3>2021-2022 Season Monster Energy Cup Statistics</h3>';

echo '<div class="container">

<center>';

$db = new mysqli("localhost", $username, $password, $database);

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

/*echo '<p>';

$current = ("SELECT * FROM res
LEFT JOIN
drivers on res.did = drivers.did
LEFT JOIN
tracks on res.tid = tracks.tid") or die(mysql_error());

if(!$result = $db->query($current)){
    die('There was an error running the query [' . $db->error . ']');
}

$row = $result->fetch_array();

$did = $row['did'];
$track = $row['track'];
$poles = $row['poles'];
$points = $row['points'];
$driver = $row['driver'];
$finished = $row['finished'];
$t5 = $row['t5'];
$t10 = $row['t10'];

echo '</p>*/

echo '<table style="border:1px solid #000000; width:70% !important">
	<tr align="center">
		<th>Driver</th>
		<th>Points</th>
		<th>Wins</th>
		<th>Poles</th>
		<th>Top 5`s</th>
		<th>Top 10`s</th>
		<th>DNF`S</th>
		<th>DNS`S</th>
	</tr>';

$race = ("SELECT * FROM res
LEFT JOIN
drivers on res.did = drivers.did 
LEFT JOIN
points on res.did = points.did where drivers.did = '$did'");

if(!$result = $db->query($race)){
    die('There was an error running the query [' . $db->error . ']');
}

$row = $result->fetch_array();

$did = $row['did'];
$track = $row['track'];
$poles = $row['poles'];
$points = $row['points'];
$driver = $row['driver'];
$finished = $row['finished'];
$t5 = $row['t5'];
$t10 = $row['t10'];

echo '<tr class="schart" align="center">
		<td class="schart">'.$driver.'</td>
<td class="schart">';

/****************
* Points System *
****************/

$sql=("SELECT * FROM points
LEFT JOIN
drivers on points.did = drivers.did where points.did='$did'");

$result = mysqli_query($db,$sql);

while ($row = mysqli_fetch_assoc($result))
{ 
echo $points;
}



		echo '</td>
		<td class="schart">';

/**************
* Wins System *
**************/

$sqlw=("SELECT sum(finished) as wins FROM res where did='$did' AND finished = 1");

$result = mysqli_query($db,$sqlw);

while ($row = mysqli_fetch_assoc($result))
{ 

if($row['wins'] == 0){
echo '0';
}
else
{
echo $row['wins'];
}
}

		echo '</td><td class="schart">';

/***************
* Poles System *
***************/

$sqlp=("SELECT count(pole) as poles FROM res
LEFT JOIN
drivers on res.did = drivers.did where res.did='$did' and pole = 1");

$result = mysqli_query($db,$sqlp);

while ($row = mysqli_fetch_assoc($result))
{ 
   echo $row['poles'];
}

/*$sqlw=("SELECT sum(finished) as wins FROM res where did='$did' AND finished = 1");

$result = mysqli_query($db,$sqlw);

while ($row = mysqli_fetch_assoc($result))
{ 
   echo $row['wins'];
}*/

		echo '</td><td class="schart">';

/*****************
* Top 5`s Totals *
*****************/

$sql3=("SELECT sum(t5) as t5 FROM res where did='$did'");

$result = mysqli_query($db,$sql3);

while ($row = mysqli_fetch_assoc($result))
{ 
   echo $row['t5'];
}

		echo '</td><td class="schart">';

/******************
* Top 10`s Totals *
******************/

$sql4=("SELECT sum(t10) as t10 FROM res where did='$did'");

$result = mysqli_query($db,$sql4);

while ($row = mysqli_fetch_assoc($result))
{ 
   echo $row['t10'];
}

		echo '</td><td class="schart">';

/*************
* DNF Totals *
*************/

$sqldnf=("SELECT sum(dnf) as dnfs FROM res where did='$did'");

$result = mysqli_query($db,$sqldnf);

while ($row = mysqli_fetch_assoc($result))
{ 
   echo $row['dnfs'];
}

		echo '</td><td class="schart">';

/***************
* DNS`s Totals *
***************/

$sql5=("SELECT sum(dns) as dns FROM res where did='$did'");

$result = mysqli_query($db,$sql5);

while ($row = mysqli_fetch_assoc($result))
{ 
   echo $row['dns'];
}

	echo '</td></tr>

</table><br>
</center>
</div>';

//}

//}

?>


