Spell test-How to send a value of '1' to mysql if word spelt right, and '0' if not?

Discussion in 'PHP' started by Uncle Dave, Feb 8, 2009.

  1. #1
    Hi everyone,

    I'm new here, so I might just introduce myself first. I'm Dave, a teacher in Sydney who teaches new arrivals to Australia to speak English.

    I feel I should also disclose that I've also asked this question on a couple other php forums. I apologise if that's not 'the done thing'. I guess I'm just trying to find the best source of help and at this stage I don't know one forum from the other....anyway...to my question:

    I have a very simple quiz here (http://www.tesolclassroom.com/compspelquiz001.html) which asks students to identify 10 pieces of computer hardware.

    I've set up the php code such that each answer is imported directly into a Mysql database (See my code below).

    I've got this part working with no problems. Basically each question from 1 to 10 is labelled '$q001' to '$q010', and these correspond with database cells of the same names ( 'q001' to 'q010' ).

    However... I also have another cell for each question which is the same name, but with 'score' appended to the end ( ie: 'q001score' to 'q010score' ).

    Is it possible to send a value of '1' to each of these score cells if the student spells the piece of hardware correctly, or a value of '0' to each of these score cells if the word is not spelt this way?

    Ie: The first answer is a 'mouse'. If the student spells it correctly (case sensitive), I'd like the value '1' sent to cell q001score, and if they spell it any other way (or leave it blank), a '0' would be sent to this cell? Similarly, the second answer is 'monitor', and I'd like a '1' sent to cell q002score if it is spelt as such, or a '0' sent to this cell if it is spelt any other way.

    The second part of my question relates to adding these scores. The final cell in my Mysql database is entitled 'totalscore'. Is it possible to add the numbers from cells '001score' to '010score' to put a total in this final cell?

    Thanks for your time,

    Dave

    
    
    $query = "INSERT INTO compspelquiz001 (firstname, lastname, class, q001, q002, q003, q004, q005, q006, q007, q008, q009, q010)" . "VALUES ('$firstname', '$lastname', '$class', '$q001', '$q002', '$q003', '$q004', '$q005', '$q006', '$q007', '$q008', '$q009', '$q010')";
    //if($query){echo 'data has been placed'}
    mysql_query($query) or die(mysql_error());
    mysql_close($con);
    
    
    
    
    Code (markup):

     
    Uncle Dave, Feb 8, 2009 IP
  2. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #2
    <?php
    
    /* $answer contains the value of correct data, ex. mouse */
    
    if ($q001 == $answer) {
    $q001 = 1;
    }
    else {
    $q001 = 0;
    }
    
    echo $q001; // will print out 1 or 0 depending on your answer
    
    ?>
    PHP:
     
    ActiveFrost, Feb 8, 2009 IP
  3. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #3
    The above is right but the $variable names do not match your question.

    Then instead of the echo which you should use for the debug routine you will want to do an update query to insert the data.

    I am not quite yet good enough with mysql to tell you how to use a caculated field for the final total. The hard way to do it is to query the table by the studentID and then add the row amonut and then insert it into the total.

    $totalscore= ($row[q001]+$row[q002]+$row[q003]+$row[q004]+ < as many as you need>)

    Then insert $totalscore into the correct field.

    Like I said there is a much better way using a caculated field. Maybe someone else will donate that.
     
    Colbyt, Feb 8, 2009 IP
  4. Uncle Dave

    Uncle Dave Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for your responses ActiveFrost and Colby,

    I've managed to surprise myself by actually succeeding in what I wanted to do here thanks to your advice.

    I'll put the code below if anyone wants to use it.

    Since I'm here I might ask a couple more minor questions:

    1 -

     if ($q010 == digital) {$q010score = 1;}else {$q010score = 0;}; 
    Code (markup):
    I actually had to change this question for the kids, and give them the word camera, because if I substitute "digital camera" for "digital" in the above line, I get a message when I press the submit button which says "Parse error: syntax error, unexpected T_STRING in /home/tesolcla/public_html/compspelquiz001.php on line 29".

    It obviously has something to do with me having two words in there with a space between them.

    2 - I played around with ColbyT's advice, and found out that I didn't need to query the table. I just used the line:

    $totalscore = $q001score + $q002score + $q003score + $q004score + $q005score + $q006score + $q007score + $q008score + $q009score + $q010score;
    Code (markup):
    .

    In case I do this with more tests with much more questions, is there a way to make an array out of this, so I don't need to type in each variable? ...or maybe I should just not be so lazy! :)

    Thanks again for your help, and here's the code below - hope it can in turn help some others...especially if there's any other teachers on here.

    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <?PHP
    global $_POST;
    $firstname = $_POST["firstname"] ;
    $lastname = $_POST["lastname"];
    $class = $_POST["class"];
    $q001 = $_POST["q001"];
    $q002 = $_POST["q002"];
    $q003 = $_POST["q003"];
    $q004 = $_POST["q004"];
    $q005 = $_POST["q005"];
    $q006 = $_POST["q006"];
    $q007 = $_POST["q007"];
    $q008 = $_POST["q008"];
    $q009 = $_POST["q009"];
    $q010 = $_POST["q010"];
    
    /* $answer contains the value of correct data, ex. mouse */
    if ($q001 == mouse) {$q001score = 1;}else {$q001score = 0;}; 
    if ($q002 == monitor) {$q002score = 1;}else {$q002score = 0;}; 
    if ($q003 == scanner) {$q003score = 1;}else {$q003score = 0;}; 
    if ($q004 == projector) {$q004score = 1;}else {$q004score = 0;}; 
    if ($q005 == speaker) {$q005score = 1;}else {$q005score = 0;}; 
    if ($q006 == keyboard) {$q006score = 1;}else {$q006score = 0;}; 
    if ($q007 == system) {$q007score = 1;}else {$q007score = 0;}; 
    if ($q008 == memory) {$q008score = 1;}else {$q008score = 0;}; 
    if ($q009 == printer) {$q009score = 1;}else {$q009score = 0;}; 
    if ($q010 == digital) {$q010score = 1;}else {$q010score = 0;}; 
    
    $totalscore = $q001score + $q002score + $q003score + $q004score + $q005score + $q006score + $q007score + $q008score + $q009score + $q010score;
    
    $str = strtolower($str);
    
    
    //**********************SEND TO DATABASE****************************
    
    $db = 'YOUR DATABASE NAME';
    $host = 'localhost';
    $un	= 'YOUR USERNAME';
    $pw = 'YOUR PASSWORD';
    
    $con = mysql_connect($host, $un, $pw);
    
    if($con){
    	mysql_select_db($db);
    	//echo 'connected';
    }else{
    	die(mysql_error());
    }
    
    
    $query = "INSERT INTO compspelquiz001 (firstname, lastname, class, q001, q001score, q002, q002score, q003, q003score, q004, q004score, q005, q005score, q006, q006score, q007, q007score, q008, q008score, q009, q009score, q010, q010score, totalscore)" . "VALUES ('$firstname', '$lastname', '$class', '$q001', '$q001score','$q002', '$q002score', '$q003', '$q003score', '$q004', '$q004score', '$q005', '$q005score', '$q006', '$q006score', '$q007', '$q007score', '$q008', '$q008score', '$q009', '$q009score', '$q010', '$q010score', '$totalscore')";
    //if($query){echo 'data has been placed'}
    mysql_query($query) or die(mysql_error());
    mysql_close($con);
    
    //***********************END OF DATABASE CODE***********************
    
    
    Code (markup):
     
    Uncle Dave, Feb 9, 2009 IP
  5. SedNaX

    SedNaX Active Member

    Messages:
    1,326
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #5
    1:

    You forgot to put quotes around digital ;)

    2:

    It is a lot easier if you create something like this:

    You start with 0 points and each time you have a correct answer you add one to $score ($score++; is the same as $score=$score+1;)

    Good luck!
     
    SedNaX, Feb 9, 2009 IP
  6. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #6
    As per above quotes go around a string variable. Sometime single sometimes double depending on where it is used.

    Glad you got it working.
     
    Colbyt, Feb 9, 2009 IP
  7. SedNaX

    SedNaX Active Member

    Messages:
    1,326
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #7
    You can use both always, everywhere. There is only one difference: if you want to use strings in it.

    If you want to echo: hi, $name

    With single quotes it would be:

    echo 'hi' . $name;
    PHP:
    With double quotes:

    echo "hi $name";
    PHP:
    So in this example double quotes seem easier :)
     
    SedNaX, Feb 9, 2009 IP
  8. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #8
    Just FYI, it should be:

    echo 'Hi ' , $name; // Notice the space
    PHP:
    To get the intended effect. You'd be amazed how many people forget.
     
    Danltn, Feb 9, 2009 IP
  9. SedNaX

    SedNaX Active Member

    Messages:
    1,326
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #9
    No, you don't have to. I always don't and it works perfectly.

    Notice you put a "," while you need to put a "." ;)

     
    SedNaX, Feb 11, 2009 IP