-1

Please see the test page here https://wintoweb.com/sandbox/question_3.php I use $_SESSION to store results of DB searches but only the last value is stored in the session. Looks like the latter is emptied upon each search.

I've used session before on that server and had no problems. My code is probably faulty but I cannot figure it out. session_start is called at top of file.

<?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;
}
?>

I expect the session to retain all info from subsequent searches but only the last value is stored.

stressless
  • 13
  • 4

2 Answers2

0

You are overwriting your Session array everytime with new value. You need to append to it, just like you are appending to $rets variable.

$_SESSION['author'] .= $rets;
ascsoftw
  • 3,466
  • 2
  • 15
  • 23
0

A couple of things. I'll explain within the code.

// 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();
GROVER.
  • 4,071
  • 2
  • 19
  • 66
  • I have used your code as is here https://wintoweb.com/sandbox/question_4.php but get some strange behavior. can you see it? – stressless Aug 13 '19 at 07:30
  • Oh! I got it. In getNames() the line " $rets .= $selected . ' -- ';" should be " $rets = $selected . ' -- ';" Note the absence of the dot before = It now works like a charm. I have updated the test page and added a clear session button on https://wintoweb.com/sandbox/question_4.php. Many thanks! – stressless Aug 13 '19 at 10:35
  • @stressless no worries friend! :) – GROVER. Aug 13 '19 at 10:42
  • `isset($choices) && !empty($choices)` is an antipattern that should not exist in any application for any reason. https://stackoverflow.com/a/4559976/2943403 – mickmackusa Jan 08 '22 at 15:01
  • @mickmackusa Thanks for the comment. Unfortunately I was still in the early stages of learning PHP back in 2019, so feel free to edit this post if you feel it necessary :-) – GROVER. Jan 11 '22 at 23:29
  • @GRO I will welcome you to always [edit] any of your answers (from any year) when you find that it does not represent the best of your current knowledge. Only by improving our own answers does Stack Overflow have a scalable way of maintaining a repository of excellent, current, educational advice. – mickmackusa Jan 12 '22 at 01:02