eugeniu Posted May 2, 2009 Share Posted May 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/156555-cant-select-random-entry-from-array-with-one-entry-using-array_rand/ Share on other sites More sharing options...
premiso Posted May 2, 2009 Share Posted May 2, 2009 $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. Quote Link to comment https://forums.phpfreaks.com/topic/156555-cant-select-random-entry-from-array-with-one-entry-using-array_rand/#findComment-824304 Share on other sites More sharing options...
eugeniu Posted May 2, 2009 Author Share Posted May 2, 2009 That worked perfectly! Thanks! Hmm, your line has like three things I don't know about. Better get studying again... Quote Link to comment https://forums.phpfreaks.com/topic/156555-cant-select-random-entry-from-array-with-one-entry-using-array_rand/#findComment-824308 Share on other sites More sharing options...
premiso Posted May 2, 2009 Share Posted May 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156555-cant-select-random-entry-from-array-with-one-entry-using-array_rand/#findComment-824309 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.