ItsWesYo Posted July 25, 2007 Share Posted July 25, 2007 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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 25, 2007 Share Posted July 25, 2007 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; ?> Quote Link to comment Share on other sites More sharing options...
stlewis Posted July 25, 2007 Share Posted July 25, 2007 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.