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;
?>
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
Try :-
$values = array_values($weetjes);
$hit = $values[$rand];
Use MODx, or the cat gets it!
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!
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();
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!
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
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!
aha ,
capice, thank you very much.
i can go on
ralph