Help with building quiz
Results 1 to 4 of 4

Thread: Help with building quiz

  1. #1
    Member
    Join Date
    May 2007
    Posts
    82

    Help with building quiz

    I have a quiz that I am building for a school project but am stuck on a part.

    I need to have the answers stored in a text file. So far I have seen examples of the following:

    {~TRUE, =FALSE}

    I cannot understand how to make the code check the answers. Im posting my code in hopes someone can explain how to make it work. Thanks in advance.
    Attached Files Attached Files

  2. #2
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Advice:

    1. describe your approach to this problem (tell us what you've already tried)
    2. describe which part doesn't work (or which part you think doesn't work)
    3. ask a specific question: don't just "dump" your whole assignment into a zip file, show us the part you're having trouble with.

  3. #3
    Member
    Join Date
    May 2007
    Posts
    82
    Fair enough. Ok in the snippet below, there is a text file with the answers in it. Does the right answers array already exist at this point and how is it read? In other words, the answers are in the text file I think but what would the text file contents look like? I dont understand that part. Say you have to chose Yes or No from Question 1, what would the array element for that look like in the text file?




    $rightAnswers=array();

    $fileName="QuizFiles/QuizAnswers.txt";
    if(file_exists($fileName) == FALSE) {
    echo "There was a problem reading the file<br />\n";
    }//end of file_exists

    else {
    if ((is_file($fileName)) && (is_readable($fileName))) $lineInFile=file_get_contents($fileName);
    }//end of else

    $rightAnswers=array();
    $rightAnswers=explode(", ", $lineInFile); //Using ',' as delimiter divide to string (actually numbers).

    $rightAnswers=array_values($rightAnswers); //Line Structure as follows:
    // numberOfQuizzes,
    // counterQ1, counterQ2, counterQ3, counterQ4, counterQ5,
    // counterQ6, counterQ7, counterQ8, counterQ9, counterQ10,
    // counter10Achieved, counter20Achieved, counter30Achieved,
    // counter40Achieved, counter50Achieved, counter60Achieved,
    // counter70Achieved, counter80Achieved, counter90Achieved,
    // counter100Achieved

    //ending of loading the answers to the quiz from file.



    if ($userAnswer[1]==$rightAnswers[1]) {
    $sumRight=$sumRight+1;
    $scoreMark[1]=$markedRight;
    $mailResults[1]="To question #1, correctly answered: " . $userAnswer[1] . "\n";
    $fileLine[1]=1; //takes counts of user's answer to this questions,
    //and prepares line for file.
    }//end of if 1
    else {
    $sumWrong=$sumWrong+1;
    $scoreMark[1]=$markedWrong . "<font color='darkgray'> the correct answer is " . $rightAnswers[1] . "</font>\n";
    $mailResults[1]="To question #1, answered " . $userAnswer[1] . " which is wrong. The correct answer is " . $rightAnswers[1] . "\n";
    }//end of else 1

  4. #4
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    How much of this is required by the assignment?
    e.g., is the "QuizAnswers" file required to be formatted in this particular way?
    are you required to use particular functions (like explode(), instead of, say, fgetcsv())?

    some thoughts:
    PHP Code:
                $rightAnswers=array();

                
    $fileName="QuizFiles/QuizAnswers.txt";
                if(
    file_exists($fileName) == FALSE) {
                    echo 
    "There was a problem reading the file<br />\n";
                }
    //end of file_exists
                
                
    else {
                    if ((
    is_file($fileName)) && (is_readable($fileName))) $lineInFile=file_get_contents($fileName);    
                }
    //end of else            
                
                // you did this at the top of your script. why are you doing it again?
                
    $rightAnswers=array();

                
    // explode() creates an array, so you didn't need `$rightAnswers = array()` at all.
                
    $rightAnswers=explode(", "$lineInFile);    //Using ',' as delimiter divide to string (actually numbers).
                // ALSO,
                // If QuizAnswers.txt does not exist, `$lineInFile` will never have been defined - 
                //   which will cause an error when you try to use it in `explode()`.

                // `array_values()` also creates an array
                // you're actually replacing `$rightAnswers` with another, identical array - this is completely nonproductive.
                
    $rightAnswers=array_values($rightAnswers); 
    Say your QuizAnswers.txt file looks something like this:
    Code:
    T,T,F,T,F,F,T,F,A,C,C,B,A,C,5,7,1000,"green"
    I've got some true/false answers, some multiple-choice, numeric answers, and a string. Everything is formatted as a CSV string - each value is on the same line, separated by a comma. Reading the answers into an array is as simple as this:
    PHP Code:
    <?php

    ##  Example Code. Not a Working Solution.  ##

    $filename '/path/to/QuizFiles/QuizAnswers.txt';

    if( 
        
    file_exists$filename )                               // can we find the file?
        
    && ( $filehandle fopen$filename,'r' ) ) !== false  // can we open the file?
    ){
        
    $rightAnswers fgetcsv$filehandle );  // we're only getting one line
        
    fclose$filehandle );                   // we don't need the file anymore

        // `$rightAnswers` will be a numerically indexed array that holds the answers provided by the file.
        // you might do some validation here, e.g., `is_array( $rightAnswers )` 
        // or `count( $rightAnswers ) == {however many answers there are *supposed* to be}`

        // check answers here.
        //   (I think you may have more questions once you get to this point.)

    }else{
        
    // file couldn't be opened for some reason.
        // show an error message (or whatever you prefer).
    }
    [edit] just caught this:
    //Using ',' as delimiter divide to string (actually numbers).
    the string in question is a string. The fact that there are commas in it will prevent PHP from automatically treating it as a number.
    That may happen after you remove the commas.

    Additionally, your code is not using ","(comma) as a delimiter: it's using ", "(comma space).
    That may cause problems, depending on how you wrote the file (if you inserted spaces or not).
    Last edited by traq; 11-04-2012 at 03:27 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •