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 product spec page I want to show "family members" of this product. All the members of a family have the same code, for instance 123.

    I tried the following but it doesn't work (yet…). Apart from passing the product id in the url to the spec page I also pass the family code, like this:
    <a href="[[~27? &prodNum=`[[+name]]` &famCode=`[[+fam]]`]]">

    Then in the snippet I'm trying to get the names of the products in this family, like this:
    $famId = $_GET['famCode'];
    
    $families = $modx->getCollection('BbProducts',$famId);
    foreach($families as $family) {
        $imgName = $family->get('name');
        $nwN = 'Name: ' . $imgName;
        $imgLink = 'assets/media/jpgs/' . $imgName . '.jpg';
    
        $fields = $family->toArray();
        $output = $modx->getChunk('showProductFam', array_merge($fields, array('nwN'=>$nwN, 'imgLink'=>$imgLink)));
    }

    And the showProductFam chunk looks something like this:
    <div>
    <img title="[[+name]]" src="[[+imgLink]]" alt="[[+name]]" /><br>
    [[+nwN]]
    </div>

    Am I going in the right direction? All help is very much appreciated!

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

    [ed. note: redhen last edited this post 11 years, 4 months ago.]
      • 47635
      • 33 Posts
      $families = $modx->getCollection('BbProducts', array('fam' => $famId));

      https://rtfm.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.getcollection
        • 45118
        • 123 Posts
        Quote from: yasya_popkin at May 08, 2015, 12:56 PM
        $families = $modx->getCollection('BbProducts', array('fam' => $famId));

        https://rtfm.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.getcollection

        Sorry, I don't know what you mean by this. Could you please explain your answer? (I think this only gives me the family code.)
          • 47635
          • 33 Posts
          $families = $modx->getCollection('BbProducts',$famId);

          What type of the second parameter? Which (SQL-)query you expect?
            • 45118
            • 123 Posts
            Quote from: yasya_popkin at May 08, 2015, 02:20 PM
            $families = $modx->getCollection('BbProducts',$famId);

            What type of the second parameter? Which (SQL-)query you expect?
            I'm looking for all the products that have the same family code.
              • 47635
              • 33 Posts
              How MODX will understand that you want to filter objects on the field 'fam'?
              RTFM.
                • 46886
                • 1,154 Posts
                You are trying to do this in a high tech way, so I can't help you a lot, but the problem is where this value of family id value is and how Modx can recognize it (or how it is provided in the db when you are doing your db search for related products.

                A low tech way to deal with this would be to put the code into the long title or description of each resource and use getRelated to sort by long title or description.

                A better way would be to attach a special characteristic to each resource (probably as a tv but I am not sure) which would hold this value and allow you to then sort using this value by using a tool such as pdoTools or getResource.

                For instance, on my site we sort using pdoTools to get latest resources and also use a special tv for the number of page views so we can sort in this way as well.

                HTH
                • discuss.answer
                  • 3749
                  • 24,544 Posts
                  Your code looks close to me.

                  This will work *if* the $famId variable contains the value of the primary key for that object. If that value is an integer, put (int) in front of it like this:

                  $families = $modx->getCollection('BbProducts', (int) $famId);


                  If it's not the primary key you'd have to use something like Yasya_popkin's code (replace 'fam' with the field name of the field holding that value, and again, it it's an integer field, but (int) in front of it):

                  $families = $modx->getCollection('BbProducts', array('fam' => (int) $famId));


                  You shouldn't need the array_merge() since $fields already holds the correct array, but you need to use .= rather than =, otherwise you'll only get the last one.

                  $output .= $modx->getChunk('showProductFam', $fields);


                  You should put this at the very top. It's good form and will prevent E_NOTICE errors:

                  $output = "";




                    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 May 10, 2015, 04:33 PM
                    Your code looks close to me.

                    This will work *if* the $famId variable contains the value of the primary key for that object. If that value is an integer, put (int) in front of it like this:

                    $families = $modx->getCollection('BbProducts', (int) $famId);


                    If it's not the primary key you'd have to use something like Yasya_popkin's code (replace 'fam' with the field name of the field holding that value, and again, it it's an integer field, but (int) in front of it):

                    $families = $modx->getCollection('BbProducts', array('fam' => (int) $famId));


                    You shouldn't need the array_merge() since $fields already holds the correct array, but you need to use .= rather than =, otherwise you'll only get the last one.

                    $output .= $modx->getChunk('showProductFam', $fields);


                    You should put this at the very top. It's good form and will prevent E_NOTICE errors:

                    $output = "";



                    Thanks Bob! Your explanation helped me make it work.
                    It's a shame yasya_popkin has to resort to using expletives (see reply #6) instead of giving an explanation.

                    Anyway...

                    This is what I ended up with:
                    $famId = $_GET['famCode'];
                     
                    $families = $modx->getCollection('BbProducts', array('fam' => (int) $famId));
                    foreach($families as $family) {
                        $imgName = $family->get('name');
                        $nwN = 'Name: ' . $imgName;
                        $imgLink = 'assets/media/jpgs/' . $imgName . '.jpg';
                     
                        $fields = $family->toArray();
                        $output .= $modx->getChunk('showProductFam', array_merge($fields, array('nwN'=>$nwN, 'imgLink'=>$imgLink)));
                    }

                    I tested it without (int) in front of the variable $famId. The result was that the products that did not have any family members had all the products that also didn't have family members listed as family... So now I understand why to put (int) in front of it.
                    Also I do need the array_merge() to have the calculated values show up.

                    I do have a follow-up question: How can I sort this list of family members? I want to sort them by their name.

                    Thanks again for your help!
                    [ed. note: redhen last edited this post 11 years, 4 months ago.]
                    • discuss.answer
                      • 3749
                      • 24,544 Posts
                      I didn't notice the calculations.

                      I would do something like this rather than array_merge(), it should be slightly faster and I think the code is a little easier to follow:

                      
                      $fields = $family->toArray();
                      $imgName = $family->get('name');
                      $fields['nwN'] = 'Name: ' . $imgName;
                      $fields['imgLink'] = 'assets/media/jpgs/' . $imgName . '.jpg';
                      $output .= $modx->getChunk('showProductFam', $fields);


                      For sorting:

                      $c = $modx->newQuery('BbProducts');
                      $c->where(array('fam' => (int) $famId));
                      $c->sortby('name', 'ASC');
                      $families = $modx->getCollection('BbProducts', $c);
                        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