-1

I have a php file that writes to a txt file:

<?php
$answers = "answers.txt";
$fh = fopen($answers, 'a+') or die("can't open file");
$stringData = $_POST["username"];
$timestamp = date("g:i A, m/j/Y");
fwrite($fh, $stringData);
fwrite($fh, " started the quiz at ");
fwrite($fh, $timestamp);
fwrite($fh, "\n");
fclose($fh);
?>

Assuming I replace this code to enter a number (e.g. 10) followed by the user's answer (a string) so it submits the question number followed by the answer, how can I search through the txt file for the username then add that line to an array? Also, could I check the file when each question loads to see which questions the user has answered and then either choose another random quiz or continue loading (if they have not)? Basically:

  • Search the text file for a string and output any lines that contain it

  • Search the text file for a string and then search for a number encased in commas or some other identifier (e.g. ,23,) on the same line as the string.

If I should be doing this using MySQL, a few links or other resources would be nice, because I have heard of MySQL but never used it. Thanks a bunch in advance!

NOTE: I believe I need to search for a string using strpos and then get the line number and take the whole line and put it in the string. Then, I need to use explode "," to get the question numbers and have a php script on each page check to see if it's number has already been answered. If so, then select another random number. If not, recieve input then write it back to the text file. Or, perhaps I could use sessions to store the questions answered. However, I have no idea how to implement all this together.

Vreality
  • 305
  • 5
  • 16
  • 1
    you should most definitely be using a db for this. –  Mar 07 '12 at 02:28
  • @Dagon Is there a good tutorial for how to set one up/use it? I have phpMyAdmin and MySQL on my server, but I do not know how to create and use databases effectively. Thanks for the reply! – Vreality Mar 07 '12 at 02:32
  • 2
    There are **a ton** of PHP/MySQL tutorials all over the interwebs. Use a search engine. Sorry, we can't be covering the very basics for you individually here, try to get up to speed yourself using the most appropriate method available to you. – deceze Mar 07 '12 at 02:34

1 Answers1

2

I would say MySQL would be the better route to take.

Get Started Here: http://www.w3schools.com

Also check out other posts here:

MySql database design for a quiz

Am doing online Quiz type of script in PHP. It is better to use cookies or sessions

EDIT:

Don't forget about security!

Community
  • 1
  • 1
rackemup420
  • 1,600
  • 2
  • 15
  • 37
  • 1
    Another great page (again by w3) is: http://www.w3schools.com/php/php_ajax_database.asp This is nearly exactly what I wanted in the end... Thanks for the really quick and useful replies! – Vreality Mar 07 '12 at 02:51
  • If this helped or is the right answer please mark it as such, thank you! – rackemup420 Mar 09 '12 at 14:49