<?php

	include('database.php');
	DB_connexion(); 
	
	$results = mysql_query("SELECT * FROM classement");
	$nb_joueurs = mysql_num_rows($results);
	//echo $nb_joueurs;
	
	$results2 = mysql_query("SELECT gagnant FROM chessmaster");
	while($row = mysql_fetch_assoc($results2))
	{
		$tab_gagnant[] = $row['gagnant'];
	}
	
	//print_r($tab_gagnant);
	
	$sans_doublon = array_unique($tab_gagnant);
	$reindex = array_values($sans_doublon);	
	//print_r($reindex);
	
	for ($i=0; $i < count($reindex); $i++) 
	{
			$cpt = 0;
			$up = 0;
			$tab_temp = array();
		
			//echo $reindex[$i];
			
			for ($j=0; $j < count($tab_gagnant); $j++) 
			{
				$test = strcmp($reindex[$i], $tab_gagnant[$j]);
				//echo $test;
				if($test == 0)
				{
					$cpt = $cpt + 1;
					$flag = "master";
					$up = $up + 1;

				}	
				if($test != 0 && $flag == "master")
				{
					$tab_temp[] = $up;
					$up = 0;
				}
				if($j == count($tab_gagnant) - 1)
				{
					if( $test == 0)
					{
							$tab_temp[] = $up;
					}
				}
				
			}
		
			//$tab_temp = array_unique($tab_temp);
			// supprime les 0 
			foreach($tab_temp as $array_key=>$array_item)
			{
				if($tab_temp[$array_key] == 0)
				{
					unset($tab_temp[$array_key]);
				}
			}
			
			$tab_temp[] = $cpt;
			$tab_temp[] = $reindex[$i];
			$tab_glob[] = array_values($tab_temp);
		
			//echo "+++++ " .$cpt ."<br>";
			
	}	
	
	
	

	//print_r(array_unique($tab_glob));
	echo "<pre>";

	print_r($tab_glob);	
	
	
	$results = mysql_query("SELECT gagnant FROM chessmaster");
	while($row = mysql_fetch_assoc($results))
	{
		$tab_gagnant[] = $row['gagnant'];
	}
	
	print_r($tab_gagnant);	
	
	echo end($tab_gagnant);
	
	
	$sql = "SELECT * FROM chessmaster";
	$results = mysql_query($sql);
	$n =  mysql_num_rows($results);
	
	
	$sql = "SELECT * FROM chessmaster WHERE id = '$n' ";
	$results = mysql_query($sql);
	echo mysql_num_rows($results);
	$row = mysql_fetch_assoc($results);
	echo $row['date'];
	
?>