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

Link to comment
Share on other sites

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

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.