-
☆ A M B ☆
- 77 Posts
I am making 2 getresources calls to generate galleries(li and img tags) that are interactive with each other. I would like to limit and randomize each of them, but need to make sure that the same 5 resources get returned in each one. Any ideas?
One way might be to write a snippet that produces a random number based on the number of images - 5 and writes that value to a TV.
Call the snippet uncached once at the top of the page, then use the TV value in the &offset property of getResources. The images will be in order from the offset value -- I don't think you can randomize the 5, but they'll be taken from a random part of the table.
Another way, if it would work for you, would be to use the &toPlaceholder property to have the results written to a placeholder, then use that placeholder in both places.
-
☆ A M B ☆
- 77 Posts
Bob, thanks for the thought starter... I ended up putting my 2 getresources calls in a snippet and then passed a simple php variable with the 3 pre-generated random id numbers...
Outer call from template:
[[!randomauthorgenerator? &rset=`[[!getResources? &parents=`99` &limit=`3` &sortby=`rand()` &tpl=`@INLINE [[+id]],`]]`]]
Inside the snippet:
<?php
echo '
<div id="auther-slider-thumb" class="auther-slider-thumb">
[[!getResources? &resources=`'.$rset.'` &limit=`3` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &tpl=`AuthorImg`]]
</div>
<ul class="auther-slider">
[[!getResources? &resources=`'.$rset.'` &limit=`3` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &tpl=`AuthorDetail`]]
</ul>
';
I'm glad you got it sorted.
You might consider turning things around by getting the resource list inside the snippet without the overhead of another getResources call (untested).
[[!randomauthorgenerator? &parents=`99`]]
$c = $modx->newQuery('modResource');
$c->limit(3);
$c->where(array('parent' => $parents));
$c->sortby('RAND()');
$docs = $modx->getCollection('modResource', $c);
$rsetArray = array();
foreach($docs as $doc) {
$rsetArray[] = $doc->get('id');
}
$rset = implode(',', $rsetArray());
return '
<div id="auther-slider-thumb" class="auther-slider-thumb">
[[!getResources? &resources=`'.$rset.'` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &tpl=`AuthorImg`]]
</div>
<ul class="auther-slider">
[[!getResources? &resources=`'.$rset.'` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &tpl=`AuthorDetail`]]
</ul>
';