I am creating a survey and when adding the total votes that have been made, I get an error. This is the code:
<?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>¡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>
And this is the error:
Notice: Undefined offset: 1 in C: \ xampp \ htdocs \ course \ Activity2 \ index.php on line 102
Line 102 is this:
$votos_totales+=(int)$votacion[$i][1];