0

I'm just starting to learn php. I have a quiz, were every correct answer gets different points. I looked on the internet, but up till now I coouldn't find the solution. This SO question helped me, but one way another I can't get the total score and that is what I'm looking for. I want to display the total score on the same page. Here is the code were there is something wrong. There is some Dutch in it, but that is not the issue:

    <?php
    $strSQL="select vraag, vraagnummer, image_1, image_2, image_3, image_4, punten, antwoord ".
    "from tbvragen as v ".
    "inner join tbpunten as p ".
    "on v.id_ptn = p.id_ptn ".
    "inner join tbantwoorden as a ".
    "on v.id_antw=a.id_antw ";

    $rs = mysql_query($strSQL, $db);
    while ($r = mysql_fetch_array($rs))
    {
    ?>

    <form>
    <h3>Vraag <?php echo($r["vraagnummer"]);?>  <?php echo($r["vraag"]);?><span>(<?echo($r["punten"]); ?> punten)</span>
    </h3>
    <ul>
    <li><img src="images/<?echo($r["image_1"]); ?>"  /><input type="radio" name="keuze" value="A" /><label>A</label></li>
     <li><img src="images/<?echo($r["image_2"]); ?>" /><input type="radio" name="keuze" value="B" /><label>B</label></li>
    <li><img src="images/<?echo($r["image_3"]); ?>" /><input type="radio" name="keuze" value="C"/><label>C</label></li>
     <li><img src="images/<?echo($r["image_4"]); ?>" /><input type="radio" name="keuze" value="D" /><label>D</label></li>
     <li><input type="hidden" name="keuze" value="<?php echo($r["antwoord"]);?> " /></li>            
    </ul>
   </form>

   <?php
    }

    ?>
   <form name="quiz" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"><input type="submit" 
   class="button" value="verstuur je antwoorden" name="verstuur" /></form>






     <?php
        $rs = mysql_query($strSQL, $db);
   while ($r = mysql_fetch_array($rs))
  {


    $beantwoord=$r["vraag".isset($_POST['keuze'])];
    $juistAntw=$r["antwoord"];
    if ($r["vraagnummer"]==$r["antwoord"]) {
                echo $r["punten"].'<br />';
    }

    }
    echo ("je hebt".$r["punten"]." punten");
    ?>



      <?php

      mysql_free_result($rs);
      mysql_close($db);    
      ?>   
Community
  • 1
  • 1
lycaon
  • 15
  • 3
  • 2
    So how should we now what your score variable is if we don't speak dutch? And just DON'T use deprecated mysql_* functions. – jtheman May 09 '13 at 21:28
  • please format your code properly, it is almost unreadable. and translate to English. – michi May 09 '13 at 22:00

1 Answers1

0

If you looked at the output html source code from your php code, you would have been able to see most of your errors.

First, you are wrapping each question/answer set in its own <form></form>, but your submit button is in a separate <form></form>, so none of your answers will be posted, as they are not in the form with the action and submit button.

Second, every radio has name="keuze", so there is no way to check their answer against the question, as only the last checked radio element will set keuze.

Third, you are providing the correct answer in a hidden element with name="keuze", so even without the second issue you are overwriting their reply with the correct answer, and they can easily get the answer by looking at the page source code.

Fourth, it is difficult to understand what you are trying to do in your total score code. In order to get the total score, you need to first check to see if the form has been submitted - if(isset($_POST['verstuur'])). You then need to compare their reply to the correct answer and if the same and the question points to a total points variable.

Trying to resolve all the issues above, try doing something like this -

<?php
$strSQL="select vraag, vraagnummer, image_1, image_2, image_3, image_4, punten, antwoord ".
"from tbvragen as v ".
"inner join tbpunten as p ".
"on v.id_ptn = p.id_ptn ".
"inner join tbantwoorden as a ".
"on v.id_antw=a.id_antw ";

$rs = mysql_query($strSQL, $db);

// check to see if they submitted their quiz
if(isset($_POST['verstuur'])){
// a variable for total points
$totaalpunten = 0;
while ($r = mysql_fetch_array($rs))
 {
      // give the question number
    echo "Vraag ".$r["vraagnummer"])." - ";
      // check if their reply is the same as the correct answer
    if ($_POST["antwoord".$r["vraagnummer"]]==$r["antwoord"]) {
             // echo the points they got for this question
            echo $r["punten"]." puntens<br />";
             // add the points to the total points
            $totaalpunten += $r["punten"];
    }
    else {   
            // if wrong show 0 points
            echo "0 puntens<br />";
}
 // give their total score
echo "je hebt ".$totaalpunten." punten";

}
 // if the quiz was not submitted, show the quiz
else { ?>
<form name="quiz" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<?php
while ($r = mysql_fetch_array($rs))
{ ?>
      <fieldset>
      <h3>Vraag <?php echo($r["vraagnummer"]);?>  <?php echo($r["vraag"]);?><span>(<?echo($r["punten"]); ?> punten)</span></h3>
      <ul>
          <li><img src="images/<?echo($r["image_1"]); ?>"  /><input type="radio" name="antwoord<?php echo($r["vraagnummer"]);?>" value="A" /><label>A</label></li>
          <li><img src="images/<?echo($r["image_2"]); ?>" /><input type="radio" name="antwoord<?php echo($r["vraagnummer"]);?>" value="B" /><label>B</label></li>
          <li><img src="images/<?echo($r["image_3"]); ?>" /><input type="radio" name="antwoord<?php echo($r["vraagnummer"]);?>" value="C"/><label>C</label></li>
          <li><img src="images/<?echo($r["image_4"]); ?>" /><input type="radio" name="antwoord<?php echo($r["vraagnummer"]);?>" value="D" /><label>D</label></li>            
      </ul>
      </fieldset>
<?php  }    ?>
<input type="submit" class="button" value="verstuur je antwoorden" name="verstuur" />
</form>
<?php
}
mysql_free_result($rs);
mysql_close($db);    
?>   
Sean
  • 12,443
  • 3
  • 29
  • 47
  • Sean, thanks for your help. The comments are very welcome, as I'm a learning php. If it works I'll buy you a beer, else I buy you a beer. – lycaon May 10 '13 at 13:25