We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24629
    • 370 Posts
    Hi all,

    i'm trying to get this to work

    $output = '';
    
    $wistjedat = $modx->getObject('modResource',21);
    
    $criteria = $modx->newQuery('modResource');
    $criteria->where(array(
               'published' => 1,
               ));
    $weetjes  = $wistjedat->getMany('Children',$criteria);
    
    $rand = rand(0, count($weetjes)-1);
    $hit  = $weetjes[$rand];
    $output .= $hit->get('content');
    
    return $output;


    somehow it seems to fail on this line
    $output .= $hit->get('content');


    does anyone know why?

    tnx Ralph
      • 4172
      • 5,888 Posts
      is it working with this?

      <?php
      
      $output = '';
      
      $c = $modx->newQuery('modResource');
      $c->where(array('published' => 1, 'parent' => 21));
      if ($weetjes = $modx->getCollection('modResource', $c)) {
          $rand = rand(0, count($weetjes) - 1);
          $hit = $weetjes[$rand];
          $output .= $hit->getContent();
      }
      
      return $output;
      
      ?>
      
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 24629
        • 370 Posts
        Hi Bruno,

        no it does not. Only when i comment the line with $output .= $hit->getContent(); the page works
        also the $rand traces out the correct amount of records so $weetjes is not empty
        also the error log in Modx is empty
        but the page does not render

          • 26903
          • 1,336 Posts
          Try :-

          $values = array_values($weetjes);
          $hit = $values[$rand];
            Use MODx, or the cat gets it!
            • 26903
            • 1,336 Posts
            Yes, which is why the array_values should work as it will re-key the array from 0 wiil it not.
              Use MODx, or the cat gets it!
              • 3749
              • 24,544 Posts
              It might work, but I think this would be faster, since there's no need to re-key the array. This definitely works:

                  $key = array_rand($weetjes);
                  $output = $weetjes[$key]->getContent();
                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
                • 26903
                • 1,336 Posts
                Yes, but you can't chose your own rand function, you may not want to randomise as the PHP array_rand does.
                  Use MODx, or the cat gets it!
                  • 24629
                  • 370 Posts
                  Yes that works. tnx

                  i'm very new to php so do i want to understand this right.
                  why is this? i would expect $weetjes[i] to return something.

                  tnx

                  Ralph
                    • 26903
                    • 1,336 Posts
                    Only if i corresponds to one of the key values of the array, if the array keys are 1,4,6 and you index by one of these you'll get back a value, in your case an object, otherwise you'll get nothing. As the keys returned by a call to getCollection are foreign keys, you don't know what these are so if you want to use an index based on the count value of the array, say 10, so we have 0..9 you have to rekey it.

                    Normally you would foreach a collection so you wouldn't see this behaviour.
                      Use MODx, or the cat gets it!
                      • 24629
                      • 370 Posts
                      aha ,

                      capice, thank you very much.
                      i can go on

                      ralph