SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    elseif not doing last step

    I have the following elseif :

    PHP Code:
    if ( $theanswer == $myanswer ) {
        echo 
    '<img src="images/right.jpg" width="31" height="29">';
    } elseif( 
    $theanswer != $myanswer) {
        echo 
    '<img src="images/wrong.jpg" width="31" height="29">';
    } else {
        echo 
    "You didn't answer this one"
    The crux of this is that there are 4 multiple choice questions, if they answer right, it shoes the right.jpg, if they got it wrong, it shows wrong.jpg and if they didn't choose anything, then when they click submit, it inserts a value of 0 into the database. So, everything works except it doesn't say "you didn't answer this one" when they don't submit something, it is just blank.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ( $theanswer == $myanswer ) { 
        echo 
    '<img src="images/right.jpg" width="31" height="29">'
    } elseif( 
    strlen($myanswer) > 0) {
        
    // they answered, but it wasnt equal to $theanswer
        
    echo '<img src="images/wrong.jpg" width="31" height="29">'
    } else { 
        echo 
    "You didn't answer this one"

  3. #3
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's because second condition is true even when there is no answer.
    You should specifically check for a blank answer, something like this:
    if(empty($theanswer)){
    echo 'You did not answer';
    } elseif($theanswer !== $myanswer){

    echo 'wrong answer';
    } else {

    echo 'correct';
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your responses. I tried both ways and it still doesn't show"you didn't answer that one". I don't know if it has anything to do with my nested repeat?

    Here is the SQL and the nested repeat.

    questions table :

    SELECT *
    FROM questions q INNER JOIN answers a ON q.AnswerID = a.AnswerID
    WHERE q.QuizID = colname
    ORDER BY q.Question ASC

    answers table

    SELECT Answer, s.QuestionID
    FROM student_answer s, answers a
    WHERE s.AnswerID = a.AnswerID AND s.QuestionID = 123456789 AND s.StudentID = colname
    ORDER BY s.AnswerID

    Nested repeat:

    PHP Code:
    <?php
      
    if ($totalRows_master1questions>0) {
        
    $nested_query_detail2student_answer str_replace("123456789"$row_master1questions['QuestionID'], $query_detail2student_answer);
        
    mysql_select_db($database_Driving);
        
    $detail2student_answer mysql_query($nested_query_detail2student_answer$Driving) or die(mysql_error());
        
    $row_detail2student_answer mysql_fetch_assoc($detail2student_answer);
        
    $totalRows_detail2student_answer mysql_num_rows($detail2student_answer);
        
    $nested_sw false;
        if (isset(
    $row_detail2student_answer) && is_array($row_detail2student_answer)) {
          do { 
    //Nested repeat
    ?>

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did what you suggested and I got the following back.

    There are only 3 questions on this quiz. I answer the first 2 and left the last one out.

    1. sting(2)"26"

    2.string(4)"Blue"

    3. this shows nothing...

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to look at which conditions must be true for that block of code to execute. You probably have an outer if, or loop statement.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I am not following, Which block of code?

    Also, I left this code out of my other posts(nested repeat code), not sure if it makes a difference to anything?

    PHP Code:
    <?php
          
    } while ($row_detail2student_answer mysql_fetch_assoc($detail2student_answer)); 
        }
      }
    ?>
                                        <?php } while ($row_master1questions mysql_fetch_assoc($master1questions)); ?>
    Also, maybe this whole thing can be avoided on the actual pages where they choose their answers.
    Is it possible to say that if they select something then submit else if they don't choose an answer then don't submit. At the moment if they don't choose an answer and submit a hidden value of 0 is stored as their answer in the database. "test" is the hidden field being submitted.

    PHP Code:
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && ($_POST['Answer'] !="")) {
    $insertSQL sprintf("INSERT INTO student_answer (StudentID, QuizID, QuestionID, AnswerID) VALUES (%s, %s, %s, %s)",        
    GetSQLValueString($_POST['StudentID'], "int"),
    GetSQLValueString($_POST['QuizID'], "int"),
    GetSQLValueString($_POST['QuestionID'], "int"),
    GetSQLValueString($_POST['Answer'], "int"));

    mysql_select_db($database_Driving$Driving);
    $Result1 mysql_query($insertSQL$Driving) or die(mysql_error());

    } elseif((isset(
    $_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && ($_POST['Answer'] =="")) {
    $insertSQL2="INSERT INTO student_answer (StudentID, QuizID, QuestionID, AnswerID) 
    VALUES ('
    $_POST[StudentID]','$_POST[QuizID]','$_POST[QuestionID]','$_POST[test]')";

    mysql_select_db($database_Driving$Driving);
    $Result2 mysql_query($insertSQL2$Driving) or die(mysql_error()); 

  8. #8
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, you can add simple javascript to validate that at least one answer has been selected.

    This is very easy to do.

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I tried that but it never used to bring up the message and just let me insert the null value anyway. Here is what I had:

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    
    function validate() {
    var theMessage = "Please complete the following: \n-----------------------------------\n";
    var noErrors = theMessage
    // make sure a radio button is selected
    var radioCheck = false;
    for (i = 0; i < document.form1.Answer.length; i++) {
    if (document.form1.Length[i].checked)
    radioCheck = true; }
    if (!radioCheck) {
    theMessage = theMessage + "\n --> Choose an answer";
    }
    // If no errors, submit the form
    if (theMessage == noErrors) {
    return true;
    } else {
    // If errors were found, show alert message
    alert(theMessage);
    return false;
    }
    }
    // End -->
    </script>
    Then on the actual form I called validate() but it still doesn't bring up the message for some reason. I don't know if it is because only one record is displayed at a time and when you click submit it moves to the next record?

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    This thread is going all over the place, going back to the answer you gave:

    1. sting(2)"26"

    2.string(4)"Blue"

    3. this shows nothing...
    if your original variables were;

    $_POST['q1'],
    $_POST['q2'],
    $_POST['q3'] -- and this one was not even sent ...

    Then you could identify that it had not been sent because it would not be set:
    PHP Code:
    if( !isset( $_POST['q3'] ) ) {
    echo 
    'You did not answer question 3' 

    It gets confusing when you try and assess;

    "" - an empty string
    0 - zero
    NULL - null
    FALSE - Boolean false
    - the absence of anything
    The variable not being sent at all.

    Using var_dump( $_POST ) (or $_GET ) will clarify what PHP is shunting around, if anything.

    Take a good look at this matrix; http://www.deformedweb.co.uk/php_variable_tests.php

  11. #11
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, this is getting seriously confusing. To try and avoid using 0 and NULL etc I just made it so that if the user submits without choosing an answer, it adds a value from a hidden field and instead of 0 I used 1000000 so at least it is a number and not 0.

    I thought that now my else if code was right. It works when they answer right or wrong but still shows nothing if they didn't choose an answer.

    PHP Code:
    $theanswer $row_master1questions['Answer'];
    $myanswer $row_detail2student_answer['Answer'];
    $test=100000;

     if ( 
    $theanswer == $myanswer ) {     
    echo 
    '<img src="images/right.jpg" width="31" height="29">'
    } elseif(
    $theanswer !== $myanswer  && $myanswer !== $test){   
    echo 
    '<img src="images/wrong.jpg" width="31" height="29">'
    } elseif(
    $myanswer == $test ){    
    echo 
    "You didn't answer this one"

  12. #12
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Show me the html form elements that constitute the questions then.

  13. #13
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I don't follow. Do you want the html form elements from the questions page or the grading page?


Bookmarks

Posting Permissions

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