Jump to content

Creating unique string


xn1

Recommended Posts

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

<?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 ?

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.

<?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'));
?>

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.

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");
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.