Codigo de las funciones:
Código:
El txt:<?php //BORRAR COOKIE function borrar(){ unset($_COOKIE['encuesta']); setcookie("encuesta","",time()-3600,"/"); header('Location: index.php'); } //OBTENER LETRAS function letras(){ $i=0; $separadas = array(); $letras = ""; $letra = array(); $file = fopen("encuesta.txt", "r"); while(!feof($file)) { $letras=fgets($file); if($letras != ""){ $separadas = explode("=",$letras); $letra[$i] = $separadas[0]; $i++; } } fclose($file); return $letra; } //OBTENER NUMEROS function numeros(){ $i=0; $separadas = array(); $numeros = ""; $numero = array(); $file = fopen("encuesta.txt", "r"); while(!feof($file)) { $numeros=fgets($file); $separadas = explode("=",$numeros); $numero[$i] = $separadas[1]; $i++; } fclose($file); return $numero; } //CREAR COOKIE Y ESCRIBIR EN TXT function encuesta(){ //comprueba si hay uno radio seleccionado if (isset($_POST['letra'])){ $letras = array(); $letras = letras(); $numeros = array(); $numeros = numeros(); $fp = fopen('encuesta.txt', 'w+'); $l = count($letras); for ($i = 0;$i < $l;$i++){ if($letras[$i] == $_POST['letra']){ $numeros[$i] += 1; } fwrite($fp,"\r\n"); fwrite($fp, $letras[$i]."=".$numeros[$i]); } fclose($fp); //CREAR COOKIE ENCUESTA setcookie("encuesta", $_POST['letra'],time()+3600,"/"); } header('Location: index.php'); } //INVOCAR FUNCION ENCUESTA if(isset($_POST['letra'])){ encuesta(); } ?>
Código:
El codigo del index:a=0 b=0 c=0
Código:
<?php include("funciones.php"); $letras = array(); $letras = letras(); $numeros = array(); $numeros = numeros(); //Comprobar si se ha votado if(isset($_COOKIE["encuesta"])){ //VER VOTACIONES //$_session["cuadro"]=1; $voto = $_COOKIE["encuesta"]; echo "Gracias por votar a <bold>$voto</bold> <br/>"; echo "Borra la cookie <a onclick='<?php borrar() ?>' href=''>aqui</a><br/>"; for ($i = 0;$i <count($letras);$i++){ if ($letras[$i] != "") echo "<bold>$letras[$i]</bold> <img background_color='yellow' width='$numeros[$i]' height='20'><br/>"; } }else{ //IMPRIMIR TEST PRINCIPAL echo "<form action='funciones.php' method='POST'>"; echo "Que letra prefieres?<br/>"; for ($i = 0;$i <count($letras);$i++){ if ($letras[$i] != "") echo "<input type='radio' name='letra' value='$letras[$i]' />$letras[$i]<br/><br/>"; } echo "<input type='submit' value='Votar'/>"; echo "</form>"; } ?>