[EDIT: see the next post for the latest attempt]
<?php
/**
*
* randomChild
*
* Returns the ID (or the content) from a randomly selected child resource
*
* @ author Paul Merchant
* @ copyright 2010, Paul Merchant
* @ version 1.0.0 - May 3, 2010
*
* OPTIONS
* parents - (Opt) A comma delimited list of immediate parents, defaults to current resource
* getContent - (Opt) Set as true to return the content instead of the ID, defaults to false
* default - (Opt) The value returned if nothing is found
*
* Exmaple1: [[randomChild?]]
* Example2: [[randomChild? &parents=`123,124,125` &getContent=`1`]]
*
* Thanks to Susan Ottwell for writing a similar snippet for modx Evolution!
*
**/
// set defaults
$parents = $modx->getOption('parents',$scriptProperties,$modx->resource->get('id'));
$getContent = $modx->getOption('getContent',$scriptProperties,false);
$output = $modx->getOption('default',$scriptProperties,'');
// get the IDs of all immediate and published children from each parent
$criteria = $modx->newQuery('modResource');
$criteria->where(array(
"`parent` IN (" . $parents . ")",
'deleted' => 0,
'published' => 1,
));
$criteria->select(array('id'));
$children = $modx->getCollection('modResource',$criteria);
// count children found
$childcount = count($children);
if ($childcount < 1) {
return $output;
}
// get the id of a random child
$id = $children[rand(1, $childcount) -1]->get('id');
if (!$getContent) {
// just the id
$output = $id;
} else {
if ($id == $modx->resource->get('id')) {
// use the current resource
$resource =& $modx->resource;
} else {
// get only the columns we need
$criteria = $modx->newQuery('modResource');
$criteria->select(array('id','content'));
$criteria->where(array('id' => $id,));
$resource = $modx->getObject('modResource',$criteria);
}
// get the contents of the content field
if (empty($resource)) return $output;
$fieldValue = $resource->get('content');
if ($fieldValue !== null) {
$output = $fieldValue;
}
}
return $output;
?>
You know you can do this with getResources by setting the sortby property to the value RAND() and limit to 1 (if you only want one random result)?
Also, why grab all of the data from all of the objects if you only want one (and probably only specific fields from it)? That’s a huge waste of memory and resources IMO.
<?php
// Get a random child, using getCollection instead of getObject for possible multiple results.
$criteria = $modx->newQuery('modResource');
$criteria->where(array(
'deleted' => 0,
'published' => 1,
'template' => 2
));
$criteria->sortby('RAND()'); //
$criteria->limit(1); //Limit the results to 1
$children = $modx->getCollection('modResource',$criteria);
foreach ($children as $child) {
echo $child->id.'<br />';
}
<?php
// Get a random page ID.
$criteria = $modx->newQuery('modResource');
$criteria->where(array(
'deleted' => 0,
'published' => 1,
'template' => 2 //Only get resources with template 2
));
$criteria->sortby('RAND()');
$criteria->limit(1); //Limit the results to 1
$child = $modx->getObject('modResource',$criteria);
return !empty($child) ? $child->id : 1; //1 is the default id when nothing can be found.
[[~[[!RandomPage]]]]
And for people who want to use this for a random link, like mine: http://nimja.com/random
Make a snippet: RandomPage with this content:
<?php // Get a random page ID. $criteria = $modx->newQuery('modResource'); $criteria->where(array( 'deleted' => 0, 'published' => 1, 'template' => 2 //Only get resources with template 2 )); $criteria->sortby('RAND()'); $criteria->limit(1); //Limit the results to 1 $child = $modx->getObject('modResource',$criteria); return !empty($child) ? $child->id : 1; //1 is the default id when nothing can be found.
And make a weblink (with the alias "random" or whatever) with this as the link:
[[~[[!RandomPage]]]]
This just emphesizes why I adore Revolution so much above any other system ^_^. Go Go Gadget Super Flexible CMS!
$criteria = $modx->newQuery('modResource');
$criteria->where(array(
'deleted' => 0,
'published' => 1
));
$criteria->sortby('RAND()');
$folder = isset($folder)? $folder:0;
$child = $modx->getObject('modResource',$criteria, $folder, 'content');
$output = $messages[$index]['content'];
return "$output";
[[ClientQuote? &folder=`3`]]
<?php
$folder = isset($folder)? $folder:0;
$criteria = $modx->newQuery('modResource');
$criteria->where(array(
'deleted' => 0,
'published' => 1,
'parent' => $folder
));
$criteria->sortby('RAND()');
$child = $modx->getObject('modResource',$criteria);
return $child->get('content');
