I am using cgsimple within a foreach loop to pull data from multiple pages and I need to randomise the output.
i.e. Say there are 10 pages and I need to show the contents of 4 but which 4 need to be randomised and of course it cannot show the same page content more than once. Also ideally the number should not be sequential i.e. it can show data in the order of page 5, 2, 9, 6.
Any ideas greatly appreciated.
How do I randomise the contents returned in a foreach loop
Re: How do I randomise the contents returned in a foreach loop
I found this doing a Google search for "php unique random" (without the quotes) and hit this page:
http://php.net/manual/en/function.rand.php (in the user comments section)
Set $total to the number of random numbers that you want. Set $min to the smallest number you want to output, and $max to the largest. In your case, you would set $min to 0 and $max to 9. This will give you 4 values that you can use to index your pages.
http://php.net/manual/en/function.rand.php (in the user comments section)
Code: Select all
$rand = array();
while (count($rand) < $total ) {
$r = mt_rand($min,$max);
if ( !in_array($r,$rand) ) {
$rand[] = $r;
}
}