Sondage PHP aide
Posté : 27 avr. 2019, 15:21
Bonjour,
j'ai un problème dans le fonctionnement de mon sondage. Je pense que le problème viens de l'écriture masi je bloque dessus. Pouvez vous m'aider.
fichier : sondage.txt
1,1
sondage.html
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" media="screen" href="mon_style.css"/>
<meta name="author" content="Lucas Jurain et Nathan Minon"/>
<link rel="icon" type="image/ico" href="favicon.ico"/>
<title>La Securite Routiere</title>
</head>
<body>
<section>
<p>Avez vous le permis de conduire ? </p>
<form action="sondage.php" method="post">
<input type="radio" name="reponse" value="0" checked="checked"/>Oui<br/>
<input type="radio" name="reponse" value="1"/>Non<br/>
<input type="submit" value="Voter" name="envoi"/>
</form>
<a href="exploitation.php">Voir les resultats</a>
</section>
</body>
</html>
sondage.php
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" media="screen" href="mon_style.css"/>
<meta name="author" content="Lucas Jurain et Nathan Minon"/>
<link rel="icon" type="image/ico" href="favicon.ico"/>
<title>La Securite Routiere</title>
</head>
<body>
<header>
<h1><em>La Securite Routiere</em></h1>
<img src="https://yt3.ggpht.com/a-/AAuE7mDL-G4oaS ... ff-rj-k-no" width="200" />
</header>
<section>
<?php
// C'est vide si l'internaute n'a pas voté.
if (empty($_POST["envoi"]) && empty($vote) && empty($_POST["reponse"])){
echo "<input type='submit' name='envoi' value='Voter'/>";
}
else {
echo "Merci pour votre vote.";
if (isset($_POST["envoi"]) && isset($_POST["reponse"]) && empty($vote)){
$rep=$_POST["reponse"];
$fichier="sondage.txt";
// Ouverture en mode lecture-écriture
$pointeur=fopen($fichier,"r+");
$sondage=fread($pointeur,filesize($fichier));
$sondage=explode(",",$sondage);
// réponse contient 0 ou 1
$sondage[$rep]++;
//reconstruction de la chaîne
$sondage=$sondage[0].",".$sondage[1];
rewind($pointeur);
fputs($pointeur,$sondage);
fclose($pointeur);
}
}
?>
</form>
<a href="exploitation.php">Voir les résultats</a>
</section>
</body>
</html>
exploitation.php
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" media="screen" href="mon_style.css"/>
<meta name="author" content="Lucas Jurain et Nathan Minon"/>
<link rel="icon" type="image/ico" href="favicon.ico"/>
<title>La Securite Routiere</title>
</head>
<body>
<header>
<h1><em>La Securite Routiere</em></h1>
<img src="https://yt3.ggpht.com/a-/AAuE7mDL-G4oaS ... ff-rj-k-no" width="200" />
</header>
<section>
<?php
$fichier="sondage.txt";
//Lecture seule
$pointeur=fopen($fichier,"r");
$sondage=fread($pointeur,filesize($fichier));
fclose($pointeur);
$sondage=explode(",",$sondage);
$sondage=file($fichier);
//total des votes
$total=$sondage[0]+$sondage[1];
//longueur max de la barre du "graphique"
$longueur=100;
//partie OUI
$oui=$sondage[0]*$longueur/$total;
$non=$sondage[1]*$longueur/$total;
// Nous allons nous servir d'un tableau pour afficher les barres du "graphique"
// Il faut donc arrondir le résultat pour l'attribut width
$oui=round($oui);
$non=round($non);
echo "oui : $oui non: $non total : $total";
?>
<!-- bordure à zéro et un TD pour espacer sinon c'est pas beau //-->
<table border="0"><tr>
<!-- Il faut un espace pour que la longueur s'adapte et qu'on voie le résultat //-->
<td>Oui </td><td> </td><td width="<?php echo $oui; ?>" bgcolor="red"> </td>
</table>
<table border="0"><tr>
<td>Non</td><td> </td><td width="<?php echo $non; ?>" bgcolor="blue"> </td>
</table>
</section>
</body>
</html>
Merci beaucoup de votre aide.
j'ai un problème dans le fonctionnement de mon sondage. Je pense que le problème viens de l'écriture masi je bloque dessus. Pouvez vous m'aider.
fichier : sondage.txt
1,1
sondage.html
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" media="screen" href="mon_style.css"/>
<meta name="author" content="Lucas Jurain et Nathan Minon"/>
<link rel="icon" type="image/ico" href="favicon.ico"/>
<title>La Securite Routiere</title>
</head>
<body>
<section>
<p>Avez vous le permis de conduire ? </p>
<form action="sondage.php" method="post">
<input type="radio" name="reponse" value="0" checked="checked"/>Oui<br/>
<input type="radio" name="reponse" value="1"/>Non<br/>
<input type="submit" value="Voter" name="envoi"/>
</form>
<a href="exploitation.php">Voir les resultats</a>
</section>
</body>
</html>
sondage.php
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" media="screen" href="mon_style.css"/>
<meta name="author" content="Lucas Jurain et Nathan Minon"/>
<link rel="icon" type="image/ico" href="favicon.ico"/>
<title>La Securite Routiere</title>
</head>
<body>
<header>
<h1><em>La Securite Routiere</em></h1>
<img src="https://yt3.ggpht.com/a-/AAuE7mDL-G4oaS ... ff-rj-k-no" width="200" />
</header>
<section>
<?php
// C'est vide si l'internaute n'a pas voté.
if (empty($_POST["envoi"]) && empty($vote) && empty($_POST["reponse"])){
echo "<input type='submit' name='envoi' value='Voter'/>";
}
else {
echo "Merci pour votre vote.";
if (isset($_POST["envoi"]) && isset($_POST["reponse"]) && empty($vote)){
$rep=$_POST["reponse"];
$fichier="sondage.txt";
// Ouverture en mode lecture-écriture
$pointeur=fopen($fichier,"r+");
$sondage=fread($pointeur,filesize($fichier));
$sondage=explode(",",$sondage);
// réponse contient 0 ou 1
$sondage[$rep]++;
//reconstruction de la chaîne
$sondage=$sondage[0].",".$sondage[1];
rewind($pointeur);
fputs($pointeur,$sondage);
fclose($pointeur);
}
}
?>
</form>
<a href="exploitation.php">Voir les résultats</a>
</section>
</body>
</html>
exploitation.php
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" media="screen" href="mon_style.css"/>
<meta name="author" content="Lucas Jurain et Nathan Minon"/>
<link rel="icon" type="image/ico" href="favicon.ico"/>
<title>La Securite Routiere</title>
</head>
<body>
<header>
<h1><em>La Securite Routiere</em></h1>
<img src="https://yt3.ggpht.com/a-/AAuE7mDL-G4oaS ... ff-rj-k-no" width="200" />
</header>
<section>
<?php
$fichier="sondage.txt";
//Lecture seule
$pointeur=fopen($fichier,"r");
$sondage=fread($pointeur,filesize($fichier));
fclose($pointeur);
$sondage=explode(",",$sondage);
$sondage=file($fichier);
//total des votes
$total=$sondage[0]+$sondage[1];
//longueur max de la barre du "graphique"
$longueur=100;
//partie OUI
$oui=$sondage[0]*$longueur/$total;
$non=$sondage[1]*$longueur/$total;
// Nous allons nous servir d'un tableau pour afficher les barres du "graphique"
// Il faut donc arrondir le résultat pour l'attribut width
$oui=round($oui);
$non=round($non);
echo "oui : $oui non: $non total : $total";
?>
<!-- bordure à zéro et un TD pour espacer sinon c'est pas beau //-->
<table border="0"><tr>
<!-- Il faut un espace pour que la longueur s'adapte et qu'on voie le résultat //-->
<td>Oui </td><td> </td><td width="<?php echo $oui; ?>" bgcolor="red"> </td>
</table>
<table border="0"><tr>
<td>Non</td><td> </td><td width="<?php echo $non; ?>" bgcolor="blue"> </td>
</table>
</section>
</body>
</html>
Merci beaucoup de votre aide.