...

View Full Version : best way to compare Strings for words, for a Test



jakenoble
09-24-2005, 03:21 AM
Hi all

Firstly I have a form with questions, the form has input fields for the answers. The answer are then passed over to a second php document. So I have the Questions and the Answer all ready as variables.

I then have certain words that need to be present within the answer of a question, these can exist where ever. The main problem is that the Questions are randomly generated, which means I cannot hard code the answers to fit the questions.

Therefore, something like this is going to have to exist, if question = x, then use this table of keywords for question x. Then check this table of keywords against the words in the answer for question x. Make sense?

I can do most of it, but the hardest part i think is going to be taking the keywords from a table, or anywhere and testing them against all the words in the variable answer string.

What's the best way of doing this in your opinion, is there a way of reading comma-separated-values out a MySQL table?

Thanks in advance for ANY Input.
Jake

missing-score
09-24-2005, 04:39 AM
Well if you grab your mysql result, you can use PHP to separate the values. The main problem is going to be punctuation used in answers...

Something like this maybe? (not tested)




$strip_punctuation = array( ',', '.', '?', '!', '\'', '"', '\\' );
$users_answer = str_replace( $strip_punctuation, '', $_POST['answer'] );


// Break users answer into an array of individual words..
$answer = explode( ' ', $users_answer );

// Just put your MySQL result of comma separated words here
$comma_separated_words = 'word,foo,bar';

// Make this into an array too
$right_words = explode( ',', $comma_separated_words );

$wordcount = 0;
$total_words = count($right_words);

for( $i=0; $i<$total_words; $i++ ){
if(in_array($right_words[$i], $answer)){
++$wordcount;
}
}

if($wordcount == $total_words){
// Got it right
} else {
// Got it wrong
}

jakenoble
09-24-2005, 11:25 AM
Hi Missing Score.

Sounds like a good idea to me. Although this part seems like a potential problem

// Make this into an array too
$right_words = explode( ',', $comma_separated_words );
As this is hard coding the right words for a question into the page. But because the questions are going to be in a randomly generated order. So would it not be better to do, if question == XYZ, then use keywords from record number 1 in MySQL table of keywords. Trouble with this is the php code is going to be huse as there are nearly 30 questions, and only 10 are only ever going o be used at one time.

I will start to work on what you have mentioned and see how I get on. I just want to get it all sorted out in my head before I start it because A) i want to get it rigth and B) I am new so want to know exactly what it is I am doing.

Thanks for your input and time.
Jake

missing-score
09-24-2005, 12:00 PM
Wouldnt it make more sense to store the questions and answers with one another in the database..., and then loop through the questions and then the answers.

jakenoble
09-24-2005, 02:01 PM
Would that not mean having to put all the questions AND all the answers entered by a user into a table in the db? Which could only be done once the answers had been submitted.
There are about 400 students going to be taking these tests, with some of them doing them more than once, wouldn't all that put alot of strain on the db?

missing-score
09-24-2005, 02:04 PM
I think you misunderstood... I meant store the questions and the answer words (comma separated in a database table)... Then, if you want to store the users answers you could do so in a separate table.

But as for strain on the db, to be put simply... no, MySQL is extremly powerful, to the point where it can be optimized for working with millions of rows... a few hundred is going to put no real strain on the database.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum