if (trim($NOM_ENCUESTA) == "") {
echo "Error\n"; exit;
}
$VER_RES = $HTTP_POST_VARS["res"];
$VOTAR = $HTTP_POST_VARS["votar"];
$respuesta = $HTTP_POST_VARS["respuesta"];
$nombre_archivo = $HTTP_SERVER_VARS["PHP_SELF"];
$datos = fopen ($NOM_ENCUESTA.".txt", "r") or die("Error");
$pregunta = trim(fgets($datos, 4096));
$opciones_data = trim(fgets($datos, 4096));
$opciones_array = explode("|", $opciones_data);
fclose($datos);
?>
function MostrarFormulario() {
global $opciones_array, $nombre_archivo;
echo "";
}
function GuardarVoto() {
global $respuesta, $NOM_ENCUESTA;
$resul_txt = fopen ($NOM_ENCUESTA."_res.txt", "r+") or die("Error");
$resul_tmp = trim(fgets($resul_txt, 4096));
$resul_todos = explode("|", $resul_tmp);
$resul_todos[$respuesta] = $resul_todos[$respuesta]+1;
$resul_nuevo = implode("|", $resul_todos);
fseek ($resul_txt, 0);
flock ($resul_txt, 2);
fwrite ($resul_txt, $resul_nuevo);
flock ($resul_txt, 3);
fclose ($resul_txt);
}
function MostrarResultado() {
global $opciones_array, $NOM_ENCUESTA;
$resul_txt = fopen ($NOM_ENCUESTA."_res.txt", "r") or die("Error");
$resul_tmp = trim(fgets($resul_txt, 4096));
$resul_todos = explode("|", $resul_tmp);
$resul_total = array_sum($resul_todos);
foreach ($opciones_array as $numero => $opcion) {
if ($resul_total)
$porcentaje = round($resul_todos[$numero] / $resul_total, 3) * 100;
else
$porcentaje = 0;
echo "
\n $opcion: ".$resul_todos[$numero]. " ($porcentaje%)
\n"; echo "
\n"; } echo "
\n
\n"; fclose ($resul_txt); } if ($VOTAR == "Votar" && is_numeric($respuesta)) { GuardarVoto(); MostrarResultado(); } else { if ($VER_RES == "Resultados") { MostrarResultado(); } else { MostrarFormulario(); } } ?>
\n $opcion: ".$resul_todos[$numero]. " ($porcentaje%)
\n"; echo "
\n"; } echo "
\n
Total de votos: $resul_total
\n"; fclose ($resul_txt); } if ($VOTAR == "Votar" && is_numeric($respuesta)) { GuardarVoto(); MostrarResultado(); } else { if ($VER_RES == "Resultados") { MostrarResultado(); } else { MostrarFormulario(); } } ?>
1


