par
Cyrano » 06 oct. 2005, 09:03
Il y a sûrement une erreur de recopie. Voici ce que j'ai fait pour tester de mon coté en reprenant ton code, le voilà corrigé et fonctionnel:
<?
// ---------------------------------------------------------
// - récupére les variables qui vont permettre l'affichage de
// - la hauteur des colonnes oui et non (affichage graphique)
// ---------------------------------------------------------
$vote = isset($_POST['vote']) ? $_POST['vote'] : "";
$fichier_oui = "./oui.txt";
$fichier_non = "./non.txt";
$fichier_ok = "./ok.txt";
if($vote == 1)
{
$fp = (file_exists($fichier_oui)) ? fopen($fichier_oui,"r+") : fopen($fichier_oui, "w");
$oui=fgets($fp,10);
if($oui != "")
{
$oui++;
}
else
{
$oui = 1;;
}
fseek($fp,0);
fwrite($fp,$oui);
fclose($fp);
}
if ($vote == 2)
{
$fp = (file_exists($fichier_non)) ? fopen($fichier_non,"r+") : fopen($fichier_non, "w");
$non=fgets($fp,10);
if($non != "")
{
$non++;
}
else
{
$non = 1;;
}
fseek($fp,0);
fwrite($fp,$non);
fclose($fp);
}
if ($vote == 3)
{
$fp = (file_exists($fichier_ok)) ? fopen($fichier_ok,"r+") : fopen($fichier_ok, "w");
$ok=fgets($fp,10);
if($ok != "")
{
$ok++;
}
else
{
$ok = 1;;
}
fseek($fp,0);
fwrite($fp,$ok);
fclose($fp);
}
if(file_exists($fichier_oui))
{
$fp=fopen($fichier_oui,"r+");
$oui=fgets($fp,10);
fclose($fp);
}
else
{
$oui = 0;
}
if(file_exists($fichier_non))
{
$fp=fopen($fichier_non,"r+");
$non=fgets($fp,10);
fclose($fp);
}
else
{
$non = 0;
}
if(file_exists($fichier_ok))
{
$fp=fopen($fichier_ok,"r+");
$ok=fgets($fp,10);
fclose($fp);
}
else
{
$ok = 0;
}
$oui = $oui == "" ? 0 : $oui;
$non = $non == "" ? 0 : $non;
$ok = $ok == "" ? 0 : $ok;
$total = (($oui + $non + $ok) != 0) ? 100/($oui + $non + $ok) : 0;
echo("<p>".$oui .", ".$non.", ".$ok .", total = ". $total ."</p>\n");
$oui = $oui * $total;
$non = $non * $total;
$ok = $ok * $total;
// ----------------------------------------------------------
// - Une fois les variables stockées je peux les rajouter
// - ds les attributs height de mes deux images oui et non
// ----------------------------------------------------------
?>
<table border="0" cellspacing=0 cellpadding=0 align="center">
<tr>
<td>
<img src="images/graph_gh.gif" width="30" height="98"><img src="images/space.gif" width="15" height="10"><img src="images/oui.gif" width="10" height="<? echo $oui; ?>"><img src="images/space.gif" width="15" height="10"><img src="images/non.gif" width="10" height="<? echo $non; ?>"><img src="images/space.gif" width="15" height="10"><img src="images/ok.gif" width="10" height="<? echo $ok; ?>"></td></tr><tr><td><img src="images/graph_bs.gif" width="125" height="12" border=0>
</td>
</tr>
</table>
<br>
<form method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>">
<div align="center">
<font color="#FF9933"><b>
<input type="radio" name="vote" value="1" > oui
<input type="radio" name="vote" value="2"> non
<input type="radio" name="vote" value="3"> ok
</b></font>
<br><br>
<input type="image" src="images/voter.gif" name="submit" value="Envoyer">
</div>
</form>
</td>
</tr>
</table>
<?
// ----------------------------------------------------------
// - Récupérer les Variables pour l'affichage en ASCii
// ----------------------------------------------------------
if(file_exists($fichier_oui))
{
$fp=fopen($fichier_oui,"r");
$oui=fgets($fp,10);
$pour = $oui ;
fclose($fp);
}
else
{
$pour = 0;
}
if(file_exists($fichier_non))
{
$fp=fopen($fichier_non,"r");
$non=fgets($fp,10);
$contre = $non ;
fclose($fp);
}
else
{
$contre = 0;
}
if(file_exists($fichier_ok))
{
$fp=fopen($fichier_ok,"r");
$ok=fgets($fp,10);
$neutre = $ok ;
fclose($fp);
}
else
{
$neutre = 0;
}
$total = (($oui + $non + $ok) != 0) ? 100/($oui + $non + $ok) : 0;
$oui = $oui * $total;
$non = $non * $total;
$ok = $ok * $total;
// ----------------------------------------------------------
// - Affichage des résultats en TXT et en %
// - la fonction intval(); permet de ne pas avoir d'arrondir
// - le résultat et de ne pas avoir 10 chiffres après la virgule
// ----------------------------------------------------------
?>
<center>
<font color="#FF9933"><b>
OUI [<span class=""><? print(intval($oui));?>%</span>]
NON [<span class=""><? print(intval($non));?>%</span>]
OK [<span class=""><? print(intval($ok));?>%</span>]<br>
VOTANTS[<span class=""><? print($pour + $contre + $neutre);?></span>]<br><br>
</b>
</font>
</center>
Comme tu vois, il est un peu rallongé et il y a des tests en plus ainsi que des changements. Par exemple, je vérifie si le fichier existe au départ lors du premier vote, parce que ouvrir un fichier inexistant en mode "r+", ça génère une erreur, donc s'il n'existe pas, je l'ouvre en mode "w".
Enfin regarde en fin de script, j'ai simplifié l'affichage et il reflète quelque chose de juste et tu ne pourras jamais à voir un vote supérieur à 100 à 1 ou 2 près à cause du inval qui arrondit à un entier. TU aurais pu mettre un nombre en virgule flottante avec deux décimales en utilisant
number_format() comme ceci par exemple :
OUI [<span class=""><? print(number_format($oui,2));?>%</span>]
NON [<span class=""><? print(number_format($non,2));?>%</span>]
OK [<span class=""><? print(number_format($ok, 2));?>%</span>]<br>
Ce qui donnerait à l'affichage:
OUI [50.00%] NON [37.50%] OK [12.50%]
VOTANTS[7]
Il y a sûrement une erreur de recopie. Voici ce que j'ai fait pour tester de mon coté en reprenant ton code, le voilà corrigé et fonctionnel:
[php]<?
// ---------------------------------------------------------
// - récupére les variables qui vont permettre l'affichage de
// - la hauteur des colonnes oui et non (affichage graphique)
// ---------------------------------------------------------
$vote = isset($_POST['vote']) ? $_POST['vote'] : "";
$fichier_oui = "./oui.txt";
$fichier_non = "./non.txt";
$fichier_ok = "./ok.txt";
if($vote == 1)
{
$fp = (file_exists($fichier_oui)) ? fopen($fichier_oui,"r+") : fopen($fichier_oui, "w");
$oui=fgets($fp,10);
if($oui != "")
{
$oui++;
}
else
{
$oui = 1;;
}
fseek($fp,0);
fwrite($fp,$oui);
fclose($fp);
}
if ($vote == 2)
{
$fp = (file_exists($fichier_non)) ? fopen($fichier_non,"r+") : fopen($fichier_non, "w");
$non=fgets($fp,10);
if($non != "")
{
$non++;
}
else
{
$non = 1;;
}
fseek($fp,0);
fwrite($fp,$non);
fclose($fp);
}
if ($vote == 3)
{
$fp = (file_exists($fichier_ok)) ? fopen($fichier_ok,"r+") : fopen($fichier_ok, "w");
$ok=fgets($fp,10);
if($ok != "")
{
$ok++;
}
else
{
$ok = 1;;
}
fseek($fp,0);
fwrite($fp,$ok);
fclose($fp);
}
if(file_exists($fichier_oui))
{
$fp=fopen($fichier_oui,"r+");
$oui=fgets($fp,10);
fclose($fp);
}
else
{
$oui = 0;
}
if(file_exists($fichier_non))
{
$fp=fopen($fichier_non,"r+");
$non=fgets($fp,10);
fclose($fp);
}
else
{
$non = 0;
}
if(file_exists($fichier_ok))
{
$fp=fopen($fichier_ok,"r+");
$ok=fgets($fp,10);
fclose($fp);
}
else
{
$ok = 0;
}
$oui = $oui == "" ? 0 : $oui;
$non = $non == "" ? 0 : $non;
$ok = $ok == "" ? 0 : $ok;
$total = (($oui + $non + $ok) != 0) ? 100/($oui + $non + $ok) : 0;
echo("<p>".$oui .", ".$non.", ".$ok .", total = ". $total ."</p>\n");
$oui = $oui * $total;
$non = $non * $total;
$ok = $ok * $total;
// ----------------------------------------------------------
// - Une fois les variables stockées je peux les rajouter
// - ds les attributs height de mes deux images oui et non
// ----------------------------------------------------------
?>
<table border="0" cellspacing=0 cellpadding=0 align="center">
<tr>
<td>
<img src="images/graph_gh.gif" width="30" height="98"><img src="images/space.gif" width="15" height="10"><img src="images/oui.gif" width="10" height="<? echo $oui; ?>"><img src="images/space.gif" width="15" height="10"><img src="images/non.gif" width="10" height="<? echo $non; ?>"><img src="images/space.gif" width="15" height="10"><img src="images/ok.gif" width="10" height="<? echo $ok; ?>"></td></tr><tr><td><img src="images/graph_bs.gif" width="125" height="12" border=0>
</td>
</tr>
</table>
<br>
<form method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>">
<div align="center">
<font color="#FF9933"><b>
<input type="radio" name="vote" value="1" > oui
<input type="radio" name="vote" value="2"> non
<input type="radio" name="vote" value="3"> ok
</b></font>
<br><br>
<input type="image" src="images/voter.gif" name="submit" value="Envoyer">
</div>
</form>
</td>
</tr>
</table>
<?
// ----------------------------------------------------------
// - Récupérer les Variables pour l'affichage en ASCii
// ----------------------------------------------------------
if(file_exists($fichier_oui))
{
$fp=fopen($fichier_oui,"r");
$oui=fgets($fp,10);
$pour = $oui ;
fclose($fp);
}
else
{
$pour = 0;
}
if(file_exists($fichier_non))
{
$fp=fopen($fichier_non,"r");
$non=fgets($fp,10);
$contre = $non ;
fclose($fp);
}
else
{
$contre = 0;
}
if(file_exists($fichier_ok))
{
$fp=fopen($fichier_ok,"r");
$ok=fgets($fp,10);
$neutre = $ok ;
fclose($fp);
}
else
{
$neutre = 0;
}
$total = (($oui + $non + $ok) != 0) ? 100/($oui + $non + $ok) : 0;
$oui = $oui * $total;
$non = $non * $total;
$ok = $ok * $total;
// ----------------------------------------------------------
// - Affichage des résultats en TXT et en %
// - la fonction intval(); permet de ne pas avoir d'arrondir
// - le résultat et de ne pas avoir 10 chiffres après la virgule
// ----------------------------------------------------------
?>
<center>
<font color="#FF9933"><b>
OUI [<span class=""><? print(intval($oui));?>%</span>]
NON [<span class=""><? print(intval($non));?>%</span>]
OK [<span class=""><? print(intval($ok));?>%</span>]<br>
VOTANTS[<span class=""><? print($pour + $contre + $neutre);?></span>]<br><br>
</b>
</font>
</center>[/php]
Comme tu vois, il est un peu rallongé et il y a des tests en plus ainsi que des changements. Par exemple, je vérifie si le fichier existe au départ lors du premier vote, parce que ouvrir un fichier inexistant en mode "r+", ça génère une erreur, donc s'il n'existe pas, je l'ouvre en mode "w".
Enfin regarde en fin de script, j'ai simplifié l'affichage et il reflète quelque chose de juste et tu ne pourras jamais à voir un vote supérieur à 100 à 1 ou 2 près à cause du inval qui arrondit à un entier. TU aurais pu mettre un nombre en virgule flottante avec deux décimales en utilisant [url=http://www.php.net/manual/fr/function.number-format.php]number_format()[/url] comme ceci par exemple :
[php]OUI [<span class=""><? print(number_format($oui,2));?>%</span>]
NON [<span class=""><? print(number_format($non,2));?>%</span>]
OK [<span class=""><? print(number_format($ok, 2));?>%</span>]<br>[/php]
Ce qui donnerait à l'affichage:
[quote][b]OUI [50.00%] NON [37.50%] OK [12.50%]
VOTANTS[7][/b][/quote]