cenachero27 de Marzo del 2004
PreguntaVeran, todo funciona bien con la unica excepción de que en el archivo encuesta.txt no de modifican los valores y por tanto la barra que muestra los datos o resultados de la encuesta no se mueve en absoluto. ¿Alguien puede ayudarme?
<?
//cargo el numero de respuestas de un fichero de texto
$f=fopen("encuesta.txt", "r");
$todas=fread($f,filesize("encuesta.txt"));
$respuestas=explode(",",$todas);
fclose($f);
if ($respuesta) {
//incremento la respuesta y la guardo en el fichero
$respuestas['respuesta']++;
$f=fopen("encuesta.txt","w");
$todas=$respuestas[0].",".$respuestas[1].",".$respuestas[2];
fwrite($f,$todas,strlen($todas));
fclose($f);
}
$total=$respuestas[0]+$respuestas[1]+$respuestas[2];
if($total==0) {
$porcentaje[0]=0;
$porcentaje[1]=0;
$porcentaje[2]=0;
} else {
$porcentaje[0]=sprintf("%1.2f",(($respuestas[0]/$total)*100));
$porcentaje[1]=sprintf("%1.2f",(($respuestas[1]/$total)*100));
$porcentaje[2]=sprintf("%1.2f",(($respuestas[2]/$total)*100));
}
?>
<h3>Encuesta:</h3>
<table border="1">
<tr>
<td colspan="3" align="center">
<h4>¿Te gusta el nombre "Qulumbayra"<br>
para el periódico del cole? </h4>
</td>
</tr>
<tr>
<td align="left">
<? echo "$porcentaje[0]%"; ?>
</td>
<td align="left">
<img src="red.png" height="15" width="<? echo "$porcentaje[0]"; ?>">
</td>
<td>
Si
</td>
</tr>
<tr>
<td align="left">
<? echo "$porcentaje[1]%"; ?>
</td>
<td align="left">
<img src="blue.png" height="15" width="<?php echo "$porcentaje[1]"; ?>">
</td>
<td>
No
</td>
</tr>
<tr>
<td align="left">
<? echo "$porcentaje[2]%"; ?>
</td>
<td align="left">
<img src="yellow.png" height="15" width="<?php echo "$porcentaje[2]"; ?>">
</td>
<td>
Me da igual
</td>
</tr>
</table>
<br><br><br>
<h3>¿Cuál es tu respuesta?</h3>
<form name="encuesta" action="encuesta.php" method="post"">
<input type=radio name="respuesta" Value="1"> Si <br>
<input type=radio name="respuesta" Value="2"> No <br>
<input type=radio name="respuesta" Value="3"> Me da igual <br><br>
<input type="submit" value="Votar">
</form>
<?
echo $HTTP_POST_VARS['respuesta'],"<br>";
?>
<?
//cargo el numero de respuestas de un fichero de texto
$f=fopen("encuesta.txt", "r");
$todas=fread($f,filesize("encuesta.txt"));
$respuestas=explode(",",$todas);
fclose($f);
if ($respuesta) {
//incremento la respuesta y la guardo en el fichero
$respuestas['respuesta']++;
$f=fopen("encuesta.txt","w");
$todas=$respuestas[0].",".$respuestas[1].",".$respuestas[2];
fwrite($f,$todas,strlen($todas));
fclose($f);
}
$total=$respuestas[0]+$respuestas[1]+$respuestas[2];
if($total==0) {
$porcentaje[0]=0;
$porcentaje[1]=0;
$porcentaje[2]=0;
} else {
$porcentaje[0]=sprintf("%1.2f",(($respuestas[0]/$total)*100));
$porcentaje[1]=sprintf("%1.2f",(($respuestas[1]/$total)*100));
$porcentaje[2]=sprintf("%1.2f",(($respuestas[2]/$total)*100));
}
?>
<h3>Encuesta:</h3>
<table border="1">
<tr>
<td colspan="3" align="center">
<h4>¿Te gusta el nombre "Qulumbayra"<br>
para el periódico del cole? </h4>
</td>
</tr>
<tr>
<td align="left">
<? echo "$porcentaje[0]%"; ?>
</td>
<td align="left">
<img src="red.png" height="15" width="<? echo "$porcentaje[0]"; ?>">
</td>
<td>
Si
</td>
</tr>
<tr>
<td align="left">
<? echo "$porcentaje[1]%"; ?>
</td>
<td align="left">
<img src="blue.png" height="15" width="<?php echo "$porcentaje[1]"; ?>">
</td>
<td>
No
</td>
</tr>
<tr>
<td align="left">
<? echo "$porcentaje[2]%"; ?>
</td>
<td align="left">
<img src="yellow.png" height="15" width="<?php echo "$porcentaje[2]"; ?>">
</td>
<td>
Me da igual
</td>
</tr>
</table>
<br><br><br>
<h3>¿Cuál es tu respuesta?</h3>
<form name="encuesta" action="encuesta.php" method="post"">
<input type=radio name="respuesta" Value="1"> Si <br>
<input type=radio name="respuesta" Value="2"> No <br>
<input type=radio name="respuesta" Value="3"> Me da igual <br><br>
<input type="submit" value="Votar">
</form>
<?
echo $HTTP_POST_VARS['respuesta'],"<br>";
?>
