使用会话变量的数组是空的PHP

问题描述:

我有问题传递一个数组与浮点值。它在另一个文件中是空的。这是代码。请任何帮助...使用会话变量的数组是空的PHP

//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; 

我仍然有问题。我认为我使用POST错误。我有3个脚本。第一个获取条目,第二个获取数组,第三个使用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("</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 
+1

你不把$ NOTAS阵列中的会话 – 2011-05-28 04:27:36

+0

感谢所有。我找到了问题,但不是解决方案。 – irmay 2011-05-28 19:40:32

这将按钮提交

//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; 

工作,这将与会话

//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; 

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

工作在你设置$notas数组的第一个文件,但在第二个你使用$_POST['notas']将其添加到会话变量,然后分配到$notas。第二个文件中的计数为0,因为您不计算第一个文件中生成的数组的元素,而是对POST请求(存储在$_POST数组中)传递的值之一进行计数。

总结:你创建了一个数组,但是统计了不同的元素

取决于你如何调用第二个文件(?它是一个不同的请求是从第一个包含?),你有以下选择:

A.(如果它是一个不同的请求)指定$notas变量像会话数组元素:

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

和代替从$_POST['notas']从中读取第二文件,或

B.(如果第二文件被从第一个包括)使用相同的v良莠不齐作为第一个文件($notas),并分配给它,而不是$_POST['notas']

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

我想作一些计算查询结果中的文件2之前发送它在文件3.我还是don'到jgraph知道为什么这部分doen't工作:

//文件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>"; 

我决定发表评论部分“calcular西格玛Ÿ妹”,现在,我可以通过数组到文件3.

//文件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");