Shortly after updating one of my posts (see the post) I ran into another situation that required a similar requirement. However, this time my previous approach simply could not meet the challenge because I was faced with creating a dropdown field that had to have only two choices (Yes or No) to indicate whether records on the list page had purchased a membership for services in my web app.
It sounds simple at first, however here is the why its not:
- I needed to display on-demand whether users on my list page had purchased memberships for an association or not.
- When users purchased a membership they had several options for different membership types. I have a field in my DB called "Group_Membership" which was an integer type column that was populated a lookup field that pulled membership types from another table. Ex: 1-Silver Membership; 2-Gold Membership; 3-Platinum Membership; 4-Super Duper Membership, etc
- The default value for my "Group Membership" column in my DB was set to ZERO. Thus, newly created users automatically have a zero value assigned to their "Group_Membership" to indicate they have not yet purchased a membership.
- If I were to use the previous approach I used for on-demand list filtering with dropdown box (see link I included above to my previous post) it would fail because my dropdown list would be populated with all of the available memberships INSTEAD of a simple dropdown that offered two choices "Purchased Membership - YES" and "Purchased Membership - NO".
- This means, that I would have not way of choosing a value in dropdown that would show me those who have not purchased a membership because that wouldn't even show up as an option.
- Additionally, if I picked any of the membership types shown (silver, gold, platinum, super-duper, etc) it would only filter to show those specific types; thus not meeting my objective/requirement of showing/filtering all records that had either purchased or not purchased a membership (regardless of the specific type of membership).
- Stay with me... I know the previous passages sound confusing or complicated, but it really isn't. Keep reading...
- So here is my solution... insert a php snippet in the list page you need to filter and add this code in the snippet:
//Preapre query string url parts so we can read the href url in js below more easily.
//These parts are using generic query string search parameters... we plug in values we want manually between the parts!
$Part1 = "a=integrated&ctlSearchFor=&simpleSrchFieldsComboOpt=&simpleSrchTypeComboNot=&simpleSrchTypeComboOpt=&criteria=and&type1=&value11=";
$Part2 = "&field1=";
$Part3 = "&option1=Equals¬1=on"; //Not criteria is on
$Part3B = "&option1=Equals¬1=off"; //Not criteria is off
$Part4 = "&MBR_Actual_Selected="; //My own made-up URL parameter (deosn't affect the actual url query)
//create dropdown box
//Here we are telling to search for value ZERO (which indicates user is not a group member since that's the default field value when record is created in db)
//Then we tell it to search for that value in Group_Member field in the Households_list.
//Essentially if option 1 is selected we want the filter to NOT show us any users that are not a membership holder (has group_member value of zero...cuz that indicates they are not a group member).
//Notice in 1st IF statement we are saying: "IF dropdown selected is YES then search for group_member NOT equal to zero value"
//Notice in 2nd IF statement we are saying: "IF dropdown selected is NO then search for group_member that IS equal to zero value"
$str = "";
$str.= "<select id=\"mbr_dropdown\" onchange=\"
if(this.options[this.selectedIndex].value == 1){
window.location.href='users_list.php?" . $Part1 ."0".$Part2."Group_Member".$Part3 . $Part4."'+this.options[this.selectedIndex].value+'';}
if(this.options[this.selectedIndex].value == 0){
window.location.href='users_list.php?" . $Part1 ."0".$Part2."Group_Member".$Part3B . $Part4."'+this.options[this.selectedIndex].value+'';}
\"><option value=\"\">Select Group Members</option>\"
<option value=1>IS Group Member-Yes</option>
<option value=0>IS Group Member-NO</option>
</select>";
echo $str;
//Insert PHP variable into a javascript value... which we later use in the JS onLoad event to set the dropdown box again so once page refreshes we don't lose the value that was selected in the first place (so we know what we selected)!
echo
"<script>
window.MBR_Actual_Selected = '" . $_GET["MBR_Actual_Selected"]. "';
</script>";
9. Now, in order for you to disaply what you selected in the dropdown box after the "onchange" event (declared in the JS of your dropdown) refreshes the page to display your filtered results, you must also add this code to JavasScript onLoad event of the list page:
//Grab value of dropdown box that filters schools on list page
//We want to set the value of dropdown so once its selected and page refreshes we know what value we had selected
//The MBR_Actual_Selected variable is set by php snippet in list page
document.getElementById("mbr_dropdown").value = MBR_Actual_Selected;
10. Done. Now you can filter a simple "Yes" or "No" criteria on demand on list page; regardless if the "YES" value actually involves multiple values... you essentially aggregate into one answer.
BTW... I am well aware that another solution would be to simply add another field in your DB that tracks whether someone has a membership as T/F; however that requires adding another field and maintaining that field in synch with the Group_Member field. Choose whatever makes most sense for you. I wanted to avoid adding a new field given that my project was undergoing an upgrade and I don't like adding new fields on a whim.
Cheers,