...

View Full Version : Random Order



CSS
03-20-2007, 03:33 PM
How would you make a randomizer where you pick 6 words and it randomizes them in order so that no word is repeated?

devinemke
03-20-2007, 03:40 PM
$words = array(
'PHP',
'is',
'the',
'greatest',
'thing',
'yet'
);

shuffle($words);
echo implode(' ', $words);

CSS
03-20-2007, 03:40 PM
Thanks a bunch.

Nightfire
03-20-2007, 03:42 PM
$words = array("elephant","yak","dog","cat","sheep","spookster");
$random = array_rand($words, 6);
for($i=0;$i<6;$i++){
echo $words[$random[$i]];
}

Something like that

aedrin
03-20-2007, 03:58 PM
It is better to use the most logical function, as it makes your code easier to read.

array_rand() is used to pick a few random elements from an array. It can be used to shuffle (!) the array but the manual does not state that the elements will be returned in a randomized order. Only that a random element be picked.

CSS
03-20-2007, 08:00 PM
Neither of those worked...

aedrin
03-20-2007, 10:02 PM
Show the code that you tried it with. So that we can see what the problem is. There's no reason that those functions should not have worked.

CSS
03-20-2007, 10:11 PM
Does anyone know how to do this?

Nightfire
03-20-2007, 10:13 PM
The 2 examples shown above WILL do this. You've done something wrong, but we cannot say what you've done wrong until you show your code

CSS
03-20-2007, 10:52 PM
Well, I tried both within PHP tags and both showed blank pages.

Fumigator
03-20-2007, 10:56 PM
Show...

us....

your....

code.....

CSS
03-20-2007, 10:59 PM
<?php
$words = array("elephant","yak","dog","cat","sheep","spookster");
$random = array_rand($words, 6);
for($i=0;$i<6;$i++){
echo $words[$random[$i]];
}
?php>

Nightfire
03-20-2007, 11:03 PM
<?php
$words = array("elephant","yak","dog","cat","sheep","spookster");
$random = array_rand($words, 6);
for($i=0;$i<6;$i++){
echo $words[$random[$i]];
}
?>


The closing of php is ?> not ?php>

If you posted the code several posts ago like you was asked, you could've gotten the answer much quicker.

Also, next time you have a problem, it'll help to show the error. You should've got something similar to


Parse error: parse error, unexpected '?' in var/www/test.php on line 7

aedrin
03-21-2007, 03:03 PM
Tested devinemke's solution and it works.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum