Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Location
    UK, derby
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts

    best way to compare Strings for words, for a Test

    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

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)

    PHP Code:

    $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


  • #3
    Regular Coder
    Join Date
    Feb 2004
    Location
    UK, derby
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Missing Score.

    Sounds like a good idea to me. Although this part seems like a potential problem
    Code:
    // 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

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Feb 2004
    Location
    UK, derby
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

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