php/mysql checkboxes

Discussion in 'PHP' started by snowLee, Dec 2, 2009.

  1. #1
    I have this script
    <?php
    
    include("config.php");
    
    $display = mysql_query("SELECT * FROM quiz ORDER BY id");
    
    if (! isset ($_POST['submit'])) {
    
    
        echo "<form method=post action=$_SERVER[PHP_SELF]>";
        echo "<table border=0>";
    
        while ($row = mysql_fetch_array($display)) {
    
        $id = $row["id"];
        $question = $row["question"];
        $opt1 = $row["opt1"];
        $opt2 = $row["opt2"];
        $opt3 = $row["opt3"];
        $answer = $row["answer"];
    	$answer1 = $row["answer1"];
    
        echo "<tr><td colspan=3><br><b>$id.$question</b></td></tr>";
        echo "<tr><td> <input type=checkbox name=q$id value=\"$opt1\">$opt1</td></tr><tr><td> <input type=checkbox name=q$id value=\"$opt2\">$opt2</td></tr><tr><td> <input type=checkbox name=q$id value=\"$opt3\">$opt3</td></tr>";
    
        }
    
        echo "</table>";
        echo "<input type='submit' value='See how you did' name='submit'>";
        echo "</form>";
    
    }
    
    elseif (isset ($_POST['submit']))
    
    {
      
        $score = 0;
        $total = mysql_num_rows($display);
            while ($row = mysql_fetch_array($display))
    
    
            {
    
                $answer = $row["answer"];
    			$answer1 = $row["answer1"];
                $userAnswer=$_POST["q${row['id']}"];
    
            if ($userAnswer == $answer)
            {
            $score++;
            }
    		
    		elseif (($userAnswer) == ($answer && $answer1))
            {
            $score++;
            }
    
        }
    
        echo "<p align=center><b>You scored $score out of $total</b></p>";
        echo "<p>";
    
        if   ($score == $total) {
        echo "Congratulations! You got every question right!";
        }
        elseif ($score/$total < 0.34) {
        echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
        }
        elseif ($score/$total > 0.67) {
        echo "Well done! You certainly know your stuff.";
        }
        else {
        echo "Not bad - but there were a few that caught you out!";
        }
    
    echo "</p>";
    
    echo "<p>Here are the answers:";
    
    echo "<table border=0>";
    $display = mysql_query("SELECT * FROM quiz ORDER BY id");
    while ($row = mysql_fetch_array($display)) {
    
    $question = $row["question"];
    $answer = $row["answer"];
    $answer1 = $row["answer1"];
    $userAnswer = $_POST["q${row['id']}"];
    $userAnswer1 = $_POST["q${row['id']}"];
    
    echo "<tr><td><br>$question</td></tr>";
    
    if (($userAnswer == $answer) || ($userAnswer == ($answer && $answer1)))
            {
            echo "<tr><td>&raquo;you answered $userAnswer, which is correct</td></tr>";
            }
    elseif ($userAnswer == "") {
    echo "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>";
    }
    else {
    echo "<tr><td>&raquo;you answered $userAnswer   . The answer is $answer $answer1</td></tr>";
    }
    
    }
    echo "</table></p>";
    
    
    
    }
    ?>
    
    PHP:
    with the database
    id
    q
    question
    opt1
    opt2
    opt3
    asnwer
    answer2

    This is a quiz script. My problem is that if the user selects two options from one question, I don't know how to show that in my script. Right now the script can compare only if the user selects one checkbox.
    Please, someone, give me an idea.
     
    snowLee, Dec 2, 2009 IP
  2. ebookbusiness

    ebookbusiness Member

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    The checkboxes need to be in an array format. Something like <input type=checkbox name=optcheck[] value='$opt1'>. The in your PHP code, you can compare values between two check boxes by using a while loop.
     
    ebookbusiness, Dec 3, 2009 IP