<?php
	session_start();
	//include('ELO_code.php');
	include('correlation.php');
	
	//include('database.php');
	//DB_connexion(); 
	
	echo '<a href=';
	echo $_SERVER['HTTP_REFERER']; 
	echo '>Retour</a>';

	echo "<br><br>";
	echo "Import du fichier ...";
	echo "<br><br>";
	
	error_reporting(0);
	header( 'content-type: text/html; charset=utf-8' );
	
/******************************************/
function lire_csv($nom_fichier)
{
	$table_csv = array();
	
	//mysql_query('TRUNCATE TABLE chessmaster;');
	
	$file = fopen ($nom_fichier,"r");
		
	while(! feof($file))
	{
			$tmp = fgets($file);
			if($tmp != "")
			{
				$tmp = strtolower($tmp);
				$tmp = str_to_noaccent($tmp);
				$pieces = explode(";", $tmp);
				$pieces[0] = mise_en_forme_date($pieces[0]);
				$table_csv[] = $pieces;
				
				$sql = " INSERT INTO chessmaster ";
				$sql = $sql . " (blanc, noir, gagnant, date) VALUES ";
				$sql = $sql . " ('$pieces[1]', '$pieces[2]','$pieces[3]','$pieces[0]') ";
				//echo $sql."<br>";
				//$result = mysql_query($sql);
				/*if (!$result)
				{ 
					echo("ERROR: " . mysql_error() . "\n$sql\n");	
				}*/
			}
	}
	
	fclose($file);
	
	echo "<pre>";
	//print_r($table_csv);
	
	// CREATE TABLE IF NOT EXISTS `chessmaster` (
  // `id` int(11) NOT NULL AUTO_INCREMENT,
  // `blanc` varchar(30) NOT NULL,
  // `noir` varchar(30) NOT NULL,
  // `gagnant` varchar(30) NOT NULL,
  // `date` int(11) NOT NULL DEFAULT '0',
   // PRIMARY KEY (`id`)
// )

	// nombre d'enregistrements
	$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[] = $tab_temp;
		
			//echo "+++++ " .$cpt ."<br>";
			
	}	
	
	
	

	//print_r(array_unique($tab_glob));
	
	print_r($tab_glob);
	
	/*$test = in_array("fabien", $tab_gagnant);
	echo $test;
	$test = in_array("laurent", $tab_gagnant);
	echo $test;*/
	
	
}
/******************************************/
function str_to_noaccent($str)
{
    $tmp = $str;
    $tmp = preg_replace('#Ç#', 'C', $tmp);
    $tmp = preg_replace('#ç#', 'c', $tmp);
    $tmp = preg_replace('#è|é|ê|ë#', 'e', $tmp);
    $tmp = preg_replace('#È|É|Ê|Ë#', 'E', $tmp);
    $tmp = preg_replace('#à|á|â|ã|ä|å#', 'a', $tmp);
    $tmp = preg_replace('#@|À|Á|Â|Ã|Ä|Å#', 'A', $tmp);
    $tmp = preg_replace('#ì|í|î|ï#', 'i', $tmp);
    $tmp = preg_replace('#Ì|Í|Î|Ï#', 'I', $tmp);
    $tmp = preg_replace('#ð|ò|ó|ô|õ|ö#', 'o', $tmp);
    $tmp = preg_replace('#Ò|Ó|Ô|Õ|Ö#', 'O', $tmp);
    $tmp = preg_replace('#ù|ú|û|ü#', 'u', $tmp);
    $tmp = preg_replace('#Ù|Ú|Û|Ü#', 'U', $tmp);
    $tmp = preg_replace('#ý|ÿ#', 'y', $tmp);
    $tmp = preg_replace('#Ý#', 'Y', $tmp);
     
    return ($tmp);
}
/******************************************/
function mise_en_forme_date($date)
{
		list($jour, $mois, $annee) = explode('/', $date);
		$timestamp = mktime (0, 0, 0, $mois, $jour, $annee);
		return($timestamp);
}
/******************************************/

	$nom_fichier = "ON Chess master.csv";
	lire_csv($nom_fichier);
	
?>