We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22019
    • 390 Posts
    This hurts, because it must be teeth-gratingly simple to do, but my PHP understanding is minimal, let alone MODx. I’m trying to replicate a randomQuote generator that appears elsewhere on the forums for Evo. It uses a deprecated call (getActiveChildren) so needs tweaking for Revo.

    I can find and loop through all resources I need:
    $quotes = $modx->getCollection('modResource', array(
     'parent' => $foldernumber,
    'published' => 1 ));
    
    foreach ($quotes as $quote) {
    echo $quote->get('introtext')." + ";
    }
    


    I can shuffle them, but I can’t seem to work out how to call an individual row of the array correctly.

    I thought it would be this:
    echo $quotes[$somenumber]['introtext']


    but it doesn’t seem to recognise the column heading ’introtext’ - I’m not referencing it correctly. I can’t find any examples that just want one item from a collection - or an example to output the contents of the whole array (ie what’s actually _in_ my $quotes variable), so I can’t easily see where I’m going wrong.

    Help a noob out....


      Writer > E-consultant > MODx developer || Salesforce || modx 2.x || PHP 5.2.13 || MySQL client 5.0.86
      • 1778
      • 659 Posts
      to print the content of the whole array you can do
      print_r($quotes);
      
        • 22303 MODX Staff
        • 10,725 Posts
        That would print out the while xPDOObject, which you do not want to do.

        It would actually be:
        echo $quotes[$primaryKey]->get('introtext');

        But that requires you know the actual id of the Resource.

        You know you can do random quotes with getResources? &sortby=`RAND()` &limit=`1` ... ?
          • 22019
          • 390 Posts
          That would print out the while xPDOObject, which you do not want to do.

          Yes. As I just discovered....

          But that requires you know the actual id of the Resource.

          Ahhh. I see. I had assumed that it wanted the number of the row number within the returned array. And thanks for your absurdly simple solution.

          Could I just impose for two more quickies - as I’m almost certainly going to back myself into this corner again.

          (1) The primary_key being the resource ID comes from the AccessibleSimpleObject then does it? (ie because the object I had created was a specific Object type, the array I had created wasn’t indexed by row number).

          and (2) if I wanted to continue my original experiment - In order to find the resource ID, I would have to retrieve all the children IDs into a non-object array, and then use the RAND function on that new array, before returning to the original array armed with the primary key? (Which is my pseudo-understanding of what getResources does, but it would be good to have it confirmed).
            Writer > E-consultant > MODx developer || Salesforce || modx 2.x || PHP 5.2.13 || MySQL client 5.0.86
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: odeclas at Sep 27, 2010, 11:57 AM

            (1) The primary_key being the resource ID comes from the AccessibleSimpleObject then does it? (ie because the object I had created was a specific Object type, the array I had created wasn’t indexed by row number).
            Yes, it’s inherited from xPDOSimpleObject via modAccessibleSimpleObject.

            Quote from: odeclas at Sep 27, 2010, 11:57 AM

            and (2) if I wanted to continue my original experiment - In order to find the resource ID, I would have to retrieve all the children IDs into a non-object array, and then use the RAND function on that new array, before returning to the original array armed with the primary key? (Which is my pseudo-understanding of what getResources does, but it would be good to have it confirmed).
            getResources sends an ORDER BY RAND() (using the config sample I showed you) and LIMIT 1 to the database as part of the SQL query, so there is no need to find an ID ahead of time, or return a huge result set for no reason. This is a much more efficient approach than trying to choose a random record from a large result set. In your original approach, you need to return all the possible results, then choose a specific record randomly and get the field you want; that’s a lot of wasted memory.