1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
<form action="<?php echo $PHP_SELF ?>" method="post" >
<fieldset>
<legend><b>Votez pour votre joueur préféré! </b></legend>
<p>
<?php
$joueurs=array("anelk"=>"Anelka","gourc"=>"Gourcuff","riber"=>"Ribéry");
?>
Anelka<input type="radio" name="vote" value="anelk" /> <br />
Gourcuff<input type="radio" name="vote" value="gourc" /> <br />
Ribéry<input type="radio" name="vote" value="riber" /> <br />
<input type="submit" value="Voter" />
<input type="submit" value="Afficher les résultats" name="affiche" />
</p>
</fieldset>
</form>
<?php
if(isset($_POST["vote"]))
{
$vote=$_POST["vote"];
echo "<h2> Merci de votre vote pour ".$joueurs[$vote] ."</h2> ";
if(file_exists("votes.txt") )
{
if($id_file=fopen("votes.txt","a"))
{
flock($id_file,2);
fwrite($id_file,$vote."\n");
flock($id_file,3);
fclose($id_file);
}
else
{ echo "Fichier inaccessible";
}
}
else
{
$id_file=fopen("votes.txt","w");
fwrite($id_file,$vote."\n");
fclose($id_file);
}
}
else
{ echo "<h2>Complétez le formulaire puis cliquez sur 'Voter' ! </h2> ";}
//Initialisation du tableau des résultats
$result=array("Anelka"=>0,"Gourcuff"=>0,"Ribéry"=>0);
//Affichage des résultats
if(isset($_POST["affiche"]))
{
if($id_file=fopen("votes.txt","r"))
{
while($ligne=fread($id_file,6) )
{
switch($ligne)
{
case "anelk\n":
$result["Anelka"]++;
break;
case "gourc\n":
$result["Gourcuff"]++;
break;
case "riber\n":
$result["Ribéry"]++;
break;
default:
break;
}
}
fclose($id_file);
}
$total= ($result["Anelka"] + $result["Gourcuff"]+ $result["Ribéry"])/100;
$tri=$result;
arsort($tri);
echo "<div style=\"border-style:double\" >";
echo "<h3> Les résultats du vote </h3>";
foreach($tri as $nom=>$score)
{
$i++;
echo "<h4>$i<sup>e</sup> : ", $nom," a $score voix soit ", number_format($score/$total,2),"%</h4>";
}
echo "</div>";
}
?> |