Jump to content

Random code...


ItsWesYo

Recommended Posts

5Kscramble.php

<?php
//Check to see if null values were submitted...
if($_POST['question1'] == "") {
?>
<form action="5Kscramble.php" method="POST">
<b>swe</b><br>
<input type='text' name='question1' size='25'><br><br>
<input type="submit" value="Submit!">
</form>
<?php
} else {
//Variables, $x records questions correct. $y is the number of questions.
$x = 0;
$y = 1;
//Check the values of the select questions.
if($_POST['question1'] == "wes") {
$x++;
}
?>
<?php
echo 'You got '. $x .' out of '. $y .' questions correct.';
?>
<?php
}
?>

 

Would there be a way to show a code (ex: 123456789) from a .txt file that I provide on my server to display if ALL questions are answered correctly?

 

Example:

If I have random codes in a text file (seperated by a line) ... like:  123, 234, 345, 456, 567, 987, 876, etc etc

Then, if someone answers ALL the questions correctly, a script would pull ONE of those codes ONCE from the file.

It would display the code on the results page.

Then I go from there by myself.

Link to comment
Share on other sites

Yes. Take a look at the file_get_contents() function. You can read a read into a string with that. You'll then need the explode() function to separate out all of your possible codes.

 

Then, use rand() to select a random number between one and the number of items in the array. For example:

 

<?php
$str = '123,345,578,487,685,963';//ive defined a string here - but you can easily retrieve one from a file.
$bits = explode(',',$str);
$n = rand(0,count($bits));
$randomcode = $bits[$n];
echo $randomcode;
?>

Link to comment
Share on other sites

Assuming your scenario...a text file with the codes in it, seperated by commas:

 




if ($all_correct)  //Fire only if we know they got all questions right...
    
    {
$file="codes.txt";

$f_handle=fopen($file,"r"); //Open the file for read only.
	   
	   
	   $f_size=filesize($file); //Get the size of the file in bytes.
	   
	   
	   $f_contents=fread($f_handle,$f_size); //Read the entire contents of the file in to a variable.



$code_array=explode("," , $f_contents); //Explode the contents in to an array


$array_count=count($code_array);  //Count the number of members in the array


/*
Now you need to select one of your array elements randomly.  Basically,
that's just a number between 0, and the total number of elements in 
your array, (minus 1, because of the 0-based index of an array)

*/


$random_code=$code_array[rand(0,$array_count-1)];  //Select the random array member.



echo $random_code; //Echo the value back.

     

     }








 

 

Hope that helps?

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.