<?php

	session_start();
	include('database.php');
	DB_connexion(); 
			
			
	if(!empty($_POST) )
	{
		$_SESSION['sauvegarde_formulaire'] = $_POST ;
     
		$page_actuelle = $_SERVER['PHP_SELF'] ;
		if(!empty($_SERVER['QUERY_STRING']))
		{
			$page_actuelle .= '?' . $_SERVER['QUERY_STRING'] ;
		}
     
		header('Location: ' . $page_actuelle);
		exit;
	}
	
	if(isset($_SESSION['sauvegarde_formulaire']))
	{
		$_POST = $_SESSION['sauvegarde_formulaire'] ;
		unset($_SESSION['sauvegarde_formulaire']);		
	}		
?>


<?php
function DB_to_file($filename)
{
				// sauvegarde du classement dans un fichier
	
				$sql = "SELECT * FROM parties ORDER by date asc"; 
				$results = mysql_query($sql);
				$nb_parties = mysql_num_rows($results);
							
				while($row = mysql_fetch_assoc($results))
				{
					$blanc = $row['w_name'];
					$noir = $row['b_name'];
					$date = $row['date'];
					$resultat = $row['result'];
					$date_partie = date('d/m/Y', $date);
					
					if($resultat == 1)
					{
						$_score_blanc = 1;
						$_score_noir = 0;
					}
					if($resultat == 2)
					{
						$_score_blanc = 0;
						$_score_noir = 1;
					}
					if($resultat == 3)
					{
						$_score_blanc = 0.5;
						$_score_noir = 0.5;
					}
				
					$table_csv[0] = $date_partie;
					$table_csv[1] = $blanc;
					$table_csv[2] = $noir;
					$table_csv[3] = $_score_blanc;
					$table_csv[4] = $_score_noir;

					$table_fichier[] = $table_csv;
				}
	
				$nom_fichier = $filename;
				chdir("sauvegarde_echec");
											
				$fp = fopen($nom_fichier, 'w'); 
				for ($i=0; $i < $nb_parties; $i++)
				{
					$str_comma_separated = implode(";", $table_fichier[$i]);
					fputs ($fp, $str_comma_separated);
					if($i != $nb_parties - 1)
					fputs($fp, "\n");
				}
						
				fclose($fp);
		
}


		if( isset($_POST['valide_form']) && $_POST['valide_form'] == "ok" )
		{
			echo " Job a faire ...";
		}
		

?>
	
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document sans nom</title>

<script type="text/javascript">
function valide_form()
{
	document.getElementById('valide_form').value="ok";
	
	alert("plouf");
}	
</script>

<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

</head>


<body>
<?php
		$jour = date('d-m-Y');
		$heure = date("H_i_s");
		$infos = $jour.'-'.$heure;
		$filename = "sauvegarde_edition_" .$infos .".txt"; // "test.csv";
		DB_to_file($filename);

		$fp = fopen($filename, 'r') or die('Could not open file!');
		$data = fread($fp, filesize($filename)) or die("Could not read file!");

		

		 echo '<h3>Contenu du fichier</h3>';
	/*	 
		<form action='lire_csv.php' method= 'post' >
		<input type='hidden' name='valide_form' id='valide_form' value='' />
		<textarea style='resize:none' name='datas' cols='50%' rows='20' > $data </textarea>
		<input type='submit' value='Sauver' id='sauve' name='sauve' onclick='valide_form()'>
		
		
		
		</form>";*/
?>

		<form method="post" id="form" action="#" > 
		<input type="hidden" name="valide_form" id="valide_form" value="" />
		<input type='submit' value='Sauver' id='sauve' name='sauve' onclick='valide_form()'>
		</form>	
		

</body>
</html>	
	
	
	
	
	

