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 my page I have a form like this:
    <div>[[+total]] Products</div>
    <form action="[[~[[*id]]]]" method="POST">
    <p>Choose a Group or Groups:</p>
    <div id="check">
       <input type="checkbox" name="group[]" id="check1" value="BRA" />
       <label for="check1">Brackets</label> 
       <input type="checkbox" name="group[]" id="check2" value="BLK" />
       <label for="check2">Blocks</label> 
       <input type="checkbox" name="group[]" id="check3" value="COR" />
       <label for="check3">Corbels</label> 
       <input type="checkbox" name="group[]" id="check4" value="GBR" />
       <label for="check4">Gingerbreads</label>
       <input type="submit" name="submit" Value="Submit"/>
    </div>
    </form>

    And a snippet to show the products that are selected:
    <?php
    $output = "";
    
    $path = MODX_CORE_PATH . 'components/aeibbproducts/';
    $result = $modx->addPackage('aeibbproducts',$path .
        'model/','aei_');
    
    if (! $result) {
        return 'failed to add package';
    }
    
    if (isset($_POST['group']) && (! empty($_POST['group']))) {
        $group = $_POST['group'];
    }
    
    $c = $modx->newQuery('BbProducts');
    
    foreach($group as $selected) {
    $c->where(array('pre' => $selected));
    }
    
    $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;

    When the page opens, all the products are displayed. When I check one box, for instance Gingerbreads and click Submit, all the gingerbreads show. So that works fine. The problem is when I check two boxes, for instance Corbels and Gingerbreads, no products show up. I can't figure out what I'm doing wrong.

    Any help would be greatly appreciated!

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      This code is resetting the where array to the last match:

      foreach($group as $selected) {
          $c->where(array('pre' => $selected));
      }


      You want something like this (untested):

      $where = array();
      foreach($group as $selected) {
              $where[] = array('pre' => $selected);
      }
      
      $query->where($where, xPDOQuery::SQL_OR);
        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
      • Quote from: BobRay at Oct 21, 2015, 10:39 PM
        This code is resetting the where array to the last match:

        Not true, all where conditions are combined, but by default they are combined by AND. That is the reason for the empty result.

        $i = 0
        foreach($group as $selected) {
            $condition = ($i) ? 'OR:' : '';
            $c->where(array($condition.'pre' => $selected));
            $i++;
        }
        


        This should work too (untested).
          • 45118
          • 123 Posts
          Quote from: BobRay at Oct 21, 2015, 10:39 PM

          You want something like this (untested):

          $where = array();
          foreach($group as $selected) {
                  $where[] = array('pre' => $selected);
          }
          
          $query->where($where, xPDOQuery::SQL_OR);

          Hi Bob,

          I tested this (changed $query to $c) and it works, but it does get an error saying "Invalid argument supplied for foreach()". I'm not sure how to correct this.
            • 45118
            • 123 Posts
            Quote from: Jako at Oct 21, 2015, 11:28 PM

            $i = 0
            foreach($group as $selected) {
                $condition = ($i) ? 'OR:' : '';
                $c->where(array($condition.'pre' => $selected));
                $i++;
            }
            


            This should work too (untested).

            Hi Jako,

            I tested this but the page comes up empty. The error log says "PHP Parse error: syntax error, unexpected 'foreach' (T_FOREACH)".
              • 3749
              • 24,544 Posts
              That error usually means that at some point, it's being called where $group is not an array. What is the code that's setting the $group variable?


                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
              • That happens if no option is selected during a form post ($_POST['group'] is empty then)

                if (isset($_POST['group']) && (! empty($_POST['group']))) {
                    $group = $_POST['group'];
                } else {
                    $group = array();
                }
                


                Should fix that.
                • Quote from: redhen at Oct 22, 2015, 02:26 PM

                  The error log says "PHP Parse error: syntax error, unexpected 'foreach' (T_FOREACH)".

                  There is a semicolon missing after the $i = 0 in my code.
                    • 45118
                    • 123 Posts
                    Quote from: BobRay at Oct 22, 2015, 08:17 PM
                    That error usually means that at some point, it's being called where $group is not an array. What is the code that's setting the $group variable?
                    Hi Bob, you're right! I didn't set the $group variable... I added this and no error message anymore!
                    $group = array();
                    Thanks!
                      • 45118
                      • 123 Posts
                      Quote from: Jako at Oct 22, 2015, 09:26 PM
                      That happens if no option is selected during a form post ($_POST['group'] is empty then)
                      if (isset($_POST['group']) && (! empty($_POST['group']))) {
                          $group = $_POST['group'];
                      } else {
                          $group = array();
                      }
                      
                      Should fix that.
                      Hi Jako,
                      Which is better, yours or this?
                      $group = array();
                      if (isset($_POST['group']) && (! empty($_POST['group']))) {
                          $group = $_POST['group'];
                      }
                      They both work...
                      Thanks