I don't know if I'm doing wrong but let's say I have this.
formulario.php
<? session_start();
$_SESSION['promedio'] = $number;
echo '
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<meta content="text/html; charset=utf-8" http-equiv=Content-Type>
<body>
<form action="main3.php" method="post">
Número de Encuestas <input type="number" name="number">
<input type="submit" value="Enviar"/>
</form>
</body>
</html> ';
?>
Now, in main3.php
<?
session_start();
$number = $_POST['number'];
$_SESSION['promedio'] = $number;
function Calcular($edades, $estado_civil, $genero){
global $number;
foreach($edades as $edad){}
echo "El promedio de edad general es: ".((array_sum($edades))/$number)."<br>";
$x;
$y;
$z;
$w;
$v;
$u;
$t;
foreach ($genero as $i) {
foreach ($i as $j) {
if($j == "Hombres")
{
$u++;
}
if($j == "Mujeres")
{
$t++;
}
$count_values[$j]++;
}
}
echo "Cantidad de hombres: ".$u."<br>",
"Cantidad de mujeres: ".$t."<br>";
foreach ($estado_civil as $a) {
foreach ($a as $b) {
if($b == "Soltero")
{
$x++;;
}
if($b == "Casado")
{
$y++;
}
if($b == "Separado")
{
$z++;
}
if($b == "Libre")
{
$w++;
}
if($b == "Viudo")
{
$v++;
}
$count_values[$b]++;
}
}
echo "Cantidad de personas solteras: ".$x."<br>",
"Cantidad de personas casadas: ".$y."<br>",
"Cantidad de personas separadas: ".$z."<br>",
"Cantidad de personas en unión libre: ".$w."<br>",
"Cantidad de personas viudas: ".$v."<br>";
}
echo '<form type="submit" action="resultados.php" method="post">';
for ($i=0; $i < $number; $i++) {
echo '
<html>
<h2>Datos de la persona '.$i.'</h2>
Edad <input type="number" name="edades[]"/> <br><br>
Sexo <br><br>
<input type="radio" name="genero['.$i.'][]" value="Hombres"/> Masculino <br>
<input type="radio" name="genero['.$i.'][]" value="Mujeres"/> Femenino <br><br>
Estado Civil <br><br>
<input type="radio" name="estado_civil['.$i.'][]" value="Soltero"/> Soltero <br>
<input type="radio" name="estado_civil['.$i.'][]" value="Casado"/> Casado <br>
<input type="radio" name="estado_civil['.$i.'][]" value="Separado"/> Separado <br>
<input type="radio" name="estado_civil['.$i.'][]" value="Libre"/> Unión Libre <br>
<input type="radio" name="estado_civil['.$i.'][]" value="Viudo"/> Viudo <br><br> ';
}
echo '
<input type="submit">
</form>
</html> ';
?>
Last file resultados.php
<?
include ("main3.php");
echo Calcular($_POST['edades'], $_POST['estado_civil'], $_POST['genero']);
?>
So, as you can see, I call the $number variable into a function and this is where the problem is. It keeps saying I'm dividing by zero, which means $number isn't saving the value through $_SESSION when I call it in Average(). I've some things to get this to work, but no luck.
Any suggestion or help about what I'm missing would be appreciated.
The problem is that when you submit the form in
main3.php
toresultados.php
, there's nonumber
input. You can add it as a hidden input in the second form:Also, you shouldn't output
<html>
in thefor
loop inmain3.php
.