We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45118
    • 123 Posts
    On this particular product page I am showing products from two categories (database field is 'cat'), NIC and NIR. This is the code in my snippet that takes care of that:
    $c = $modx->newQuery('Products');
    $c->where(array('cat'=>'NIC','OR:cat:='=>'NIR'));


    I'm trying to add a choice option for the viewer to only see one or the other category. How can I filter on a 'VALUE'?

    Thanks for your help!

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

      • 3749
      • 24,544 Posts
      It sounds like you just need a dropdown, radio options, or checkboxes in a form. Make the where() statement based on how they're set.

      Unless you want to go all JavaScript and call a getList processor based on what the user has selected.

        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 Mar 06, 2014, 05:50 PM
        It sounds like you just need a dropdown, radio options, or checkboxes in a form. Make the where() statement based on how they're set.
        Hi Bob,

        I'm sorry, I think my question wasn't very well formulated... Let me try again: the default setting of my page has the products of the two categories on it (in this case NIC and NIR). There are radio buttons at the top of the page a visitor can use to sort by height and width. This all works nicely.

        Now I also want to give the visitor the option to view only the products from one category at the time and still be able to do the sorting. How to a add that to the functionality that's already on the page?

        Thanks for your help!
          • 3749
          • 24,544 Posts
          Do you have a user interface in mind?

          How do the current radio buttons work -- JavaScript or form/reload?
            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 Mar 07, 2014, 02:46 PM
            Do you have a user interface in mind?

            How do the current radio buttons work -- JavaScript or form/reload?

            Here's my snippet:
            <?php
            $path = MODX_CORE_PATH . 'components/table/';
            $result = $modx->addPackage('table',$path .
                'model/','pre_');
            
            if (! $result) {
                return 'failed to add package';
            }
            
            $modx->runSnippet('RadioButtons');
            
            $modx->runSnippet('FeetInch');
            $fi = new FeetInch($modx);
            
            $sortby = 'cat';
            $checked = 'checked="checked"';
            if (isset($_POST['sort_method']) && (! empty($_POST['sort_method']))) {
                $sortby = $_POST['sort_method'];
            }
            
            $modx->setPlaceholder($sortby . '_checked', $checked);
            
            $c = $modx->newQuery('Products');
            $c->sortby($sortby, 'ASC');
            $c->where(array('cat'=>'NIC','OR:cat:='=>'NIR'));
            
            $products = $modx->getCollection('Products',$c);
            foreach($products as $product) {
                .....
                }
                $fields = $product->toArray();
                $output .= $modx->getChunk('ShowNiches', array_merge($fields, array(.....)));
            }
            
            return $output;


            And this is the chunk for the buttons that do the sorting:
            <form action="[[~[[*id]]]]" method="POST">
            <p id="radio">
               <input type="radio" name="sort_method" id="radio4" value="cat" onChange="this.form.submit();" />
               <label for="radio4">Reset Sorting</label>
               <input type="radio" name="sort_method" [[+width_checked]] id="radio3" value="width" onChange="this.form.submit();" />
               <label for="radio3">Sort by Width</label>
               <input type="radio" name="sort_method" [[+height_checked]] id="radio2" value="height" onChange="this.form.submit();" />
               <label for="radio2">Sort by Height</label>
               <input type="radio" name="sort_method" [[+depth_checked]] id="radio1" value="depth" onChange="this.form.submit();" />
               <label for="radio1">Sort by Depth</label>
            </p>
            <p> </p>
            </form>


            The snippet puts all the products from category NIC and NIR on the page. Now I want to add to the page two more buttons. One to choose just the NIC products and one just the NIR. What I can't seem to figure out is how to work that into the existing snippet.
            • discuss.answer
              • 3749
              • 24,544 Posts

              With two checkboxes (Show NIR and Show NIC), the user could select either one or both.


              if ($nicCheckbox && (! $nirCheckbox)) {
                  $c->where(array('cat'=>'NIC'));
              } elseif ($nirCheckbox && (! $nicCheckbox)) {
                  $c->where(array('cat'=>'NIR'));
              } else { /* default, show both */
                  $c->where(array('cat'=>'NIC','OR:cat:='=>'NIR'));
              }
                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
                Thanks Bob, this works nicely!!

                I've incorporated your suggestion in my snippet like this:
                $sortby = 'itemno';
                if (isset($_POST['sort_method']) && (! empty($_POST['sort_method']))) {
                    $sortby = $_POST['sort_method'];
                }
                $checkbox1 = $_POST['check_cat1'];
                $checkbox2 = $_POST['check_cat2'];
                
                $c = $modx->newQuery('Products');
                $c->sortby($sortby, 'ASC');
                if ($checkbox1 && (! $checkbox2)) {
                    $c->where(array('cat'=>'BRA'));
                } elseif ($checkbox2 && (! $checkbox1)) {
                    $c->where(array('cat'=>'COR'));
                } else {
                    $c->where(array('cat'=>'BRA','OR:cat:='=>'COR'));
                }
                
                $checked = 'checked="checked"';
                $modx->setPlaceholder($sortby . '_checked', $checked);
                $modx->setPlaceholder($checkbox1 . '_checked', $checked);
                $modx->setPlaceholder($checkbox2 . '_checked', $checked);
                


                and added the toggle buttons at the top of the page. Here's the buttons chunk:
                <form action="[[~[[*id]]]]" method="POST">
                <p id="radio">
                   <input type="radio" name="sort_method" id="radio4" value="itemno" onChange="this.form.submit();" />
                   <label for="radio4">Reset</label>
                   <input type="radio" name="sort_method" [[+width_checked]] id="radio3" value="width" onChange="this.form.submit();" />
                   <label for="radio3">Sort by Width</label>
                   <input type="radio" name="sort_method" [[+height_checked]] id="radio2" value="height" onChange="this.form.submit();" />
                   <label for="radio2">Sort by Height</label>
                   <input type="radio" name="sort_method" [[+depth_checked]] id="radio1" value="depth" onChange="this.form.submit();" />
                   <label for="radio1">Sort by Depth</label>
                </p>
                <p> </p>
                
                <p id="check">
                   <input type="checkbox" name="check_cat1" [[+cat1_checked]] id="check1" value="cat1" onChange="this.form.submit();" />
                   <label for="check1">Category 1</label>
                   <input type="checkbox" name="check_cat2" [[+cat2_checked]] id="check2" value="cat2" onChange="this.form.submit();" />
                   <label for="check2">Category 2</label>
                </p>
                <p> </p>
                </form>


                The sorting buttons also work when only one category is chosen!! Very cool!

                Thanks for your help, very much appreciated.
                  • 3749
                  • 24,544 Posts
                  I'm glad it worked for you. Thanks for reporting back. 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