PHP - error en la cuenta

 
Vista:
Imágen de perfil de Charly
Val: 182
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

error en la cuenta

Publicado por Charly (69 intervenciones) el 21/10/2018 16:26:47
Estoy creando una encuesta y a la hora de sumar los votos totales que han realizado me da error.
Este es el código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
	$resultado="";
	if(isset($_COOKIE['voto'])) $voto=$_COOKIE['voto'];
	if(isset($_REQUEST['voteID'])) $voteID=$_REQUEST['voteID'];
	if(isset($_REQUEST['boton_votar'])) $boton_votar=$_REQUEST['boton_votar'];
	if((isset($boton_votar))&&(isset($voteID)) ){
		if((isset($voto))&&($voto=="1")){
			$resultado="<br><font size=3 color=red>Sólo se permite votar una vez!</font><p>";
		}else{
			$id_fichero=fopen("encuesta.txt","r+")or die("<b>No se ha podido abrir el fichero 'encuesta.txt'.</b><p>");
            $votacion=array();
            $titulo_encuesta=trim(fgets($id_fichero,256));
            while(!feof($id_fichero)){
				$linea=fgets($id_fichero,256);
				$votacion[]=explode("¦",$linea);
			}
            fclose($id_fichero);
            $validador=true;
            for($i=0;$i<sizeof($voteID);$i++)
				if((!is_numeric($voteID[$i]))&&($voteID>=sizeof($votacion))&&($voteID<0)){
					$validador=false;
					break;
				}
            if($validador){
				for($i=0;$i<sizeof($voteID);$i++){
					$votacion[$voteID[$i]][1]+=1;
				}
				$id_fichero=fopen("encuesta.txt","w")or die("<b>No se ha podido crear el fichero 'encuesta.txt'.</b><p>");
				fputs($id_fichero,$titulo_encuesta.chr(13).chr(10));
				for($i=0;$i<sizeof($votacion);$i++){
					$linea=trim(implode("¦",$votacion[$i]));
					if($i<sizeof($votacion)-1) $linea.= chr(13).chr(10);
					fputs($id_fichero,$linea);
				}
				fclose($id_fichero);
				setcookie("voto","1",time()+120);
				$resultado="<br><font size=3 color=white>¡Gracias por votar!</font><p>";
			}else $resultado="<br><font size=3 color=red>&iexcl;Opcion de voto incorrecta!</font><p>";
		}
	}
?>
<html>
	<head>
		<title>Actividad 2</title>
		<style type=text/css>
			td {font-family: Verdana,Arial,Helvetica}
			body {font-size: 10pt; font-family: Verdana,Arial,Helvetica}
		</style>
	</head>
	<body bgcolor=91E5F2 text=000000 link=000000 vlink=000000>
		<!-- <center> -->
		<?php echo $resultado; ?>
		<table border=1><tr><td>
			<table width=100% border=0 cellspacing=0 cellpadding=10>
				<tr><td colspan=2 bgcolor=CCCCCC>
					<font size=3><b>Encuesta</b></td></tr>
				<tr><td bgcolor=FFFFFF><font size=3>
					<form action="index.php" method="post">
						<?php
							$id_fichero=fopen("encuesta.txt","r")or die("<b>El fichero \"encuesta.txt\" no se ha podido abrir.</b><p>");
							$linea=fgets($id_fichero,256);
							echo "<br><font size=3><b>$linea</b></font><br>";
							$i=0;
							while(!feof($id_fichero)){
								$linea=fgets($id_fichero,256);
								$matriz=explode("¦",$linea);
								echo "<input type=\"checkbox\" name=\"voteID[]\" value=\"$i\">".$matriz[0]."<br>";
								$i++;
							}
							echo "<input type=\"hidden\" name=\"total_opciones\" value=\"$i\">";
							fclose($id_fichero);
						?>
						<br><td align=LEFT bgcolor=FFFFFF><input type=submit name="boton_votar" value="Votar"></td>
					</form>
		<!-- </center> -->
				</td></tr>
			</table>
		</td></tr></table>
		<p>
		<table cellSpacing=0 cellPadding=2 width="100%" border=0>
			<tr><td vAlign=top width="100%">
				<!-- <center> -->
				<table cellspacing=2 cellPadding=0 bgColor=#000000 border=0>
					<tr><td colspan=2 height=50 bgcolor=CCCCCC>
					<font size=3><b>Resultados</b></td></tr>
					<tr>
						<td colspan=2>
							<table cellSpacing=2 cellPadding=5 bgColor=#ffffff border=0>
								<tr><td>
								<!-- <center> -->
								<br>
									<?php
										$id_fichero=fopen("encuesta.txt","r+")or die("<b>No se ha podido abrir el fichero \"encuesta.txt\".</b><p>");
										$votacion=array();
										$titulo_encuesta=trim(fgets($id_fichero,256));
										echo "<b>$titulo_encuesta</b><br>";
										$votos_totales=0;
										$i=0;
										while(!feof($id_fichero)){
											$linea=fgets($id_fichero,256);
											$votacion[]=explode("¦",$linea);
											$votos_totales+=(int)$votacion[$i][1];
											$i++;
										}
										fclose($id_fichero);
										echo "<table>";
										for($i=0;$i<sizeof($votacion);$i++){
											$votacion[$i][1]=trim($votacion[$i][1]);
											$porcentaje=number_format($votacion[$i][1]/$votos_totales*100,2);
											$long_barra=number_format(128*$votacion[$i][1]/$votos_totales,0);
											echo "<tr><td>".$votacion[$i][0]."</td>
												<td><img height=14 src=\"images/leftbar.gif\" width=7>";
											echo"<img height=14 alt=\"32 %\" src=\"images/mainbar.gif\" width=$long_barra>";
											echo "<img height=14 src=\"images/rightbar.gif\" width=7> $porcentaje % (".$votacion[$i][1].")</td></tr>";
										}
										echo "</table>";
									?>
								<!-- </center> -->
								</td></tr>
							</table>
						</td>
					</tr>
				</table>
				<!-- </center> -->
			</td></tr>
		</table>
		<br>
		<center><b><font color=blue>Nº total de votos: <?php echo $votos_totales; ?></font></b></center>
	</body>
</html>
Y este es el error:
"Notice: Undefined offset: 1 in C:\xampp\htdocs\curso\Actividad2\index.php on line 102"
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

error en la cuenta

Publicado por xve (6935 intervenciones) el 21/10/2018 19:37:14
Por el error, me parece que estas intentando acceder a un elemento del array que no existe.

Te recomiendo que hagas un
1
echo $linea;
para ver exactamente en que linea de marga el error, asi veras si realmente dispone de una | para el explode.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
Imágen de perfil de Charly
Val: 182
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

error en la cuenta

Publicado por Charly (69 intervenciones) el 22/10/2018 11:23:52
Ya lo he arreglado, no era problema del código de esa línea, sino del símbolo que utilizo para separar las dos partes de cada línea.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar