We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42239
    • 73 Posts
    Trying to get random child ids based on the assigned parent. Snippet works great except it doesn't limit and doesn't produce it randomly.

    <?php
    //****** $group is a hook pulled from tv in snippet on page ******
    
    $c = $modx->newQuery('modResource');
    
    $c->sortby('RAND()');
    $c->limit(3);
    
    $children = $modx->getChildIds('5',$c);
    
    foreach($children as $item) {
         $values = array('id' => $item);
    
    	 $page = $modx->getObject('modResource', $item);
    	 $list = $page->getTVValue('directoryInclude');
    
    	 if (strpos($list, $group)) {
    	 	$output .= $modx->parseChunk('specificList', $values, '[+', '+]');
    	 }else {
    	 }
    }
    
    return $output;


    Thanks for the help.
      • 3749
      • 24,544 Posts
      It's not so much your code as your choice of functions. wink

      getChildIds() is not an xPDO method, so it doesn't understand criteria.

      Try this instead (with the same $c):

      $doc = $modx->getObject('modResource', 5);
      
      if ($doc) {
          $children = $doc->getMany('Children, $c);
          // the rest of your code
      } else {
          return 'Resource 5 not found';
      }
      

        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
        • 42239
        • 73 Posts
        Quote from: BobRay at Aug 22, 2013, 05:16 PM
        It's not so much your code as your choice of functions. wink

        Just finally getting back to this issue.

        It should look like this?

        <?php
        //****** $group is a hook pulled from tv in snippet on page ******
         
        $c = $modx->newQuery('modResource');
         
        $c->sortby('RAND()');
        $c->limit(3);
         
        $doc = $modx->getObject('modResource', 5);
         
        if ($doc) {
            $children = $doc->getMany('Children', $c);
        foreach($children as $item) {
             $values = array('id' => $item);
         
             $page = $modx->getObject('modResource', $item);
             $list = $page->getTVValue('directoryInclude');
         
             if (strpos($list, $group)) {
                $output .= $modx->parseChunk('specificList', $values, '[+', '+]');
             }else {
             }
        }
         
        return $output;
        
        } else {
            return 'Resource 5 not found';
        }
        
        
        with this I get an error:
        Call to undefined method modDocument_mysql::prepare()
        
        
         
          • 3749
          • 24,544 Posts
          Children is an array of Resource objects so $item is a Resource Object. Your problem is the $values = line and the line after it. It's setting the 'id' to an object rather than an integer and trying to use that as an ID in the getObject() call. Also, you don't need to get the child resource with getObject() -- you already have it.

          I assume you're setting $group somewhere earlier.

          Try this:


          $c = $modx->newQuery('modResource');
          $output = '';
          $c->sortby('RAND()');
          $c->limit(3);
          
          $doc = $modx->getObject('modResource', 5);
          
          if ($doc) {
              $children = $doc->getMany('Children', $c);
              foreach($children as $item) {
                  $list = $item->getTVValue('directoryInclude');
                  if (strpos($list, $group)) {
                     $output .= $modx->parseChunk('specificList', $values, '[+', '+]');
                  }
              }
          } else {
              $output = 'Resource 5 not found';
          }
          
          return $output;


          BTW, if you're using Revolution, the placeholders in the chunk would normally be in this form, in which case you wouldn't need the prefix and suffix and things would be a little faster:

          [[+PlaceholderName]]


          The line would then be:

          $output .= $modx->getChunk('specificList', $values);


          If you're not using Revolution, none of this is going to work. wink
            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