Adding a score in a quiz with php and mysql were answers heeft different points -


i'm starting learn php. have quiz, every correct answer gets different points. looked on internet, till coouldn't find solution. this question helped me, 1 way can't total score , i'm looking for. want display total score on same page. here code there wrong. there dutch in it, not issue:

    <?php     $strsql="select vraag, vraagnummer, image_1, image_2, image_3, image_4, punten, antwoord ".     "from tbvragen v ".     "inner join tbpunten p ".     "on v.id_ptn = p.id_ptn ".     "inner join tbantwoorden ".     "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);           ?>    

if looked @ output html source code php code, have been able see of errors.

first, wrapping each question/answer set in own <form></form>, submit button in separate <form></form>, none of answers posted, not in form action , submit button.

second, every radio has name="keuze", there no way check answer against question, last checked radio element set keuze.

third, providing correct answer in hidden element name="keuze", without second issue overwriting reply correct answer, , can answer looking @ page source code.

fourth, difficult understand trying in total score code. in order total score, need first check see if form has been submitted - if(isset($_post['verstuur'])). need compare reply correct answer , if same , question points total points variable.

trying resolve issues above, try doing -

<?php $strsql="select vraag, vraagnummer, image_1, image_2, image_3, image_4, punten, antwoord ". "from tbvragen v ". "inner join tbpunten p ". "on v.id_ptn = p.id_ptn ". "inner join tbantwoorden ". "on v.id_antw=a.id_antw ";  $rs = mysql_query($strsql, $db);  // check see if submitted quiz if(isset($_post['verstuur'])){ // variable total points $totaalpunten = 0; while ($r = mysql_fetch_array($rs))  {       // give question number     echo "vraag ".$r["vraagnummer"])." - ";       // check if reply same correct answer     if ($_post["antwoord".$r["vraagnummer"]]==$r["antwoord"]) {              // echo points got question             echo $r["punten"]." puntens<br />";              // add points total points             $totaalpunten += $r["punten"];     }     else {                // if wrong show 0 points             echo "0 puntens<br />"; }  // give total score echo "je hebt ".$totaalpunten." punten";  }  // if quiz not submitted, show 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);     ?>    

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -