We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45118
    • 123 Posts
    Quote from: BobRay at Dec 21, 2015, 02:59 PM
    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.
    Hmmm... that's not really what I was going for, but I'll give it a thought.
    When the page reloads, are the previously selected checkboxes still checked?
    Yes! All three of them, even though the error report shows only two in the array.
      • 3749
      • 24,544 Posts
      That's baffling. I can't think of a reason for it.
        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 22, 2015, 11:38 AM
        That's baffling. I can't think of a reason for it.
        It seems that the snippet isn't dealing well with the OR condition
        $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);
        }
        if ((!empty($projArray)) && (!empty($groupArray))) {
            $c->where(array($groupArray, $projArray));
        } elseif (!empty($projArray)) {
            $c->where(array($projArray));
        } elseif (!empty($groupArray)) {
            $c->where(array($groupArray));
        }

        So I looked back at my two previous posts (https://forums.modx.com/thread/98657/issue-with-checking-more-than-one-checkboxes#dis-post-533479 and https://forums.modx.com/thread/98676/making-checkboxes-sticky#dis-post-533582) and rewrote the snippet with the info BobRay suggested initially (https://forums.modx.com/thread/98657/issue-with-checking-more-than-one-checkboxes#dis-post-533487).
        Now it all works exactly the way I wanted it. smiley

        For this test I used these two groups of checkboxes:
        <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 Dimensions from 0" to 9":</p>
        <div id="checkb">
           <input type="checkbox" name="proj[]" value="0" [[+0_checked]] id="checkb0" onChange="this.form.submit();" />
           <label for="checkb0">0"</label> 
           <input type="checkbox" name="proj[]" value="1" [[+1_checked]] id="checkb1" onChange="this.form.submit();" />
           <label for="checkb1">1"</label> 
           <input type="checkbox" name="proj[]" value="2" [[+2_checked]] id="checkb2" onChange="this.form.submit();" />
           <label for="checkb2">2"</label> 
           <input type="checkbox" name="proj[]" value="3" [[+3_checked]] id="checkb3" onChange="this.form.submit();" />
           <label for="checkb3">3"</label> 
           <input type="checkbox" name="proj[]" value="4" [[+4_checked]] id="checkb4" onChange="this.form.submit();" />
           <label for="checkb4">4"</label> 
           <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="6" [[+6_checked]] id="checkb6" onChange="this.form.submit();" />
           <label for="checkb6">6"</label> 
           <input type="checkbox" name="proj[]" value="7" [[+7_checked]] id="checkb7" onChange="this.form.submit();" />
           <label for="checkb7">7"</label> 
           <input type="checkbox" name="proj[]" value="8" [[+8_checked]] id="checkb8" onChange="this.form.submit();" />
           <label for="checkb8">8"</label> 
           <input type="checkbox" name="proj[]" value="9" [[+9_checked]] id="checkb9" onChange="this.form.submit();" />
           <label for="checkb9">9"</label>
        </div>
        </form>
        And the new snippet including the diagnostic code to see the content of the two arrays in the error log:
        $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');
         
        $groupArray = array();
        foreach($group as $selected) {
                $groupArray[] = array('pre' => $selected);
                $modx->setPlaceholder($selected . '_checked', $checked);
        }
        $projArray = array();
        foreach($proj as $select) {
                $projArray[] = array('proj:LIKE' => $select.'.'.'%');
                $modx->setPlaceholder($select . '_checked', $checked);
        }
        
        if ((!empty($projArray)) && (!empty($groupArray))) {
            $c->where(array($groupArray, $projArray), xPDOQuery::SQL_OR);
        } elseif (!empty($projArray)) {
            $c->where(array($projArray), xPDOQuery::SQL_OR);
        } elseif (!empty($groupArray)) {
            $c->where(array($groupArray), xPDOQuery::SQL_OR);
        }
         
        $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);
        }
        
        $msg1 = "\ngroupArray" . print_r($groupArray, true);
        $modx->log(modX::LOG_LEVEL_ERROR, $msg1);
        
        $msg2 = "\nprojArray" . print_r($projArray, true);
        $modx->log(modX::LOG_LEVEL_ERROR, $msg2);
        
        return $output;
        And lastly here is what the error log shows regarding the projArray:

        [2015-12-23 16:06:12] (ERROR @ /index.php)
        projArrayArray
        (
        [0] => Array
        (
        [proj:LIKE] => 7.%
        )

        [1] => Array
        (
        [proj:LIKE] => 8.%
        )

        [2] => Array
        (
        [proj:LIKE] => 9.%
        )

        )
          • 3749
          • 24,544 Posts
          Now it all works exactly the way I wanted it. smiley

          I'm glad you got it sorted. smiley
            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