xn1 Posted March 23, 2011 Share Posted March 23, 2011 Can anyone help me with the following please. Upon loading the page, I want to generate a random string based of the following... Select one value from the following array $categories = array("easy", "medium", "hard") And generate a random number from one to five. The string would then look like hard1 or easy5 etc.. Then check if the string generated exists in the following session array $_SESSION['asked'] If not, add it to that array and continue, if it is, create a new random string and check if that one exists and loop until its found one that isn't. Thank you in advance Quote Link to comment Share on other sites More sharing options...
MrXHellboy Posted March 23, 2011 Share Posted March 23, 2011 <?php mt_srand((double)microtime()*1000000); $categories = array("easy", "medium", "hard"); $created = $categories[mt_rand(0, count($categories)-1)].mt_rand(1,5); if ($created != $_SESSION['asked']) $_SESSION['asked'] = $created; ?> I am not sure what you mean but you want something like this ? Quote Link to comment Share on other sites More sharing options...
xn1 Posted March 23, 2011 Author Share Posted March 23, 2011 Thank you for the quick response. Will this keep generating strings until it finds one not in the $_SESSION['asked'] array? Quote Link to comment Share on other sites More sharing options...
MrXHellboy Posted March 23, 2011 Share Posted March 23, 2011 You mean you did assign a array to the index asked of the global array $_SESSION ? Or you mean that the value is assign to the $_SESSION['asked'] ? Maybe Code? Quote Link to comment Share on other sites More sharing options...
xn1 Posted March 23, 2011 Author Share Posted March 23, 2011 Well, I'm trying to create the code, I want it to generate the random string, check to see if that string exists in the SESSION['asked'] array, if it does already exist, make up a new string and keep making them until it finds one that doesn't. When it creates a string that isn't in the array, add the created string to the array. and continue processing. Quote Link to comment Share on other sites More sharing options...
MrXHellboy Posted March 23, 2011 Share Posted March 23, 2011 <?php function CheckExsits($haystack, $Session_arr) { mt_srand((double)microtime()*1000000); $random = $haystack[mt_rand(0, count($haystack)-1)].mt_rand(1,5); return (in_array($Session_arr, $haystack)) ? CheckExsits($haystack, $Session_arr): $random; } echo CheckExsits(array('hard', 'easy', 'medium'), array('easy2', 'hard3')); ?> Quote Link to comment Share on other sites More sharing options...
xn1 Posted March 23, 2011 Author Share Posted March 23, 2011 Sorry, you've lost me with haystack I'm currently using this to create my random string On a page load I create a string with... $catagories = array("easy", "medium", "hard"); $rand_key = array_rand($catagories, 1); $rand_catagory = $catagories[$rand_key]; $rand_number = mt_rand(1,5); Then if I were echo $rand_catagory.$rand_number it would show hard1 or easy3 or medium4 etc. So upon creating the very first string it adds to the array with $_SESSION['asked'][]=$rand_catagory.$rand_number; Now upon creating the next string on new a page refresh, I want it to the check is the newly created string exists in the $_SESSION['asked'] array. If it exists already, try making a new string and see if that exists in the $_SESSION['asked'] array. Upon finding one that isn't in the array add it too the array and continue process the rest of my script. Hope that explains myself well. Quote Link to comment Share on other sites More sharing options...
MrXHellboy Posted March 23, 2011 Share Posted March 23, 2011 Sorry, you've lost me with haystack haystack = array to search in Quote Link to comment Share on other sites More sharing options...
xn1 Posted March 23, 2011 Author Share Posted March 23, 2011 Sorry about this, i must be appropriating what you are saying wrong, I've done the below but I'm getting server errors if(isset($_POST['submitbutton'])) { $_SESSION['asked']=array(); $catagories = array("easy", "medium", "hard"); function CheckExsits($catagories, $_SESSION['asked']) { mt_srand((double)microtime()*1000000); $random = $catagories[mt_rand(0, count($catagories)-1)].mt_rand(1,5); return (in_array($_SESSION['asked'], $catagories)) ? CheckExsits($catagories, $_SESSION['asked']): $random; } header ("Location: $random.php"); } Quote Link to comment Share on other sites More sharing options...
xn1 Posted March 23, 2011 Author Share Posted March 23, 2011 I've just figured it out actually, thank you for the help Quote Link to comment 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.