Jump to content

Can't select random entry from array with one entry using 'array_rand'


Recommended Posts

I have a quiz script I'm trying to make where a user can select what levels the questions can be in the settings page, which are saved with cookies. Each level has it's own text database of questions (I don't know any better..) and I want the quiz to select a random database from the levels enabled and select a random question from that. The only problem I'm facing is that I can't get the program to select a random database when there's only one level enabled, which is set as default. My script is below:

 

$levelz = array();
if ($_COOKIE['ToggleKanaWordsLVL1'] == "yes") {
$levelz[] = "levelone";
}
if ($_COOKIE['ToggleKanaWordsLVL2'] == "yes") {
$levelz[] = "leveltwo";
}
if ($_COOKIE['ToggleKanaWordsLVL3'] == "yes") {
$levelz[] = "levelthree";
}

$rand_keys = array_rand($levelz, 2);
$db = "quizdocs/" . $levelz[$rand_keys[0]] . ".txt";

 

This code works fine when there are any two or three databases selected, but when the user has only one level enabled, it shows this error:

 

Warning: array_rand() [function.array-rand]: Second argument has to be between 1 and the number of elements in the array in [...]kanawords.php on line 18

 

But when I change the code to say "$rand_keys = array_rand($levelz, 1);", it doesn't appear to select anything, resulting in "quizdocs/.txt" when I try to display $db. How can I fix this?

$rand_keys = (count($levelz) > 1)?array_rand($levelz, 2):array_keys($levelz); // you can change the 2 to be dynamic if you wish or just leave it at 2.

 

What this should do is if $levelz as more than 1 item it uses array_rand, if not well randomizing will not do us anygood, so it just grabs the array keys.

That worked perfectly! Thanks! Hmm, your line has like three things I don't know about. Better get studying again...

 

Ternary operators act like a shortened if/else.

 

(condition is true/false) then ( ? ) do this else ( : ) do that.

 

EDIT:

On a separate note, if you wanted to get a random element, you could have just used shuffle with current

 

$rand = current(shuffle($levelz));
$db = "quizdocs/" . $rand . ".txt";

 

Should work just as well, without the need for the ternary statement.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.