Seeing as you pass the chunk name could you not just call getChunk in a loop, appending 1,2,3,4 etc. to the end of the name, check that you get something, if you don’t you stop, the number of iterations now becomes the number of chunks.
Use MODx, or the cat gets it!
That sounds like a great idea Shamblett - exactly the kind of think I was thinking - I don’t know how to program though (I do css / front end) - any ideas on the code to make it happen?
try this one:
<?php
$randomChunkName = isset($randomChunkName) ? $randomChunkName : 'randomChunkBL'; //Set name or use default
$maxNoOfChunks = isset($maxNoOfChunks) ? $maxNoOfChunks : '10'; //Must set a value
for($noOfChunks=1;$noOfChunks<=$maxNoOfChunks;$noOfChunks++){
if (!$modx->getChunk($randomChunkName.$noOfChunks)){
break;
}
}
echo '{{' . $randomChunkName . rand(1, ($noOfChunks-1)) . '}}';
?>
cheers Bruno
Hello Bruno,
That works perfectly - thank you so very much!!!
You are super clever!
Thanks again and all the best.
Nick
Hi Bruno,
I have created 25 random chunks - and changed the max count in the snippet to 30 - but only the first 9 are shown (Fact1, Fact2, Fact3 .... Fact9).
The rest (the names with 2 digit numbers at the end) are not shown - Fact11, Fact12, Fact13, .... Fact20... etc)
I call it like this:
[!randomChunkAdl?!]
Any ideas on how to include the double digit named chunks as well?
Thanks and all the best.
Regards,
Nick
Have modified it a little bit, but I think this is not the matter.
Are you sure you have also the chunk Fact10 and you have changed $maxNoOfChunks or used &maxNoOfChunks=`30` in the call?
I did a try and it worked for me
<?php
$randomChunkName = isset($randomChunkName) ? $randomChunkName : 'randomChunkBL'; //Set name or use default
$maxNoOfChunks = isset($maxNoOfChunks) ? $maxNoOfChunks : '30'; //Must set a value
for($noOfChunks=1;$noOfChunks<=$maxNoOfChunks;$noOfChunks++){
if (!$modx->getChunk($randomChunkName.$noOfChunks)){
return '{{' . $randomChunkName . rand(1, ($noOfChunks-1)) . '}}';
}
}
return;
?>
Regards,
Bruno
Hello Bruno,
Thank you so much - you are right and I must apologise for an error at my end.
I had Fact1, Fact2, ... Fact 10 - but missed Fact11- then Fact 12... fact20
So missing one number broke it - I fixed that and now the script works an absolute treat.
Sorry that you had to double post!
Thank you so much.
All the best.
-
☆ A M B ☆
- 427 Posts
I apologize for being late, but this is what I have been using for years
$out = false;
$chunkPrefix = isset($chunkPrefix) ? $chunkPrefix : 'random';
$totalChunks = isset($totalChunks) ? $totalChunks : intval(0);
if ($totalChunks > 0) {
$chunk = $modx->getObject('modChunk', array(
'name' => $chunkPrefix . rand(1, ($totalChunks)),
));
}
if (is_object($chunk)) {
$out = $chunk->process();
}
return $out;
I will probably modify this to use a Category to pull the random chunks from. That way name no longer matters.