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

    I have two groups of checkboxes. With one the user can select a group (one or more) of products and with the other one the user can select a projection dimension (one or more), like this:
    <div>[[+total]] Products</div>
    <form action="[[~[[*id]]]]" method="POST">
    <p>Choose a Group or Groups:</p>
    <div id="checka">
       <input type="checkbox" name="group[]" value="BRA" [[+BRA_checked]] id="checka1" onChange="this.form.submit();" />
       <label for="checka1">Brackets</label> 
       <input type="checkbox" name="group[]" value="BLK" [[+BLK_checked]] id="checka2" onChange="this.form.submit();" />
       <label for="checka2">Blocks</label> 
       <input type="checkbox" name="group[]" value="COR" [[+COR_checked]] id="checka3" onChange="this.form.submit();" />
       <label for="checka3">Corbels</label> 
       <input type="checkbox" name="group[]" value="GBR" [[+GBR_checked]] id="checka4" onChange="this.form.submit();" />
       <label for="checka4">Gingerbreads</label> 
       <input type="checkbox" name="group[]" value="WBR" [[+WBR_checked]] id="checka5" onChange="this.form.submit();" />
       <label for="checka5">Wood</label>
    </div>
    <p>Select Projection:</p>
    <div id="checkb">
       <input type="checkbox" name="proj[]" value="5" [[+5_checked]] id="checkb5" onChange="this.form.submit();" />
       <label for="checkb5">5"</label> 
       <input type="checkbox" name="proj[]" value="9" [[+9_checked]] id="checkb9" onChange="this.form.submit();" />
       <label for="checkb9">9"</label> 
       <input type="checkbox" name="proj[]" value="12" [[+12_checked]] id="checkb12" onChange="this.form.submit();" />
       <label for="checkb12">12"</label>
    </div>
    </form>

    And this is the snippet to make it work:
    $output = "";
    
    $path = MODX_CORE_PATH . 'components/aeibbproducts/';
    $result = $modx->addPackage('aeibbproducts',$path .
        'model/','aei_');
    
    if (! $result) {
        return 'failed to add package';
    }
    
    $proj = array();
    if (isset($_POST['proj']) && (! empty($_POST['proj']))) {
        $proj = $_POST['proj'];
    }
    
    $group = array();
    if (isset($_POST['group']) && (! empty($_POST['group']))) {
        $group = $_POST['group'];
    }
    
    $checked = 'checked="checked"';
    
    $c = $modx->newQuery('BbProducts');
    
    $i = 0;
    foreach($group as $selected) {
        $condition = ($i) ? 'OR:' : '';
        $c->where(array($condition.'pre:=' => $selected));
        $i++;
        $modx->setPlaceholder($selected . '_checked', $checked);
    }
    
    $j = 0;
    foreach($proj as $select) {
        $cond = ($j) ? 'OR:' : '';
        $c->where(array($cond.'proj:LIKE' => $select.'%'));
        $j++;
        $modx->setPlaceholder($select . '_checked', $checked);
    }
    
    $total = $modx->getCount('BbProducts',$c);
    $totalVar = $modx->getOption('totalVar', $scriptProperties, 'total');
    $modx->setPlaceholder($totalVar,$total);
    
    $modx->runSnippet('FeetInch');
    $fi = new FeetInch($modx);
    
    $noImage ='assets/media/jpgs/noimage-s.jpg';
    $products = $modx->getCollection('BbProducts',$c);
    foreach($products as $product) {
        $fields = $product->toArray();
        $width = $product->get('width');
        $fields['nwW'] = 'Width: ' . $fi->dec2inch($width);
        $height = $product->get('height');
        $fields['nwH'] = 'Height: ' . $fi->dec2inch($height);
        $proj = $product->get('proj');
        $fields['nwP'] = 'Proj: ' . $fi->dec2inch($proj);
        $pitch = $product->get('pitch');
        $fields['nwPi'] = $pitch . ' Pitch';
        $fleName = $product->get('aei');
        $temp = 'assets/media/jpgs/prod-s/' . '01-' . $fleName . '.jpg';
        $fields['imgLink'] = file_exists($temp) ? $temp : $noImage;
        $output .= $modx->getChunk('showBrackets', $fields);
    }
    return $output;

    The problems start when I want to select for instance Blocks and Corbels with a projection of 5". The result I get is Blocks with a projection of 5" and all Corbels. It seems it can't deal with a second selection as soon as an checkbox from the other group gets checked also.

    Hopefully somebody can help me figure this out. All help is very much appreciated!



    This question has been answered by BobRay. See the first response.

      • 3749
      • 24,544 Posts
      That really looks like it should work. It may work to create the two arrays as separate arrays before creating the where() statement

      $i = 0;
      $groupArray = array();
      foreach($group as $selected) {
          $condition = ($i) ? 'OR:' : '';
          $groupArray[$condition.'pre:='] =  $selected;
          $i++;
          $modx->setPlaceholder($selected . '_checked', $checked);
      }
       
      $j = 0;
      $projArray = array();
      foreach($proj as $select) {
          $cond = ($j) ? 'OR:' : '';
          $projArray[$cond.'proj:LIKE'] = $select.'%';
          $j++;
          $modx->setPlaceholder($select . '_checked', $checked);
      }
      
      $c->where(array($groupArray, $projArray));
      



        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
        • 45118
        • 123 Posts
        Quote from: BobRay at Dec 16, 2015, 02:24 AM
        That really looks like it should work. It may work to create the two arrays as separate arrays before creating the where() statement

        $i = 0;
        $groupArray = array();
        foreach($group as $selected) {
            $condition = ($i) ? 'OR:' : '';
            $groupArray[$condition.'pre:='] =  $selected;
            $i++;
            $modx->setPlaceholder($selected . '_checked', $checked);
        }
         
        $j = 0;
        $projArray = array();
        foreach($proj as $select) {
            $cond = ($j) ? 'OR:' : '';
            $projArray[$cond.'proj:LIKE'] = $select.'%';
            $j++;
            $modx->setPlaceholder($select . '_checked', $checked);
        }
        
        $c->where(array($groupArray, $projArray));
        



        Hi Bob,
        This works (yeah!! smiley) but now another problem appears: when I select a projection dimension first, I get an empty result. When I select a group first and then a proj dim, it works fine. When I switch the arrays in the where() statement (like below), selecting a group first gets an empty result.
        $c->where(array($projArray, $groupArray));
        I'm not sure if that tells you anything... Thanks again for your help!
        • discuss.answer
          • 3749
          • 24,544 Posts
          I can't believe that worked on the first try. wink

          I don't see why it would be needed, but you might try this instead of line 19:

          if ( (!empty($projArray)) && (!empty($groupArray)) ) {
              $c->where(array($groupArray, $projArray));
          } elseif (!empty($projArray)) {
            $c->where(array($projArray));
          } elseif (!empty($groupArray)) {
            $c->where(array($groupArray));
          }
            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
            • 45118
            • 123 Posts
            Quote from: BobRay at Dec 16, 2015, 03:21 PM
            I can't believe that worked on the first try. wink

            I don't see why it would be needed, but you might try this instead of line 19:

            if ( (!empty($projArray)) && (!empty($groupArray)) ) {
                $c->where(array($groupArray, $projArray));
            } elseif (!empty($projArray)) {
              $c->where(array($projArray));
            } elseif (!empty($groupArray)) {
              $c->where(array($groupArray));
            }
            This does the trick!! Thanks so much! smiley
              • 3749
              • 24,544 Posts
              Wow, I hit the daily double. smiley

              I'm glad I could help.
                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
                • 45118
                • 123 Posts
                Quote from: BobRay at Dec 17, 2015, 01:40 PM
                Wow, I hit the daily double. smiley

                I'm glad I could help.
                I'm sorry, but I celebrated too early! sad
                After some more testing I noticed I can't select more than two checkboxes. For instance in groups, when I check off blocks and corbels, it works fine. But as soon as I also check off gingerbreads it ignores it, or it ignores one of the other groups. It seems to depend on the order they are in.

                All help is greatly appreciated!
                  • 3749
                  • 24,544 Posts
                  Have you thought about leaving out all the onChange stuff and just putting a submit ("Search") buttons in the form? You could use multiple, identical submit buttons if you're worried about them scrolling off the page. I would think users would be annoyed by having the page reload every time they checked or unchecked a checkbox.

                  I'm not sure that would solve your problem, but it might.

                  As a diagnostic, I would try adding this code near the end:

                  $msg = "progArray" . print_r($progArray, true);
                  $msg = "\ngroupArray" . print_r($groupArray, true);
                  $modx->log(modX::LOG_LEVEL_ERROR, $msg);
                  


                  That should let you look at the content of the two arrays in the error log. If you install the controlErrorLog extra, you can just click on the triangle at the top right of the menu to see the results.
                    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
                    • 45118
                    • 123 Posts
                    Quote from: BobRay at Dec 18, 2015, 02:21 PM
                    Have you thought about leaving out all the onChange stuff and just putting a submit ("Search") buttons in the form? You could use multiple, identical submit buttons if you're worried about them scrolling off the page. I would think users would be annoyed by having the page reload every time they checked or unchecked a checkbox.

                    I'm not sure that would solve your problem, but it might.
                    Actually having the page reload every time a users checks a checkbox is kind of the idea on this page. The user starts with ~2000 products and can whittle it down to their choice(s) by checking off a group or groups and a dimension or dimensions.
                    I did do a test with a submit button instead of onChange="this.form.submit();" but it didn't make a difference regarding the issue.

                    As a diagnostic, I would try adding this code near the end:

                    $msg = "progArray" . print_r($progArray, true);
                    $msg = "\ngroupArray" . print_r($groupArray, true);
                    $modx->log(modX::LOG_LEVEL_ERROR, $msg);
                    

                    That should let you look at the content of the two arrays in the error log. If you install the controlErrorLog extra, you can just click on the triangle at the top right of the menu to see the results.
                    I can only get this code to work for one of the arrays at the time but nevertheless it shows what's happening:
                    [2015-12-21 16:07:17] (ERROR @ /index.php) 
                    projArrayArray
                    (
                    )
                    [2015-12-21 16:07:45] (ERROR @ /index.php) 
                    projArrayArray
                    (
                        [proj:LIKE] => 13.%
                    )
                    [2015-12-21 16:07:47] (ERROR @ /index.php) 
                    projArrayArray
                    (
                        [proj:LIKE] => 13.%
                        [OR:proj:LIKE] => 14.%
                    )
                    [2015-12-21 16:07:50] (ERROR @ /index.php) 
                    projArrayArray
                    (
                        [proj:LIKE] => 13.%
                        [OR:proj:LIKE] => 15.%
                    )
                      • 3749
                      • 24,544 Posts
                      Another way to go would be to show no products on the page until the user made a selection, but add another checkbox with 'All Products'. The initial page would load very fast and they could then make a full selection before hitting Submit.

                      When the page reloads, are the previously selected checkboxes still checked?
                        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