We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38783
    • 571 Posts
    Hello,

    I have used Bob Ray's excellent 'ClassExtender' to create additional user fields.

    I am able to search those fields using his utility snippet 'UserSearchForm'.

    I would like to be able to perform searches using a checkbox format, but am not sure how I would achieve that.

    Any help/guidance would be greatly appreciated.



    In the examples below which show my code I have two extended fields:
    profileTwo
    profileThree

    I have two sample users with the following values applied to these fields:
    Cathy Brear: profileTwo: 0, profileThree: 1
    Test User: profileTwo: 1, profileThree: 0

    I would like site visitors to be able to:
    Check a box for profileTwo and have returned to them any user who has the value 1 applied to profileTwo (ie Cathy Brear)
    Check a box for profileTwo and profileThree and have returned to them any user who has the value 1 applied to profileTwo or profileThree (ie Cathy Brear and Test User)

    I would also like them to be able to use the checkbox method, in a second form, to search for users who fulfill all the criteria they have selected.

    Below are extracts from my code
    UserSearchFormTest Snippet
    $formTpl = $modx->getOption('extFormTpl', $scriptProperties, 'ExtUserSearchFormTestTpl');
    $output = $modx->getChunk($formTpl);
    $pprofileTwo = $modx->getOption('user_search_profileTwo', $_POST, '');
    $pprofileThree = $modx->getOption('user_search_profileThree', $_POST, '');
    $modx->setPlaceholder('user_search_profileTwo', $pprofileTwo);
    $modx->setPlaceholder('user_search_profileThree', $pprofileThree);
    $fields = array();
    if (isset($_POST['submit-var']) && $_POST['submit-var'] == 'etaoinshrdlu') {
        $fields['where'] = '{"profileTwo:=":"' . $pprofileTwo . '","OR:profileThree:=":"' . $pprofileThree . '"}';
        $results = $modx->runSnippet('GetExtUsers', $fields);
    }
    if (! empty ($results) ){
        $modx->SetPlaceholder('user_search.results_heading',
            $modx->lexicon('ce_user_search_results_heading'));
        $modx->setPlaceholder('user_search.results', $results);
    }
    return $output;


    UserSearchFormTestTpl Chunk
    <h3>User Search</h3>
    
    <p>Enter a user first and/or last name to search for, then click on Submit</p>
    
    <form class="ext-user-search-form" id="ext-user-search-form" method="post">
        <input type="hidden" name="submit-var" value="etaoinshrdlu"/>
    
        <label for="user_search_profileTwo">[[%user_search_profileTwo_caption? &namespace=`classextender` &topic=`default`]]<br/>
            <input type="text" size="50" id="user_search_profileTwo"
                    name="user_search_profileTwo" value="[[+user_search_profileTwo]]"/><br/>
        </label>
    
        <label for="user_search_profileThree">[[%user_search_profileThree_caption? &namespace=`classextender` &topic=`default`]]<br/>
            <input type="text" size="50" id="user_search_profileThree"
                    name="user_search_profileThree" value="[[+user_search_profileThree]]"/><br/>
        </label>
        <br/>
        <input type="submit" value="Submit">
    
    </form>
    
    <h3>[[+user_search.results_heading]]</h3>
    
    [[+user_search.results]]


    core/components/classextender/lexicon/en/default.inc.php
    /* Used in extusersearchformtpl.chunk.html */
    $_lang['user_search_profileTwo_caption'] = 'Agility';
    $_lang['user_search_profileThree_caption'] = 'Flyball';
      If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

      email: [email protected] | website: https://andytough.com
      • 3749
      • 24,544 Posts
      I don't see anything obvious that's wrong. Is your custom class called 'userData'? If not, you'll need to set the 'UserDataClass' member of the $fields array. Are you getting any results at all?

      Incidentally, do you know both trivia questions that have 'etaoinshrdlu' as an answer (no Googling)?
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 38783
        • 571 Posts
        Hello Bob

        Thank you for getting back to me.

        I had to Google etaoinshrdlu! Interesting.

        As you say, there is nothing obviously wrong and indeed the search form is working correctly.

        Where I got stuck was how to format it so that the user would tick checkboxes rather than entering their search terms.

        ie: they tick the boxes for agility AND flyball and get to see all the trainers who offer both those services.

        When I tried formatting the UserSearchFormTestTpl Chunk with checkboxes I stopped getting any results.

        I did start looking into using simple search but that just lead to more questions! https://forums.modx.com/thread/99525/incorporating-class-extender-extend-moduser-data-into-simple-search-results#dis-post-538209

        If you can shed any light on how I could use checkboxes your thoughts would be gratefully received.

          If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

          email: [email protected] | website: https://andytough.com
          • 38783
          • 571 Posts
          Hello Bob

          I have made progress with checkboxes, and I am getting results, but they are not the right results! I think the solution may be very simple, but it eludes me.

          My form now consists of the three elements shown below.

          I have these trainers set up:
          Cathy Brear
          Agility: 1
          Flyball: 1
          Rally: 0

          Test User
          Agility: 1
          Flyball: 1
          Rally: 1

          What I would like to to happen is that if someone checks the Agility and Flyball boxes they get the trainers that do Agility AND Flyball, regardless of what other services those trainers may provide.

          But if I check the boxes for Agility and Rally I get no results. I would like to get Test User but not Cathy Brear.
          I only get Test User if I check all three boxes as that is EXACTLY what Test User provides.

          I think my problem lies with the AND in this line, but I may be wrong.
          $fields['where'] = '{"profileTwo:=":"' . $pprofileTwo . '","AND:profileThree:=":"' . $pprofileThree . '","AND:profileFour:=":"' . $pprofileFour . '"}';


          Am I nearly there, or am I totally off course and out of my depth?!

          My Current Code
          UserSearchFormTest Snippet
          $formTpl = $modx->getOption('extFormTpl', $scriptProperties, 'ExtUserSearchFormTest2Tpl');
          
          
          $output = $modx->getChunk($formTpl);
          
          $pprofileTwo = $modx->getOption('user_search_profileTwo', $_POST, '');
          $pprofileThree = $modx->getOption('user_search_profileThree', $_POST, '');
          $pprofileFour = $modx->getOption('user_search_profileFour', $_POST, '');
          
          $modx->setPlaceholder('user_search_profileTwo', $pprofileTwo);
          $modx->setPlaceholder('user_search_profileThree', $pprofileThree);
          $modx->setPlaceholder('user_search_profileFour', $pprofileFour);
          
          
          $fields = array();
          
          if (isset($_POST['submit-var']) && $_POST['submit-var'] == 'etaoinshrdlu') {
          
              $fields['where'] = '{"profileTwo:=":"' . $pprofileTwo . '","AND:profileThree:=":"' . $pprofileThree . '","AND:profileFour:=":"' . $pprofileFour . '"}';
          
              $results = $modx->runSnippet('GetExtUsers', $fields);
          
          }
          
          if (! empty ($results) ){
              $modx->SetPlaceholder('user_search.results_heading',
                  $modx->lexicon('ce_user_search_results_heading'));
              $modx->setPlaceholder('user_search.results', $results);
          }
          return $output;


          UserSearchFormTest2Tpl Chunk
          <h3>User Search</h3>
          
          <p>Enter a user first and/or last name to search for, then click on Submit</p>
          
          <form class="ext-user-search-form" id="ext-user-search-form" method="post">
              <input type="hidden" name="submit-var" value="etaoinshrdlu"/>
          
          <div>        <input type="hidden" id="user_search_profileTwoHidden"
                          name="user_search_profileTwo" value="x"/>        
                          <input type="checkbox" id="user_search_profileTwo"
                          name="user_search_profileTwo" value="1"/>
                  <label for="user_search_profileTwo">[[%user_search_profileTwo_caption? &namespace=`classextender` &topic=`default`]]<br/>
              </label>
          </div>
          
          <div>
                  <input type="hidden" id="user_search_profileThreeHidden"
                          name="user_search_profileThree" value="x"/>        
                                  <input type="checkbox" id="user_search_profileThree"
                          name="user_search_profileThree"  value="1"/>
          				<label for="user_search_profileThree">[[%user_search_profileThree_caption? &namespace=`classextender` &topic=`default`]]<br/>
          <br/>
              </label>
          </div>
          
          <div>
                  <input type="hidden" id="user_search_profileFourHidden"
                          name="user_search_profileFour" value="x"/>        
                                  <input type="checkbox" id="user_search_profileFour"
                          name="user_search_profileFour"  value="1"/>
          				<label for="user_search_profileFour">[[%user_search_profileFour_caption? &namespace=`classextender` &topic=`default`]]<br/>
          <br/>
              </label>
          </div>
          
          
              <br/>
              <input type="submit" value="Submit">
          
          </form>
          
          <h3>[[+user_search.results_heading]]</h3>
          
          [[+user_search.results]]
          


          core/components/classextender/lexicon/en/default.inc.php
          $_lang['user_search_profileTwo_caption'] = 'Agility';
          $_lang['user_search_profileThree_caption'] = 'Flyball';
          $_lang['user_search_profileFour_caption'] = 'Rally';
            If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

            email: [email protected] | website: https://andytough.com
            • 3749
            • 24,544 Posts
            I think the problem is that you've set things up so that the DB value has to be exactly equal to the $_POST value for all three options. The solution (and this will be faster) is to create your 'where' array on the fly so it only looks for the options that are checked.

            Something like this (untested):

            $options = array(
                'user_search_profileTwo',
                'user_search_profileThree',
                'user_search_profileFour',
            );
            
            $search = array();
            foreach ($options as $option) {
               $val = $modx->getOption($option, $_POST, '');
               if (!empty($val)) {
                   $search[$option] = '1';
               }
            }
            
            $fields['where'] = $modx->toJSON($search);


            Now if only two fields are checked, it will only look at those two fields and ignore the third. If one is checked, it will check only that field and ignore all others.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 38783
              • 571 Posts
              Thank you Bob. Rushing back upstairs to my computer now to try this out.
                If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

                email: [email protected] | website: https://andytough.com
                • 3749
                • 24,544 Posts
                To answer my own trivia question,

                1. 'etaoinshrdlu' is (or was) the most commonly used letters in English text, in the order of their frequency.

                2. Etaoin Shrdlu is the name of the fictitious student created by humorist Max Max Shulman (whose work I recommend) and his friends while attending the University of Minnesota. They chipped in for Etaoin's tuition, and took turns taking his exams. Sadly, Shulman was expelled for the prank by a rather humor-impaired administration.

                Etaoin was also a character in Shulman's satiric novel Barefoot Boy with Cheek.
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 38783
                  • 571 Posts
                  Hello Bob,

                  I think I am being a bit slow here. If you have time could you elaborate a little by showing me where this code would go and what, if anything it would replace?

                  Should I be removing the default value of x for an unchecked box from my html?

                  <div>
                          <input type="hidden" id="user_search_profileFourHidden"
                                  name="user_search_profileFour" value="x"/>        
                                          <input type="checkbox" id="user_search_profileFour"
                                  name="user_search_profileFour"  value="1"/>
                                  <label for="user_search_profileFour">[[%user_search_profileFour_caption? &namespace=`classextender` &topic=`default`]]<br/>
                  <br/>
                      </label>
                  </div>


                  Thank you,

                  Andy
                    If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

                    email: [email protected] | website: https://andytough.com
                    • 3749
                    • 24,544 Posts
                    I think the code I posted would replace this line:

                    $fields['where'] = '{"profileTwo:=":"' . $pprofileTwo . '","AND:profileThree:=":"' . $pprofileThree . '","AND:profileFour:=":"' . $pprofileFour . '"}';


                    I think you can leave the X there, but I'm not positive.
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 38783
                      • 571 Posts
                      I think I am getting somewhere now!

                      I have removed the x from the html so that an unchecked box does return an empty result.

                      With the code below in place for the snippet, if I do a search where I check the boxes for profileTwo and profileFour I get the following array sent. The problem I have is that it is still returning no users. But it should be returning Test User as they do all three activities.

                      Array ( [where] => {"user_search_profileTwo":"1","user_search_profileFour":"1"} )


                      Does that array shed any light on what I need to change in the following code? (You can see in the code where I have placed the 'print array' code.

                      $formTpl = $modx->getOption('extFormTpl', $scriptProperties, 'ExtUserSearchFormTest2Tpl');
                      
                      
                      $output = $modx->getChunk($formTpl);
                      
                      $pprofileTwo = $modx->getOption('user_search_profileTwo', $_POST, '');
                      $pprofileThree = $modx->getOption('user_search_profileThree', $_POST, '');
                      $pprofileFour = $modx->getOption('user_search_profileFour', $_POST, '');
                      
                      $modx->setPlaceholder('user_search_profileTwo', $pprofileTwo);
                      $modx->setPlaceholder('user_search_profileThree', $pprofileThree);
                      $modx->setPlaceholder('user_search_profileFour', $pprofileFour);
                      
                      
                      $fields = array();
                      
                      if (isset($_POST['submit-var']) && $_POST['submit-var'] == 'etaoinshrdlu') {
                      /*   $fields['where'] = '{"profileTwo:=":"' . $pprofileTwo . '","OR:profileThree:=":"' . $pprofileThree . '","OR:profileFour:=":"' . $pprofileFour . '"}';*/
                      /*NEW */
                      $options = array(
                          'user_search_profileTwo',
                          'user_search_profileThree',
                          'user_search_profileFour',
                      );
                       
                      $search = array();
                      foreach ($options as $option) {
                         $val = $modx->getOption($option, $_POST, '');
                         if (!empty($val)) {
                             $search[$option] = '1';
                         }
                      }
                       
                      $fields['where'] = $modx->toJSON($search);
                      /* /NEW */
                      
                          $results = $modx->runSnippet('GetExtUsers', $fields);
                      
                      }
                      echo '';
                      print_r ($fields);
                      echo '';
                      if (! empty ($results) ){ 
                          $modx->SetPlaceholder('user_search.results_heading',
                              $modx->lexicon('ce_user_search_results_heading'));
                          $modx->setPlaceholder('user_search.results', $results);
                      }
                      
                      
                      return $output;
                        If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

                        email: [email protected] | website: https://andytough.com