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.
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).
//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
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).
Bookmarks