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 Link to comment https://forums.phpfreaks.com/topic/231526-creating-unique-string/ 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 ? Link to comment https://forums.phpfreaks.com/topic/231526-creating-unique-string/#findComment-1191448 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? Link to comment https://forums.phpfreaks.com/topic/231526-creating-unique-string/#findComment-1191455 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? Link to comment https://forums.phpfreaks.com/topic/231526-creating-unique-string/#findComment-1191456 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. Link to comment https://forums.phpfreaks.com/topic/231526-creating-unique-string/#findComment-1191462 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')); ?> Link to comment https://forums.phpfreaks.com/topic/231526-creating-unique-string/#findComment-1191473 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. Link to comment https://forums.phpfreaks.com/topic/231526-creating-unique-string/#findComment-1191482 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 Link to comment https://forums.phpfreaks.com/topic/231526-creating-unique-string/#findComment-1191483 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"); } Link to comment https://forums.phpfreaks.com/topic/231526-creating-unique-string/#findComment-1191495 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 Link to comment https://forums.phpfreaks.com/topic/231526-creating-unique-string/#findComment-1191503 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.