Jump to content

Recommended Posts

Hi, I need to generate a random array that stores various values. For example,

<form action="testscript.php" method="POST">
Value1:<input type = "text" name="value1">
Value2:<input type = "text" name="value2">
<?php
$valueone = $_POST['value1'];
$valuetwo = $_POST['value2'];
$number = random(1,10); //random value for array
$array[$number] = array($valueone,$valuetwo); //each array number is unique
?>
<input type="submit" name="submit">
</form>

However, I need to make it such that if an array already has values stored in it, it moves on to another random array until it finds an empty one.

Also,  since I would need to validate the form, I will require the random array number to remain the same upon submission of the form.

I've tried using a for loop:

for($i=0; $i<1000; $i++){
if(isset($array[$number])){ //if the array is set, since $number is unique, generate new array by randoming another number
$number = random(1,10); //random another value for array
} $i++; }

but it doesn't seem to work the way I want it to.

Link to comment
https://forums.phpfreaks.com/topic/264788-storing-html-values-in-array/
Share on other sites

I didn't use shuffle as I want my array to store values from the 100th position. So basically, array[0] -> array[99] are empty. I have a problem which is I am unable to capture the random number upon validation of the form. When I generate a random number say:

 

$rand = rand(100,999);

 

I want to be able to echo the same random number, instead when I submit the form, my code generates another random number. I can't use another variable to store the random number as again

 

$storage = $rand;

 

when I submit the form, $storage will also be the new random number instead of the old one.

Is there any way to capture the random number?

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.