I need to find a way to easily set a session variable. At least I think a session variable is called - hopefully someone here can set me straight. The executive summary of what follows is this question : is there a way of formatting a letter (e.g. "B", "D") in such a way that clicking on it posts that value to a session variable?
The situation is this : I am using a variant of LoopDBChunk to search an external database and return a set of paginated results. The search query is generated from input into a simple form field on the page. At the same, there is a list of letters across the top of the page which reload the page while appending a parameter to the URL. The value from the form searchfield is posted to a session variable. The parameter on the URL obviously is not in a session variable. The purpose of the list of letters is to allow a quick search on all records whose name begins with that letter. The form field allows for finding records whose description contains the word/phrase entered in the field.
The format of the "letter" navigation looks like this :
<a href="[~[*id*]~]&x=[0-9]">#</a>/
<a href="[~[*id*]~]&x=a">A</a>/ [...]
which is followed by the form, which looks like this :
<form method="post" action="[~[*id*]~]">
<p>Search database:<br />
Search:
<input type="text" name="searchfield" size="25" />
<input type="submit" value="Search" /><br />
</p>
</form>
Below this is a call to a snippet which looks like this :
<?php
//see if a value has been posted from the form, or if we're loading this page as part
//of paginated results from a search that's already been conducted
$searchtext = isset($_POST['searchfield']) ? $_POST['searchfield'] : $_SESSION['searchfield'];
$x = $_GET['x']; //should check to see if it's been set
if (!empty($searchtext) && empty($x)){
$query = "SELECT * FROM dbtable WHERE description LIKE '%".$searchtext."%'";
//if the query results take up more than one page, we'll lose the POST value
//so we need to save it in case we've got more pages coming
$_SESSION['searchfield'] = $searchtext;
$url_parameters = '';
}
elseif (empty($searchtext) && empty($x)){
$query = "SELECT * FROM dbtable";
$_SESSION['searchfield'] = ''; //this should be an unset command
$url_parameters = '';
}
else {
$query = "SELECT * FROM dbtable WHERE description REGEXP '^".$x."'";
$_SESSION['searchfield'] = ''; //this should be an unset command
$url_parameters = 'x='.$x;
}
$loopOutput = $modx->runSnippet( 'loopdbchunk', array(
'sql' => $query,
'chunkName' => 'show_record_chunk',
'missingValTxt' => '',
'numLinks' => '2',
'perPage' => '10',
'URLparameters' => $url_parameters
)
);
return $loopOutput;
?>
(The reason for "URLparameters" is to allow the pagination associated with a long list returned by the query to continue to work. Without keeping track of the parameter passed on the URL, the "pagestart" value ends up referring to the full database list and not the list returned. As best I can tell ...)
With some refinement of the values passed to the Paginate function in LoopDBChunk, this whole Rube Goldberg contraption works surprisingly well. But I think it makes more sense to not clutter up the URL. Which leads me to wanting to set session variables. How that is accomplished though while retaining the "one click" character of the top navigation bar eludes me. Does anyone have any suggestions on how to accomplish that? Or how alot of this could be accomplished more elegantly?
(To address the elephant in the room : I’m doing this because I need to replicate the existing look and feel of a website before embarking on a larger overhaul.)
If you’ve read this far, thanks for taking the time,
MattC