﻿ <html>
<a href="createur.php"> créer un sondage</a><br/>
<a href="liste_sondages.php"> liste des sondages</a><br/>
bravooo tu as voté! 

<?php
print_r($_POST);


$pseudo=$_POST[pseudo];
$id_sondage=$_POST[id_sondage];
$numero_sondage= $_POST[id_sondage];
$vote=$_POST[$numero_sondage];
$nb_oui_non=$_POST[nb_oui_non];

$date= date('Y-m-d G:i:s');


$id_sondage=$numero_sondage;
mysql_connect("localhost","tykayn","password") OR die(mysql_error());
mysql_select_db("tykayn") OR die(mysql_error());

$sql="select count(id_votant) as nbr from tk_tbl_reponse where id_sondage_reponse='".$id_sondage."'";
$reponse=mysql_query("$sql") or die( "$sql <br/>". mysql_error());
$donnees=mysql_fetch_array($reponse);

$id_votant=$donnees[nbr]++;

echo' id_votant: '.$id_votant.' <input type="hidden" name="id_votant" value="'.$id_votant.'"';
 
 
  //ON ENREGISTRE POUR les oui non
 
$sql='insert into tk_tbl_reponse VALUES';

for($i=0;$i<$nb_oui_non;$i++)
{
$sql.='(
NULL,
NULL,
"'.$vote.'",
NULL,
NULL,
"'.$date.'",
"'.$id_sondage.'",
"'.$pseudo.'",
"'.$id_votant.'"
)';
}
$sql.=',';
 
 
 
 //ON ENREGISTRE POUR LE VOTE RADIO

$sql='insert into tk_tbl_reponse VALUES
(
NULL,
NULL,
"'.$vote.'",
NULL,
NULL,
"'.$date.'",
"'.$id_sondage.'",
"'.$pseudo.'",
"'.$id_votant.'"
)

';


echo'<br/>enregistréééééééééééééééééééééééééééééééééééééééééééééééééééééé<br/>';
mysql_query($sql) or die( "$sql <br/>". mysql_error());


$POST[saved]=0;

echo "id_votant: $id_votant<br/>";


echo"<br/>sondage n° $numero_sondage. ton vote: $vote<br/>
la date: $date <br/>
";

/////////////////////////////////////////////////////////

















//nb de votes pour le sondage
$sql="select count(*) as nbs from tk_tbl_reponse where id_sondage_reponse='".$id_sondage."' AND radio_reponse!='NULL'";
$reponse=mysql_query("$sql") or die( "$sql <br/>" .mysql_error());
$donnees=mysql_fetch_array($reponse);
echo"<br/>Il ya $donnees[nbs] votes pour ce sondage<br/><br/>STATISTIQUES DE RÉPONSE:<br/><br/>";
$votes_totaux=$donnees[nbs];

//recrutement des choix pour le sondage
$compteur=0;
$sql="select * from tk_tbl_options where id_sondage='".$id_sondage."'";
$reponse=mysql_query("$sql") or die( "$sql <br/>" .mysql_error());
while($donnees=mysql_fetch_array($reponse))
{
$compteur++;
$array_choix[$compteur]="$donnees[question]";
}


//affiche le graphique
echo" <table>";
$nb_de_choix=$compteur+1;
	for ($i=1 ; $i < $nb_de_choix ; $i++)
	{
	$sql="select count(*) as nbs from tk_tbl_reponse where id_sondage_reponse='".$id_sondage."' AND radio_reponse='".$i."' AND radio_reponse!='NULL'";
		$reponse=mysql_query($sql) or die( "$sql <br/>" .mysql_error());
		$donnees=mysql_fetch_array($reponse);


			$votes_choix =$donnees[nbs];
			$pourcentage=floor($votes_choix*100/$votes_totaux);
			$largeur=3*$pourcentage;
			$reste=(100-$pourcentage)*3;
			$graphique="<img src=\"http://www.ailesse.com/~tykayn/bazar/sondages/carre_rose.PNG\" width=\"$largeur\" height=\"10\"/><img src=\"http://www.ailesse.com/~tykayn/bazar/sondages/carre_violet.PNG\" width=\"$reste\" height=\"10\"/> ";
			echo"<tr><td> $array_choix[$i]  $votes_choix / $votes_totaux ($pourcentage%) </td><td>$graphique </td></tr>";

	}
echo" </table>";




mysql_close();
?>

</html>