$ _SESSION变量只存储一个值

$ _SESSION变量只存储一个值

问题描述:

I have this code:

// Values of drop-down lists (cmbPosition)
$studPosition = array();
$i = 0;
foreach ($_SESSION['arrayNamePosition'] as $value) {
    $studPosition[$i] = $_GET[$value];
    $i++;
// Calculates the points for each student in the event
    $points = ($_SESSION['noStudents']+1) - $_GET[$value]; 
    echo $points;
    $_SESSION['points'] = $points;
}

The code above loops through $_SESSION['arrayNamePosition'] which contains an array. Every thing works but $_SESSION['points'] = $points; is where the problem is (This is the $_SESSION variable which contains $points).

When I echo $points in the current php form, it outputs: 583276

But when I echo $_SESSION['points'] inside a while loop in a different php form it only outputs the last element stored in $echo $_SESSION['points']points which is 6.

How can I fix this so that echo $_SESSION['points'] outputs all the values of $points in a different php form.

NOTE: I have also put echo $_SESSION['points'] inside a for loop but it still outputs the last value stored in $points. e.g. output: 666666

Thanks in advance.

我有这段代码: p>

  // drop的值 -down lists(cmbPosition)
 $ studPosition = array(); 
 $ i = 0; 
foreach($ _SESSION ['arrayNamePosition'] as $ value){
 $ studPosition [$ i] = $ _GET [$  value]; 
 $ i ++; 
 //计算事件中每个学生的积分
 $ points =($ _SESSION ['noStudents'] + 1) -  $ _GET [$ value];  
 echo $ points; 
 $ _SESSION ['points'] = $ points; 
} 
  code>  pre> 
 
 

上面的代码循环遍历 $ _ SESSION [ 'arrayNamePosition'] code>,其中包含一个数组。 每件事都有效,但 $ _ SESSION ['points'] = $ points; code>是问题所在(这是包含 $ points code>的$ _SESSION变量)。 p >

当我在当前php表单中回显 $ points code>时,它输出:583276 p>

但是当我 echo $ _SESSION ['points'] code>在一个不同的php形式的while循环中,它只输出存储在$ echo $ _SESSION ['points'] code>点的最后一个元素,即6。 p >

如何解决此问题,以便 echo $ _SESSION ['points'] code>以不同的php形式输出 $ points code>的所有值。 p>

注意:我还在for循环中放入 echo $ _SESSION ['points'] code>,但它仍然输出存储在$ points中的最后一个值。 例如 输出:666666 p>

提前致谢。 p> div>

It's because $_SESSION['points'] itself is not an array. You should change your line to:

$_SESSION['points'][] = $points;

Variable points is not an array, therefore it contains only last calculated element. Change it to array, then save each value as new element in array.

I guess you mean that the loop with echo $points; outputs you 583276. Like the $_SESSION['points'], it stores only one value, but you print the value each time. I would suggest following changes:

$studPosition = array();
$i = 0;
$points = '';
foreach ($_SESSION['arrayNamePosition'] as $value) {
  $studPosition[$i] = $_GET[$value];
  $i++;
  $points = $points.(($_SESSION['noStudents']+1) - $_GET[$value]); 
}
echo $points;
$_SESSION['points'] = $points;