SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    id field missing the continuity

    hello,
    this is a simple question and answer script sort of multiple choice.
    When the user submits the answer for the first question, i show if the answer is correct or wrong and then proceed to next question.Now, the user submits the answer and now, i see the answer is displaying for the first one rather than the 2nd one and when i click on next question, i get the second question again rathern than the 3rd question.In total there are 5 questions in my table and i see the id field is missing the continuity.You can see the working example
    http://inkaytown.f2o.org/spiele/quizpage2.php
    Could somebody please tell me where the error would be.

    Thanks in advance

    PHP Code:
    [b][size=2][color=#ff0000]<?php
    [/color][/size][/b][size=2][color=#ff0000][/color][/size][size=2][b]include[/b]([/size][size=2][color=#0000f0]"contentdb.php"[/color][/size][size=2]);
    [/size][size=2][color=#800000]$id[/color][/size][size=2] = [/size][size=2][color=#800000]$HTTP_GET_VARS[/color][/size][size=2][[/size][size=2][color=#0000f0]"id"[/color][/size][size=2]]; 
    [b]if[/b] ([/size][size=2][color=#800000]$id[/color][/size][size=2] == [/size][size=2][color=#0000f0]""[/color][/size][size=2])
    {
    [/
    size][size=2][color=#800000]$id[/color][/size][size=2] = [/size][size=2][color=#0000f0]"0"[/color][/size][size=2];
    }
    [/
    size][size=2][color=#800000]$display[/color][/size][size=2] = mysql_query([/size][size=2][color=#0000f0]"SELECT * FROM [/color][/size][b][size=2]$table[/size][/b][size=2][/size][size=2][color=#0000f0] WHERE id > [/color][/size][b][size=2]$id[/size][/b][size=2][/size][size=2][color=#0000f0] ORDER BY id ASC LIMIT 1 "[/color][/size][size=2],[/size][size=2][color=#800000]$db[/color][/size][size=2]);
    [b]if[/b] (![/size][size=2][color=#800000]$submit[/color][/size][size=2]) {
    echo [/size][size=2][color=#0000f0]"<form method=post action=[/color][/size][b][size=2]$PHP_SELF[/size][/b][size=2][/size][size=2][color=#0000f0]>"[/color][/size][size=2];
    echo [/size][size=2][color=#0000f0]"<table border=0>"[/color][/size][size=2];
    [b]while[/b] ([/size][size=2][color=#800000]$row[/color][/size][size=2] = mysql_fetch_array([/size][size=2][color=#800000]$display[/color][/size][size=2])) {
    [/size][size=2][color=#800000]$id[/color][/size][size=2] = [/size][size=2][color=#800000]$row[/color][/size][size=2][[/size][size=2][color=#0000f0]"id"[/color][/size][size=2]];
    [/size][size=2][color=#800000]$question[/color][/size][size=2] = [/size][size=2][color=#800000]$row[/color][/size][size=2][[/size][size=2][color=#0000f0]"question"[/color][/size][size=2]];
    [/size][size=2][color=#800000]$opt1[/color][/size][size=2] = [/size][size=2][color=#800000]$row[/color][/size][size=2][[/size][size=2][color=#0000f0]"opt1"[/color][/size][size=2]];
    [/size][size=2][color=#800000]$opt2[/color][/size][size=2] = [/size][size=2][color=#800000]$row[/color][/size][size=2][[/size][size=2][color=#0000f0]"opt2"[/color][/size][size=2]];
    [/size][size=2][color=#800000]$opt3[/color][/size][size=2] = [/size][size=2][color=#800000]$row[/color][/size][size=2][[/size][size=2][color=#0000f0]"opt3"[/color][/size][size=2]];
    [/size][size=2][color=#800000]$answer[/color][/size][size=2] = [/size][size=2][color=#800000]$row[/color][/size][size=2][[/size][size=2][color=#0000f0]"answer"[/color][/size][size=2]];
    echo [/size][size=2][color=#0000f0]"<tr><td colspan=3><br><b>[/color][/size][b][size=2]$question[/size][/b][size=2][/size][size=2][color=#0000f0]</b></td></tr>"[/color][/size][size=2];
    echo [/size][size=2][color=#0000f0]"<tr><td>[/color][/size][b][size=2]$opt1[/size][/b][size=2][/size][size=2][color=#0000f0] <input type=radio name=q[/color][/size][b][size=2]$id[/size][/b][size=2][/size][size=2][color=#0000f0] value=[/color][/size][size=2][color=#008000]\"[/color][/size][b][size=2]$opt1[/size][/b][size=2][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0]></td><td>[/color][/size][b][size=2]$opt2[/size][/b][size=2][/size][size=2][color=#0000f0] <input type=radio name=q[/color][/size][b][size=2]$id[/size][/b][size=2][/size][size=2][color=#0000f0] value=[/color][/size][size=2][color=#008000]\"[/color][/size][b][size=2]$opt2[/size][/b][size=2][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0]></td><td>[/color][/size][b][size=2]$opt3[/size][/b][size=2][/size][size=2][color=#0000f0] <input type=radio name=q[/color][/size][b][size=2]$id[/size][/b][size=2][/size][size=2][color=#0000f0] value=[/color][/size][size=2][color=#008000]\"[/color][/size][b][size=2]$opt3[/size][/b][size=2][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0]></td></tr>"[/color][/size][size=2];
    }
    echo [/
    size][size=2][color=#0000f0]"</table>"[/color][/size][size=2];
    echo [/size][size=2][color=#0000f0]"<input type=[/color][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0]hidden[/color][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0] name=[/color][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0]id[/color][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0] value=[/color][/size][size=2][color=#008000]\"[/color][/size][b][size=2]$id[/size][/b][size=2][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0]>"[/color][/size][size=2];
    echo [/size][size=2][color=#0000f0]"<input type='submit' value='See how you did' name='submit'>"[/color][/size][size=2];
    echo [/size][size=2][color=#0000f0]"</form>"[/color][/size][size=2];
    }
    [
    b]elseif[/b] ([/size][size=2][color=#800000]$submit[/color][/size][size=2]) 
    {
    [/
    size][size=2][color=#c0c000]/*
    $score 0;
    $total mysql_num_rows($display);
    while (
    $result mysql_fetch_array($display)) 


    {

    $answer $result["answer"];
    $q $result["q"];

    if ($
    $q == $answer
    {
    $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!";
    }
    */
    [/
    color][/size][size=2]echo [/size][size=2][color=#0000f0]"</p>"[/color][/size][size=2];
    echo [/size][size=2][color=#0000f0]"<p>Here are the answers:"[/color][/size][size=2];
    echo [/size][size=2][color=#0000f0]"<table border=0>"[/color][/size][size=2];
    [/size][size=2][color=#800000]$display[/color][/size][size=2] = mysql_query([/size][size=2][color=#0000f0]"SELECT * FROM [/color][/size][b][size=2]$table[/size][/b][size=2][/size][size=2][color=#0000f0] WHERE id > [/color][/size][b][size=2]$id[/size][/b][size=2][/size][size=2][color=#0000f0] ORDER BY id ASC LIMIT 1 "[/color][/size][size=2],[/size][size=2][color=#800000]$db[/color][/size][size=2]);
    [b]while[/b] ([/size][size=2][color=#800000]$row[/color][/size][size=2] = mysql_fetch_array([/size][size=2][color=#800000]$display[/color][/size][size=2])) {
    [/size][size=2][color=#800000]$question[/color][/size][size=2] = [/size][size=2][color=#800000]$row[/color][/size][size=2][[/size][size=2][color=#0000f0]"question"[/color][/size][size=2]];
    [/size][size=2][color=#800000]$answer[/color][/size][size=2] = [/size][size=2][color=#800000]$row[/color][/size][size=2][[/size][size=2][color=#0000f0]"answer"[/color][/size][size=2]];
    [/size][size=2][color=#800000]$q[/color][/size][size=2] = [/size][size=2][color=#800000]$row[/color][/size][size=2][[/size][size=2][color=#0000f0]"q"[/color][/size][size=2]];
    echo [/size][size=2][color=#0000f0]"<tr><td><br>[/color][/size][b][size=2]$question[/size][/b][size=2][/size][size=2][color=#0000f0]</td></tr>"[/color][/size][size=2];
    [b]if[/b] ([/size][size=2][color=#800000]$$q[/color][/size][size=2] == [/size][size=2][color=#800000]$answer[/color][/size][size=2]) 
    {
    echo [/
    size][size=2][color=#0000f0]"<tr><td>&raquo;you answered ${[/color][/size][b][size=2]$q[/size][/b][size=2][/size][size=2][color=#0000f0]}, which is correct</td></tr>"[/color][/size][size=2];
    [/size][size=2][color=#800000]$id[/color][/size][size=2] = [/size][size=2][color=#800000]$id[/color][/size][size=2]+[/size][size=2][color=#ff0000]1[/color][/size][size=2]; 
    echo([/size][size=2][color=#0000f0]"<a href=""[/color][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0]quizpage2.php?id=[/color][/size][b][size=2]$id[/size][/b][size=2][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0]>Next question</a>"[/color][/size][size=2]);
    }
    [
    b]elseif[/b] ([/size][size=2][color=#800000]$$q[/color][/size][size=2] == [/size][size=2][color=#0000f0]""[/color][/size][size=2]) 
    {
    echo [/
    size][size=2][color=#0000f0]"<tr><td>&raquo;you didn't select an answer. The answer is [/color][/size][b][size=2]$answer[/size][/b][size=2][/size][size=2][color=#0000f0]</td></tr>"[/color][/size][size=2];
    [/size][size=2][color=#800000]$id[/color][/size][size=2] = [/size][size=2][color=#800000]$id[/color][/size][size=2]+[/size][size=2][color=#ff0000]1[/color][/size][size=2]; 
    echo([/size][size=2][color=#0000f0]"<a href=""[/color][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0]quizpage2.php?id=[/color][/size][b][size=2]$id[/size][/b][size=2][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0]>Next question</a>"[/color][/size][size=2]);
    }
    [
    b]else[/b] {
    echo [/
    size][size=2][color=#0000f0]"<tr><td>&raquo;you answered ${[/color][/size][b][size=2]$q[/size][/b][size=2][/size][size=2][color=#0000f0]},which is wrong. The answer is [/color][/size][b][size=2]$answer[/size][/b][size=2][/size][size=2][color=#0000f0]</td></tr>"[/color][/size][size=2];
    [/size][size=2][color=#800000]$id[/color][/size][size=2] = [/size][size=2][color=#800000]$id[/color][/size][size=2]+[/size][size=2][color=#ff0000]1[/color][/size][size=2]; 
    echo([/size][size=2][color=#0000f0]"<a href=""[/color][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0]quizpage2.php?id=[/color][/size][b][size=2]$id[/size][/b][size=2][/size][size=2][color=#008000]\"[/color][/size][size=2][color=#0000f0]>Next question</a>"[/color][/size][size=2]);
    }
    [/
    size][size=2][color=#c0c000]/*
    switch ($$q) {
    case 
    0:
    print 
    "<tr><td>&raquo;you answered ${$q}, which is correct</td></tr>";
    $id $id+1
    print(
    "<a href=""\"quizpage2.php?id=$id\">Next question</a>");
    case 
    1:
    print 
    "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>";
    $id $id+1
    print(
    "<a href=""\"quizpage2.php?id=$id\">Next question</a>");
    break;
    case 
    2:
    print 
    "<tr><td>&raquo;you answered ${$q},which is wrong. The answer is $answer</td></tr>";
    $id $id+1
    print(
    "<a href=""\"quizpage2.php?id=$id\">Next question</a>");
    break;
    }
    */
    [/
    color][/size][size=2]}
    echo [/
    size][size=2][color=#0000f0]"</table></p>"[/color][/size][size=2];
    }
    [/
    size][b][size=2][color=#ff0000]?>
    [/color][/size][/b][size=2][color=#ff0000][/color][/size]

  2. #2
    SitePoint Addict BenANFA's Avatar
    Join Date
    Apr 2003
    Location
    Bath, UK
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are submitting your form using the POST methodso when you access $HTTP_GET_VARS there is no data there.

    Either submit your form using the GET method or use $_REQUEST variable to access the data which contains all POST and GET variables.

    Posting the code in a more readable format too would help us help you.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello,

    I tried edit my code several times and it does'nt respond,huh!Now, I attached my file as a txt doc.could you please explain me further.

    why the id field is'nt responding after submitting the second question.

    Thanks a lot
    Attached Files Attached Files

  4. #4
    SitePoint Addict BenANFA's Avatar
    Join Date
    Apr 2003
    Location
    Bath, UK
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First lets get up the code in a readable format :-)

    PHP Code:
    <?php
    include("contentdb.php");
    $id $HTTP_GET_VARS["id"]; 
    if (
    $id == "")
    {
      
    $id "0";
    }

    $display mysql_query("SELECT * FROM $table WHERE id > $id ORDER BY id ASC LIMIT 1 ",$db);
    if (!
    $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=\"hidden\" name=\"id\" value=\"$id\">";
      echo 
    "<input type='submit' value='See how you did' name='submit'>";
      echo 
    "</form>";
    }
    elseif (
    $submit
    {
    /*
      $score = 0;
      $total = mysql_num_rows($display);
      while ($result = mysql_fetch_array($display)) 
      {
        $answer = $result["answer"];
        $q = $result["q"];

        if ($$q == $answer) 
        {
          $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 $table WHERE id > $id ORDER BY id ASC LIMIT 1 ",$db);
      while (
    $row mysql_fetch_array($display)) 
      {
        
    $question $row["question"];
        
    $answer $row["answer"];
        
    $q $row["q"];
        echo 
    "<tr><td><br>$question</td></tr>";
        if ($
    $q == $answer
        {
          echo 
    "<tr><td>&raquo;you answered ${$q}, which is correct</td></tr>";
          
    $id $id+1
          echo(
    "<a href=""\"quizpage2.php?id=$id\">Next question</a>");
        }
        elseif ($
    $q == ""
        {
          echo 
    "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>";
          
    $id $id+1
          echo(
    "<a href=""\"quizpage2.php?id=$id\">Next question</a>");
        }
        else 
        {
          echo 
    "<tr><td>&raquo;you answered ${$q},which is wrong. The answer is $answer</td></tr>";
          
    $id $id+1
          echo(
    "<a href=""\"quizpage2.php?id=$id\">Next question</a>");
        }
    /*
        switch ($$q) 
        {
        case 0:
          print "<tr><td>&raquo;you answered ${$q}, which is correct</td></tr>";
          $id = $id+1; 
          print("<a href=""\"quizpage2.php?id=$id\">Next question</a>");
        case 1:
          print "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>";
          $id = $id+1; 
          print("<a href=""\"quizpage2.php?id=$id\">Next question</a>");
          break;
        case 2:
          print "<tr><td>&raquo;you answered ${$q},which is wrong. The answer is $answer</td></tr>";
          $id = $id+1; 
          print("<a href=""\"quizpage2.php?id=$id\">Next question</a>");
          break;
        }
    */
      
    }
      echo 
    "</table></p>";
    }
    ?>

  5. #5
    SitePoint Addict BenANFA's Avatar
    Join Date
    Apr 2003
    Location
    Bath, UK
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK now the second code line
    PHP Code:
    $id $HTTP_GET_VARS["id"]; 
    is trying to access data that has been submitted to the URL using the GET method, however on the 7th(ish) code line
    PHP Code:
    echo "<form method=post action=$PHP_SELF>"
    you are writing a form that submits the data using the POST method.

    There is nothing wrong with this but the way you are reading the data will not work.

    You need to either sumbit the form using the GET method
    PHP Code:
    echo "<form method=get action=$PHP_SELF>"
    or access the data in the php script using the _REQUEST global variable that contains all GET, POST, COOKIE and SESSION data
    PHP Code:
    $id $_REQUEST["id"]; 
    The id field isn't resonding after answering the first question it is just that you default to the first question at the top of the script so it works for the first question.

    Also I suspect that where you have the line
    PHP Code:
          echo("<a href=""\"quizpage2.php?id=$id\">Next question</a>" ); 
    you need
    PHP Code:
          $next $id+1;
          echo(
    "<a href=""\"quizpage2.php?id=$next\">Next question</a>" ); 
    Also you have this in 3 places however you could just put it in 1 place (after the if statement).


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
  •