$_SESSION 似乎只保留最后一个值
$_SESSION seems to retain only last value
请在此处查看测试页https://wintoweb.com/sandbox/question_3.php
我使用 $_SESSION 来存储数据库搜索的结果,但只有最后一个值存储在会话中。看起来后者在每次搜索时都被清空了。
我以前在该服务器上使用过会话,没有遇到任何问题。我的代码可能有问题,但我无法弄清楚。 session_start 在文件顶部调用。
<?php
if(isset($_GET['search'])){
} else if(isset($_GET['display_this'])){
$rets = getNames(); //The $rets will hold the value returned by your function getName().
if(!empty($rets)){
echo '</br><b>Your selections so far :</b></br></br>';
}
//But yet, only the last search is stored in the session (why?)
echo "Echo of session array : " . $_SESSION['author'] . "<br>";
}
function getNames(){
$rets = '';
if(isset($_GET['choices']) and !empty($_GET['choices'])){
foreach($_GET['choices'] as $selected){
$rets .= $selected . ' -- ';
// This should add every $rets to the session array. Right?
$_SESSION['author'] = $rets;
//session_write_close();
}
}
return $rets;
}
?>
我希望会话保留后续搜索的所有信息,但只存储最后一个值。
您每次都在用新值覆盖会话数组。您需要附加到它,就像附加到 $rets
变量一样。
$_SESSION['author'] .= $rets;
几件事。我会在代码中解释。
// make sure that the function is before it is called.
// computers execute code top to bottom, and so getNames wouldn't even exist yet
// so therefore your if statement (below) will always evaluate to false
function getNames($choices){
// use parameters instead of $_GET inside of a function
$rets = '';
if(isset($choices) && !empty($choices)){
foreach($choices as $selected){
$rets .= $selected . ' -- ';
// you need to append using '.=' not '=' otherwise all the data will be overwritten
$_SESSION["author"] .= $rets;
}
}
return $rets;
}
// you shouldn't be checking if something isset and then doing nothing about it
// you might as well just be checking if it isn't.
if(!isset($_GET["search"]) && isset($_GET['display_this'])){
$rets = getNames($_GET["choices"]);
if(!empty($rets)){
echo '</br><b>Your selections so far :</b></br></br>';
}
echo "Echo of session array : " . $_SESSION['author'] . "<br>";
}
// you need to move this to the end of your script, otherwise the session will be closed
// before the second iteration of your loop begins and you won't be able to change values
// or access them later on (like in the above if statement)
session_write_close();
请在此处查看测试页https://wintoweb.com/sandbox/question_3.php 我使用 $_SESSION 来存储数据库搜索的结果,但只有最后一个值存储在会话中。看起来后者在每次搜索时都被清空了。
我以前在该服务器上使用过会话,没有遇到任何问题。我的代码可能有问题,但我无法弄清楚。 session_start 在文件顶部调用。
<?php
if(isset($_GET['search'])){
} else if(isset($_GET['display_this'])){
$rets = getNames(); //The $rets will hold the value returned by your function getName().
if(!empty($rets)){
echo '</br><b>Your selections so far :</b></br></br>';
}
//But yet, only the last search is stored in the session (why?)
echo "Echo of session array : " . $_SESSION['author'] . "<br>";
}
function getNames(){
$rets = '';
if(isset($_GET['choices']) and !empty($_GET['choices'])){
foreach($_GET['choices'] as $selected){
$rets .= $selected . ' -- ';
// This should add every $rets to the session array. Right?
$_SESSION['author'] = $rets;
//session_write_close();
}
}
return $rets;
}
?>
我希望会话保留后续搜索的所有信息,但只存储最后一个值。
您每次都在用新值覆盖会话数组。您需要附加到它,就像附加到 $rets
变量一样。
$_SESSION['author'] .= $rets;
几件事。我会在代码中解释。
// make sure that the function is before it is called.
// computers execute code top to bottom, and so getNames wouldn't even exist yet
// so therefore your if statement (below) will always evaluate to false
function getNames($choices){
// use parameters instead of $_GET inside of a function
$rets = '';
if(isset($choices) && !empty($choices)){
foreach($choices as $selected){
$rets .= $selected . ' -- ';
// you need to append using '.=' not '=' otherwise all the data will be overwritten
$_SESSION["author"] .= $rets;
}
}
return $rets;
}
// you shouldn't be checking if something isset and then doing nothing about it
// you might as well just be checking if it isn't.
if(!isset($_GET["search"]) && isset($_GET['display_this'])){
$rets = getNames($_GET["choices"]);
if(!empty($rets)){
echo '</br><b>Your selections so far :</b></br></br>';
}
echo "Echo of session array : " . $_SESSION['author'] . "<br>";
}
// you need to move this to the end of your script, otherwise the session will be closed
// before the second iteration of your loop begins and you won't be able to change values
// or access them later on (like in the above if statement)
session_write_close();