GetList order by RAND()?

2 views
Skip to first unread message

Aaron Vegh

unread,
Jan 1, 2006, 2:38:28 PM1/1/06
to Php-Object...@googlegroups.com
Hi there,
Has anyone tried to get a random list back from a GetList query? I tried this...

$test = $test -> GetList(array(array("Compliance_CourseId", "=",
$_POST["module_id"])),
"RAND()", true, "15");

But I just get the first 15 records. Help?

Thanks,
Aaron.

Joel

unread,
Jan 1, 2006, 2:45:39 PM1/1/06
to Php Object Generator
Hey Aaron,

you're getting 15 records because the limit is 15, and passing rand()
as the field to sort by.

so, for instance, let's say rand() returns the value 1. getlist() will
try to sort by a field named "1". if there isn't such field, your list
will remain as-is and truncated to 15 items.

I don't really understand the purpose of Rand() in this case though..
maybe you can clarify this in another msg..

thanks

Joel

Joel

unread,
Jan 1, 2006, 2:57:17 PM1/1/06
to Php Object Generator
oh i think i understand what you're trying to do.

try using array_rand
(http://ca.php.net/manual/en/function.array-rand.php)

for instance,
srand((float) microtime() * 10000000);


$test = $test -> GetList(array(array("Compliance_CourseId", "=",

$_POST["module_id"])));
$rand_keys = array_rand($test, 15);
echo $test[$rand_keys[0]] . "\n";
echo $test[$rand_keys[1]] . "\n";
...

hope this helps

joel

Aaron Vegh

unread,
Jan 1, 2006, 11:09:39 PM1/1/06
to Php-Object...@googlegroups.com
Hi Joel,
Once more you proved that you are, indeed, The Man.

For the kids back home, the complete solution looked a little
something like this:

$test = new Compliance_Tests();


$test = $test -> GetList(array(array("Compliance_CourseId", "=",
$_POST["module_id"])));

srand((float) microtime() * 10000000);
$rand_keys = array_rand($test, 15);

for($i=0; $i<15; $i++) {
echo $test[$rand_keys[$i]]->question;
}


Cheers!
Aaron.

Reply all
Reply to author
Forward
0 new messages