Problème d'incrémentation !

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : Problème d'incrémentation !

par Invité » 19 nov. 2007, 21:06

Merci à tous !
j'ai règler le prblème en remplaçant GET par POST et en faisant suivre les varriables qui allaient avec ^_^

:)

Code finale :
function sondage ($titre, $question, $reponse1, $reponse2, $reponse3, $reponse4, $nb_max_votes)	{	
			// INFORMATIONS SUR LE SONDAGE
			// $titre = 'sondage'; // = nom du fichier dans lequel est stocké les votes
			// $question = 'Comment trouvez-vous ce site ?';
			 $reponse[1] = $reponse1;
			 $reponse[2] = $reponse2;
			 $reponse[3] = $reponse3;
			 $reponse[4] = $reponse4;
			// $nb_max_votes = 0; // 0 si illimité
			// MODIFICATION DU SONDAGE
			$choix = "4";
			if (isset($_POST['vote']))
			{
			   if(empty($_COOKIE['sondage'.$titre.'']) && !isset($_COOKIE['sondage'.$titre.''])){
				$resultats = fopen("sondage".$titre.".txt", "r+") or die('Erreur d\'ouverture de fichier');
				$vote = $_POST['vote'];
				for ($numero = 1; $numero <= $choix; $numero ++)
				{
					$ligne[$numero] = (int) fgets($resultats);
					if ($numero == $vote)
					{
						$ligne[$numero] ++;
					}
					if (isset($donnees_votes))
					{
						$donnees_votes = $donnees_votes . "\n" . $ligne[$numero];
						$nb_votes += $ligne[$numero]; // comptage du nombre de votes
					}
					else
					{
						$donnees_votes = $ligne[$numero];
						$nb_votes = (int) $ligne[$numero];
					}
				}	
				fseek ($resultats, 0);
				if($nb_votes<=$nb_max_votes OR $nb_max_votes==0)
				  fputs ($resultats, $donnees_votes);	// écriture des données

				fclose($resultats);
			   }
			}
			// LECTURE DU SONDAGE		
			$resultats = fopen("sondage".$titre.".txt", "r") or die('Erreur d\'ouverture de fichier');
			
			$numero = 1;
			while ($numero <= $choix)	// attribution d'un nombre pour chaque vote à l'array $resultat[]
			{
				$resultat[$numero] = fgets($resultats);
				if ($resultat[$numero] == NULL) // on remplace les lignes vides du fich txt par 0
				{
						$resultat[$numero] = 0;
				}
			$numero ++;
			}

			$total_votes = 0; // calcul du total des votes
			foreach($resultat as $nb_resultat) $total_votes += $nb_resultat;
			if ($total_votes == 0)	// éviter la division par 0
			{
				$total_votes = 1;
			}
			$numero = 1;
			while ($numero <= $choix) // transformation du nombre de vote en pourcentages
			{
				$pourcentage[$numero] = $resultat[$numero] / $total_votes * 100;
				$numero ++;
			}
			
			$long_max_bloc = 200; // longueur maximale du curseur pour un vote en pixels

			// affichage des barres et du nombre de votes
			$numero = 1;
			   if(empty($_COOKIE['sondage'.$titre.'']) && !isset($_COOKIE['sondage'.$titre.''])){
			   			echo ('<div class="sondaffeu"><div class="titresondage" align="center">'. $question . '</div><br /><form action="http://'.$_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI'].'"  method="POST"><input name="titresondage" type="hidden" value="sondage'.$titre.'" /> ');

			while ($numero <= $choix) 
			{
						if($resultat[$numero]>1){ $votess="s"; } else { $votess=""; }
				echo ('<div class="choix"><label>&nbsp;&nbsp; <input class="textInput" type="radio" name="vote" value="' . $numero . '" />'. $reponse[$numero] .'</label>
        <br />&nbsp;&nbsp; <img src="../images/01.png" height="12" width="3"/><img src="../images/02.png" height="12" width="' . $pourcentage[$numero] / 100 * $long_max_bloc . '" alt="'.round($pourcentage[$numero]).'%" /><img src="../images/03.png" height="12" width="3"/><br />
        <span style="font-size:80%;"><strong>&nbsp;&nbsp; ' . $resultat[$numero] . ' vote'.$votess.' ('.round($pourcentage[$numero]).'%)</strong></span></div><br>');

//				echo ($pourcentage[$numero] . '%'); 
				$numero ++;
			}
						echo ('<div align="center"><input class="textInput" type="submit" value="Voter"></div>');

			} else {
			echo ('<div class="sondaffeu"><div align="left"><a href="javascript:affmas();"><img id="tap" name="tap" src="../images/downs.gif" align="absmiddle"/></a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="javascript:affmas();">'. $question . '</a></div><div id="choix" style="display: none;"><br /><form action="http://'.$_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI'].'">');

						while ($numero <= $choix) 
			{
			if($resultat[$numero]>1){ $votess="s"; } else { $votess=""; }
				echo ('<div class="choix"><label>&nbsp;&nbsp; '. $reponse[$numero] .'</label>
        <br />&nbsp;&nbsp; <img src="../images/01.png" height="12" width="3"/><img src="../images/02.png" height="12" width="' . $pourcentage[$numero] / 100 * $long_max_bloc . '" alt="'.round($pourcentage[$numero]).'%" /><img src="../images/03.png" height="12" width="3"/><br />
        <span style="font-size:80%;"><strong>&nbsp;&nbsp; ' . $resultat[$numero] . ' vote'.$votess.' ('.round($pourcentage[$numero]).'%)</strong></span></div><br>');

//				echo ($pourcentage[$numero] . '%'); 
				$numero ++;
			}
 }
			echo ('</form></div></div>');
			fclose($resultats);
///== Par Yvan0|_| ==///
}

par Hubert Roksor » 19 nov. 2007, 10:41

J'ai un peu survolé le script (c'est d'ailleurs pour cela qu'on demande de ne poster qu'une vingtaine de lignes, pour motiver les gens à les lire attentivement) et j'ai remarqué quelques trucs étranges comme:
  • fermer et réouvrir le fichier (pourquoi faire :?:)
  • $$titre
  • des opérateurs de suppression de message d'erreur @, ce qui n'est pas du tout conseillé, particulièrement quand ça marche pas (sans message d'erreur il faut faire du vaudou pour débugger)
Si tu es sous PHP5, remplace fopen() et compagnie par file_get_contents() / file_put_contents()

par BioSs » 19 nov. 2007, 10:31

Salut,
Ce script est un système de sondage n'utilisant pas MySQL.
Toutes les données se font dans un fichier texte se nommant sondage-$IDDUSONDAGE.
Le système lit très bien le fichier texte puisque il affiche les résultats initiaux ou modifié à la main.
Mais l'incrémentation automatique de ce fichier ne se fait pas.
C'est là qu'est le problème...
Je ne trouve aucune solution...
Auriez-vous des lumièers pour me guider ? :idea: :idea: :idea: :idea:

par Truc » 18 nov. 2007, 18:51

Si tu pouvais expliquer un peu plus l'idée de ton script. :roll:

Problème d'incrémentation !

par BioSs » 18 nov. 2007, 17:55

Salut à tous,
J'ai un problème dans mon script mais je ne trouve pas la solution...
Pour moi tout est correct mais apparement non :)
function sondage ($titre, $question, $reponse1, $reponse2, $reponse3, $reponse4, $nb_max_votes)	{	
			// INFORMATIONS SUR LE SONDAGE
			// $titre = 'sondage'; // = nom du fichier dans lequel est stocké les votes
			// $question = 'Comment trouvez-vous ce site ?';
			 $reponse[1] = $reponse1;
			 $reponse[2] = $reponse2;
			 $reponse[3] = $reponse3;
			 $reponse[4] = $reponse4;
			// $nb_max_votes = 0; // 0 si illimité
			// MODIFICATION DU SONDAGE
			$choix = "4";
			if (isset($_GET['vote']))
			{
			   if(empty($_COOKIE['sondage'.$$titre.'']) && !isset($_COOKIE['sondage'.$$titre.''])){
				$resultats = @fopen("sondage".$titre.".txt", "r+") or die('Erreur d\'ouverture de fichier');
				$vote = $_GET['vote'];
				for ($numero = 1; $numero <= $choix; $numero ++)
				{
					$ligne[$numero] = (int) @fgets($resultats);
					if ($numero == $vote)
					{
						$ligne[$numero] ++;
					}
					if (isset($donnees_votes))
					{
						$donnees_votes = $donnees_votes . "\n" . $ligne[$numero];
						$nb_votes += $ligne[$numero]; // comptage du nombre de votes
					}
					else
					{
						$donnees_votes = $ligne[$numero];
						$nb_votes = (int) $ligne[$numero];
					}
				}	
				fseek ($resultats, 0);
				if($nb_votes<=$nb_max_votes OR $nb_max_votes==0)
				  fputs ($resultats, $donnees_votes);	// écriture des données

				fclose($resultats);
			   }
			}
			// LECTURE DU SONDAGE		
			$resultats = @fopen("sondage".$titre.".txt", "r") or die('Erreur d\'ouverture de fichier');
			
			$numero = 1;
			while ($numero <= $choix)	// attribution d'un nombre pour chaque vote à l'array $resultat[]
			{
				$resultat[$numero] = fgets($resultats);
				if ($resultat[$numero] == NULL) // on remplace les lignes vides du fich txt par 0
				{
						$resultat[$numero] = 0;
				}
			$numero ++;
			}

			$total_votes = 0; // calcul du total des votes
			foreach($resultat as $nb_resultat) $total_votes += $nb_resultat;
			if ($total_votes == 0)	// éviter la division par 0
			{
				$total_votes = 1;
			}
			$numero = 1;
			while ($numero <= $choix) // transformation du nombre de vote en pourcentages
			{
				$pourcentage[$numero] = $resultat[$numero] / $total_votes * 100;
				$numero ++;
			}
			
			$long_max_bloc = 200; // longueur maximale du curseur pour un vote en pixels

			// affichage des barres et du nombre de votes
			$numero = 1;
			   if(empty($_COOKIE['sondage'.$$titre.'']) && !isset($_COOKIE['sondage'.$$titre.''])){
			   			echo ('<div class="sondaffeu"><div class="titresondage" align="center">'. $question . '</div><br /><form action="http://'.$_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI'].'"  method="GET"> ');

			while ($numero <= $choix) 
			{
						if($resultat[$numero]>1){ $votess="s"; } else { $votess=""; }
				echo ('<div class="choix"><label>&nbsp;&nbsp; <input class="textInput" type="radio" name="vote" value="' . $numero . '" />'. $reponse[$numero] .'</label>
        <br />&nbsp;&nbsp; <img src="../images/01.png" height="12" width="3"/><img src="../images/02.png" height="12" width="' . $pourcentage[$numero] / 100 * $long_max_bloc . '" alt="'.round($pourcentage[$numero]).'%" /><img src="../images/03.png" height="12" width="3"/><br />
        <span style="font-size:80%;"><strong>&nbsp;&nbsp; ' . $resultat[$numero] . ' vote'.$votess.' ('.round($pourcentage[$numero]).'%)</strong></span></div><br>');

//				echo ($pourcentage[$numero] . '%'); 
				$numero ++;
			}
						echo ('<div align="center"><input class="textInput" type="submit" value="Voter"></div>');

			} else {
			echo ('<div class="sondaffeu"><div align="left"><a href="javascript:affmas();"><img id="tap" name="tap" src="../images/downs.gif" align="absmiddle"/></a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="javascript:affmas();">'. $question . '</a></div><div id="choix" style="display: none;"><br /><form action="http://'.$_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI'].'">');

						while ($numero <= $choix) 
			{
			if($resultat[$numero]>1){ $votess="s"; } else { $votess=""; }
				echo ('<div class="choix"><label>&nbsp;&nbsp; '. $reponse[$numero] .'</label>
        <br />&nbsp;&nbsp; <img src="../images/01.png" height="12" width="3"/><img src="../images/02.png" height="12" width="' . $pourcentage[$numero] / 100 * $long_max_bloc . '" alt="'.round($pourcentage[$numero]).'%" /><img src="../images/03.png" height="12" width="3"/><br />
        <span style="font-size:80%;"><strong>&nbsp;&nbsp; ' . $resultat[$numero] . ' vote'.$votess.' ('.round($pourcentage[$numero]).'%)</strong></span></div><br>');

//				echo ($pourcentage[$numero] . '%'); 
				$numero ++;
			}
 }
			echo ('</form></div></div>');
			fclose($resultats);
///== Par Yvan0|_| ==///
}
Le problème est que le vote n'est pas pris en charge : on a beau voter l'incrémentaiton du vote ne se fait pas

Quelle est le problème ?