PHP quiz Problem

Discussion in 'Programming' started by mbu_bashir, Jan 9, 2011.

  1. #1
    I have a php quiz script.

    When i click submit, it successfully outputs the total number of correct answers.
    but when i submit a query for random select then the result of a quiz is wrong please help me as soon as possible:

    the code is:

    <?php

    include("contentdb.php");
    $display = mysql_query("SELECT * FROM $table ORDER BY id",$db);

    if (!$_POST['submit']) {

    echo "<form method=post action=$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"];

    echo "<tr><td colspan=3><br><b>$question</b></td></tr>";

    echo "<tr><td>$opt1 <input type=radio name='q$id' value=\"$opt1\"></td>
    <td>$opt2 <input type=radio name='q$id' value=\"$opt2\"></td>
    <td>$opt3 <input type=radio name='q$id' value=\"$opt3\"></td></tr>";

    }

    echo "</table>";

    echo "<input type='submit' value='See how you did' name='submit'>";
    echo "</form>";

    } elseif ($_POST['submit']) {

    $score = 0;
    $total = mysql_num_rows($display);

    while ($result = mysql_fetch_array($display)) {

    $answer = $result['answer'];
    $q = "q$result[id]";
    $q = trim($q);
    if ($_POST[$q] == $answer) {
    $score++;
    }
    }

    echo "<p align=center><b>You scored $score out of $total</b></p>";
    echo "<p>";
    }
    ?>

    If I add the query as like
    $display = mysql_query("SELECT * FROM $table ORDER BY RAND() LIMIT 4",$db);
    It wouldn't work exactly, such as answer will come wrong.
    e.g if i can select all of the correct answer but answer come 40% or 50% or other

    please help me.
    please link send to facebook.riseuplabs@gmail.com
    with best regards
    Bashir
     
    Last edited: Jan 9, 2011
    mbu_bashir, Jan 9, 2011 IP
  2. Lam3r

    Lam3r Active Member

    Messages:
    235
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #2
    $q = "q$result[id]"; 
    Code (markup):
    should be either $q = "q$result['id']"; or $q = "q$result[$id]";
     
    Lam3r, Jan 9, 2011 IP
  3. mbu_bashir

    mbu_bashir Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you for reply!
    but it was not work correctly
    when i use this:
    $q = "q$result['id']"; the error is folloing:
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\newquiz\test5.php on line 41

    if i use
    $q = "q$result[$id]";
    then out put is 0
    this is the problem when i select question from database randomly.
     
    mbu_bashir, Jan 9, 2011 IP
  4. jkl6

    jkl6 Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Print out the response and answer and you should see what is causing the problem. My guess is that the problem is your data, not the code. When you do a regular select, you are always testing the same 4 questions. When you do a random select, you are randomly testing all your question. Some of your answers doesn't match up with the answer options 100%.

    echo $_POST[$q];
    echo $answer;
    if ($_POST[$q] == $answer) {  ...
    Code (markup):
     
    jkl6, Jan 9, 2011 IP
  5. mbu_bashir

    mbu_bashir Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you jkl6 for your suggestion.
    Also i have the problem, this is:
    if i use:
    echo $_POST[$q]."</br>"; then all selected answer is not render, only show the answer which is correct and
    for the following:
    echo $answer."</br>"; showing the answer from database but not the answer of that question which is show before submit.
    Actually answer from database and selected is come randomly for that score is come randomly.
    what's the problem I can't find it.
    Please help me.
     
    mbu_bashir, Jan 9, 2011 IP