After quite a few hours of agony and self-doubt I seem to have managed to get the Sessions working so that a list of searched-for items can be displayed on successive pages that share the same snippet call - the same snippet that uses chunks to set the search form up and also processes the search query.
I was hoping that it would have been the sort of learning experience that would put me in a position to write something to really help others who might be similarly clueless. But no. There was too much trial-and-error stuff - trying things out without having a clue whether they should work or not, and realising I really didn’t know whether sometimes I was dealing with strings or arrays or some strange hybrid.
But if people are looking for a clear explanation of how to use sessions, the clearest I found over the last few hours is here:
http://www.phpro.org/tutorials/Introduction-To-PHP-Sessions.html
As pointed out by Jason and Susan, there is no need to put session_start() at the top of the relevant pages.
So far the following seems to be working for me, but it breaks the rules by sneaking in some presentation stuff, and I am sure there are other problems. Here is an excerpt with a while loop used to work through a query result. It stores the results of the while loop in a session variable.
$resultb = mysqli_query($dblink, $queryb);
while ($row = mysqli_fetch_assoc($resultb)) {
$var1 = $row['desc'];
$var2 = $row['alias'];
$_SESSION['searchoutput'][] = '<li><a href=" '. $var2 . '.html">' . $var1 . '</a></li>';
} // end while loop
Then to print out the array stored in the session variable my guess was that I needed something like this:
if(isset($_SESSION['searchoutput'])) {
foreach($_SESSION['searchoutput'] as $key=>$value){
$toprint = $value;
}
$output = $modx->getChunk('searchchunk', array(
'searchresults' => $toprint,
));
return $output;
}
That unpacks the array and sends the contents to the chunk called ’searchchunk’, which contains the placeholder [[+searchresults]].
Larry Ullman’s book on good old-fashioned procedural PHP made PHP seem easy. I’m going to stick with Modx, but I have to admit that it is heavy going. There seems to be a massive leap required to get from the cosy stuff described by Larry to the kind of thing required by Modx.
Thanks again for the excellent support.