开发者

Array is empty using session variables PHP

I´m having problems passing an array with float values. It´s empty in the other file. Here´s the code. Please any help...

 //file 1 
session_start();
//...
while ($row = pg_fetch_assoc($sql))
{       
    $notas[$i] = ($row['grade'] - 57.3)/12;                 
    $i++;
}

print("<FORM method=post action='../indicadores/distr_notas.php'>");
print("<input type=hidden name=notas value='$notas'>");
print("<INPUT type=submit>");
print("</FORM>");

//file 2
session_start();

$_SESSION['notas'] = $_POST['notas'];
$notas = $_SESSION['notas'];


$cant = count($notas);
echo $cant; 

I´m still having problems. I think I`m using POST wrong. I have 3 scripts. The first one get the entry, the second one gets the array and the third one shows a graph with the array using jpgraph.

file 1 // get the array
<?php  
    print("<FORM method=post action='proc_notas.php'>");
    print("Codigo de Carrera.<p>");
    print("<INPUT type=text name='cod_depto'><p>");
    print("<INPUT type=submit>");
    print("</FORM>");
 ?>


//file 2. 

if($_SERVER['REQUEST_METHOD'] != "POST")
{
print("<FORM method=post action='normal.php'>");
print("Desviación estándar.<p>");
print("<INPUT type=text name='desviacion'><p>");
print("<INPUT type=submit>");
print("&l开发者_JS百科t;/FORM>");
}
else
{
    $depto = $_REQUEST['cod_depto'];
    $ordenada = array();
    $z = array();
        $i = 0;
        $suma = 0;
    $conectar = new Conector();
    $cadena =  "select distinct a.grade FROM evaluation_student_evals a inner join td_estudiantes b on a.party_id = b.id_estudiante where b.cod_depto = '$depto' and a.grade >= 0 and a.grade <= 100 order by a.grade ";
    $sql = $conectar-> consultas($cadena);      
    //calcular sigma y miu      
    $total = pg_num_rows($sql);
    $sumanotas = new distribucion();
    $totalnotas = $sumanotas -> suma_notas($sql);
    $media = $totalnotas/$total;

//Normalizar datos de notas Z = (X-miu)/sigma
    while ($row = pg_fetch_assoc($sql))
    {       

        $ordenada[$i] = (1/(12*sqrt(pi())))*(exp(-0.5*(($row['grade']-$media)*($row['grade'] - $media))/($desviacion*$desviacion)));                                    
        $i++;   }               

    if (!isset($row))
    {
        header("Content-Type: text/html");
        print("<HTML><HEAD><TITLE>Desempeño de Aprendizaje</TITLE>");
        print("</HEAD>");
        print("<BODY>");
        print("$depto no se encuentra.");
        print("</BODY></HTML>");


//file 3 Graph the array
Here, I don´t know how to get the array $ordenada


 $_SESSION['notas'][] = ($row['grade'] - 57.3)/12;     


This will work with button submission

 //file 1 
session_start();
//...
while ($row = pg_fetch_assoc($sql))
{       
    $notas[$i] = ($row['grade'] - 57.3)/12;                 
    $i++;
}

print("<FORM method=post action='../indicadores/distr_notas.php'>");
print("<input type=hidden name=notas value='".implode(',' $notas)."'>"); // use implode to convert array to string
print("<INPUT type=submit>");
print("</FORM>");

//file 2
session_start();

$notas = explode(',' $_POST['notas']);  // use explode to convert string to array

$cant = count($notas);
echo $cant;

And this will work with session

//file 1 
session_start();
//...
while ($row = pg_fetch_assoc($sql))
{       
    $_SESSION['notas'][$i] = ($row['grade'] - 57.3)/12;                 
    $i++;
}

print("<FORM method=post action='../indicadores/distr_notas.php'>");
print("<input type=hidden name=notas value='$notas'>");  // no need to use
print("<INPUT type=submit>");
print("</FORM>");

//file 2
session_start();

$notas = $_SESSION['notas'];


$cant = count($notas);
echo $cant;


In the first file you set $notas array, but in the second you use $_POST['notas'] to add it to session variables, then assign to $notas. The count in the second file is 0, because you do not count the elements of array generated in the first file, but instead you count elements of one of the values passed by POST request (stored in $_POST array).

To sum up: you create one array, but count elements of different one.

Depending on how you invoke the second file (is it a different request? is it included from the first one?), you have following options:

A. (if it is a different request) Assign $notas variable to the session array element like that:

// at the end of the first file:
$_SESSION['notas'] = $notas;

and in the second file read from it instead from $_POST['notas'], or

B. (if the second file is included from the first one) Use the same name of the variable as in the first file ($notas) and assign it instead of $_POST['notas']:

// in the second file, instead of " $_SESSION['notas'] = $_POST['notas']; "
$_SESSION['notas'] = $notas;


I wanted to make some calculations to the query result in file 2 before to send it to jgraph at file 3. I still don´t know why this part doen´t work:

//file 2

$depto = $_REQUEST['cod_depto'];
$desviacion = $_REQUEST['desviacion'];
$ordenada = array();
$i = 0;
$suma = 0;
$conectar = new Conector();
$cadena =  "select distinct a.grade FROM evaluation_student_evals a inner join td_estudiantes b on a.party_id = b.id_estudiante where b.cod_depto = '$depto' and a.grade >= 0 and a.grade <= 100 order by a.grade ";
$sql = $conectar-> consultas($cadena);      

//calcular sigma y miu          
$total = pg_num_rows($sql);
$sumanotas = new distribucion();
$totalnotas = $sumanotas -> suma_notas($sql);
$media = $totalnotas/$total;

//Normalizar datos de notas Z = (X-miu)/sigma
while ($row = pg_fetch_assoc($sql))
    {       
        $ordenada[$i] = (1/($desviacion*sqrt(pi())))*(exp(-0.5*(($row['grade']-$media)*($row['grade'] - $media))/($desviacion*$desviacion)));                                      $i++;
    }
$tmp= serialize($ordenada);
$tmp= urlencode($tmp);
echo "<form method=post action='../indicadores/distr_notas.php'>";
echo "<input type=hidden name=ordenada value=$tmp>";
echo "<input type=submit name=enviar>";
echo "</form>";

I decide to comment the part "calcular sigma y mui" and now, I can pass the array to file 3.

//file 3

function array_recibe($url_array) {
    $tmp = stripslashes($url_array);
    $tmp = urldecode($tmp);
    $tmp = unserialize($tmp);

   return $tmp;
} 

$notas =$_REQUEST['ordenada'];
$notas =array_recibe($notas);

//jpgraph code
$graph = new Graph(600,400,"auto");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜