There have been a lot of questions asked about how to set the default values for advanced search, ie., to keep the defaults as distinct session variables.
The advanced search and advanced search panel use an object called $_SESSION[$strTableName.'_advsearch'] which many of you will have encountered before, but only to interrogate. Unfortunately there is no public API for $_SESSION[$strTableName.'_advsearch'] and no mention in the documentation, but occasionally you'll see it referenced in a tip. I gather the reason why its not public is that its a core structure and the developers wish to retain the option of evolving it release to release, let's hope an API will be added in PHPR6.2.
$_SESSION[$strTableName.'_advsearch'] is dynamic and is built-up based on the options chosen in search settings. Direct access to the structure is difficult without an API, the hard part is construction (creating or initiallizing it). Maybe later I (or someone) add a tip on how to create the $_SESSION[$strTableName.'_advsearch'] directly using an API, but for now we'll use another indirect way to create it. This is a bit of a hack but it works across multiple versions without a huge in-depth knowledge.
The $strTableName.'_list.php' can create the array $_SESSION[$strTableName.'_advsearch'] when it is passed an advanced search url, such as generated by $strTableName.'_search.php'. So first setup your search settings to enable advanced search just with the fields you need. Ideally you only want to search on fields with indices. Run your app and check that the advanced search is working ok. Reset the advanced search, and pick the field you want to set a default on, and do the search. The search.php will call the list.php with the search parameters, so take a copy of the URL. It will look something like this:
// if parameter set then
xxx.xxx/mytable_list.php?a=integrated&simpleSrchFieldsComboOpt=&simpleSrchTypeComboNot=&simpleSrchTypeComboOpt=&criteria=and&type1=&value11=MyValue&field1=MyField&option1=Equals¬1='>http://xxx.xxx/mytable_list.php?a=integrated&simpleSrchFieldsComboOpt=&simpleSrchTypeComboNot=&simpleSrchTypeComboOpt=&criteria=and&type1=&value11=MyValue&field1=MyField&option1=Equals¬1=
// else if previous parameters were cleared then
http:/xxx.xxx/mytable_list.php?a=integrated&simpleSrchFieldsComboOpt=&simpleSrchTypeComboNot=&simpleSrchTypeComboOpt=&criteria=and
Change it so it looks something like this (could be compound as well):
global $strTableName;
$MyValue1 = urlencode($_SESSION[$strTableName."my_search1"]); // get your advance search initial value(s). Note: may also need to use htmlspecialchars()
$MyField1 = "fieldname1";
$s1 = $strTableName."_list.php?a=integrated&simpleSrchFieldsComboOpt=&simpleSrchTypeComboNot=&simpleSrchTypeComboOpt=&criteria=and&type1=&value11=".$MyValue1."&field1=".$MyField1."&option1=Equals¬1=";
and paste this into your table_list page before process event. What we are going to do is detect the conditions for when you want to initialze $_SESSION[$strTableName.'_advsearch'], such when there is no search criteria already or first time in, or whatever. How and when you initialize $MyValue and $MyField is up to you. Then invoke this:
if ($conditions) {
// initializes advsearch
header("Location: ".$s1);
exit();
}
That's it. What this does is refreshes the list.php with the search defaults applied. But you don't want to create a loop. You have to do this conditionally, such as first time only, or if there no search parameters already, etc. Of course instead of automatic, the URL can placed on the page in a link, list of links, dropdown box, etc.
To detect if there already are search parameters in use you need to interrogate $_SESSION[$strTableName.'_advsearch']. Use this adapted snippet from the Help:
$i=0;
if (isset($_SESSION[$strTableName.'_advsearch'])){
// grab the search parameters
include_once("classes/searchclause.php");
$searchClauseObj = unserialize($_SESSION[$strTableName.'_advsearch']);
if ($searchClauseObj->isUsedSrch()){
if ($searchClauseObj->_where[$strTableName."_simpleSrch"] == ''){
foreach($searchClauseObj->_where[$strTableName."_srchFields"] as $srchField) {
if($srchField['fName'] != ''){ // or $srchField['value1'] != '', but usually if the value is null the fieldname is also null
$i++; // found an advanced search parameter
// add a parameter save here ...
}
}
}
}
}
// if $i > 0 then there are advanced search parameters in effect, so don't do the initialization
// if $i == 0, then even if there was previous search parameters, then they have been cleared
If you want to capture subsequent search parameters and save to session variable, log or persistent store, then just add one or more of these lines to the above:
if ($srchField['fName']=="fieldname1") $_SESSION[$strTableName."my_search1"] = $srchField['value1']; // fName, value1, value2, opt, not
Note: If the advanced search panel is enabled (in PHPR6+ with ajax enabled), there is a small consideration in capturing what search parameters are subsequently entered on the panel (as opposed to the advanced search page). The panel's search filter is invoked via AJAX (but still using _advsearch) and the grid is updated, but some table events are not triggered. The events that are triggered are beforeSQLquery and before/after record processed. So the capturing of search parameter changes should be done in the beforeSQLquery event.
To disable showall button with this snippet on the BeforeDisplay:
$xt->assign("searchform_showall",false); // this works on PHPR6+, hides the showall button