Question on how to compare value while selected radio button
Results 1 to 3 of 3

Thread: Question on how to compare value while selected radio button

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2

    Question on how to compare value while selected radio button

    PHP Code:
    //display student exam question based on subject number
            
    $id=$_GET['id'];
            
    $sql="SELECT MAX(eqid) FROM fyp_studentexamquestion where subjectnumber='$id'";
            
    $result=mysql_query($sql) or die (mysql_error());
            
    $maxid =mysql_fetch_row($result);
            
    $maxid=$maxid[0];
                                    
            
    $sql="SELECT * FROM fyp_studentexamquestion where subjectnumber='$id' and RAND()<(SELECT ((30/COUNT(*))*10) FROM fyp_studentexamquestion) ORDER BY RAND() LIMIT 30;";
            
    $result=mysql_query($sql) or die (mysql_error());
            
    $row mysql_num_rows($result);
            
            if(
    $row 0)
            {               
                    
    $a 1;
                    
    $countrandom=1;
                    echo 
    "<form method=\"POST\" action=\"";
                    echo 
    htmlentities($_SERVER['PHP_SELF']);
                    echo 
    "?id=";
                    echo 
    $_GET['id'];
                    echo 
    "\">";
                    while(
    $row mysql_fetch_assoc($result))
                    {
                            if(
    $countrandom<=$counttotal//compare total question and set question amount
                            
    {
                                    if(
    $a <= $maxid// loop for question
                                    
    {
                                    echo 
    "Question:".$a."<br />";
                                    
    $a++ ;
                                    }
                                    echo 
    $row["questions"]."<br />";
                                    
                                    echo 
    '<input type="radio" name="q' $row["eqid"] . '_answer" value="A" />'.$row["answer1"].'<br />';
                                    echo 
    '<input type="radio" name="q' $row["eqid"] . '_answer" value="B" />'.$row["answer2"].'<br />';
                                    echo 
    '<input type="radio" name="q' $row["eqid"] . '_answer" value="C" />'.$row["answer3"].'<br />';
                                    echo 
    '<input type="radio" name="q' $row["eqid"] . '_answer" value="D" />'.$row["answer4"].'<br />';
                                    echo 
    "<br />";
                                    
    $_SESSION["q{$row["eqid"]}_correct_answer"] = $row["correctanswer"];
                                    
    $countrandom++;
                            }
                    }
                    echo 
    '<input type="submit" name="submit" class="buttonmodify" value="Submit Answers"/>'.'<br />';
                    echo 
    '<input type="hidden" name="submitted" value="TRUE" />';
                    echo 
    "<br />";
                    echo 
    '</form>';

            } 
    i can show every eqid into q[eqid]answer and answer by looping
    but problem is. once i press the submitted button
    i got no idea how to check with it.

    as you can see im looping my while loop to get all the question and answer
    but once the user click the radio button, how can it know the answer compare with correctanswer based on the eqid and display correct answer?
    any suggestion ?

    The value for epid is integer.
    Example :
    q1_answer = "A"
    q1_answer = "B"
    q1_answer ="C"
    q1_answer ="D"

    if i checked on D
    how should it know?
    and i wan compare the $row["answer"] with $row["correctanswer"]..
    how could it done ><

  2. #2
    Senior Member anoopmail's Avatar
    Join Date
    May 2005
    Location
    Hell
    Posts
    241
    Instead of $_SESSION["q{$row["eqid"]}_correct_answer"] you may store it as array wghich is easy to manipulate using loops.
    PHP Code:
    $_SESSION["correct_answer"][{$row["eqid"]}] = $row["correctanswer"] ; 
    PHP Code:
    if(isset($_POST['submit'])){
    foreach(
    $_SESSION["correct_answer"] as $eqid => $crt_answer){
    $radio_name 'q' $eqid ;
    if(
    $_POST[$radio_name] == $crt_answer){
      echo 
    sprintf('%s Correct'$eqid) ;
    }else{
      echo 
    sprintf('%s Incorrect'$eqid) ;
    }

    }

    Of cause this code in not optimized , can be better.
    Last edited by anoopmail; 04-12-2012 at 09:48 AM. Reason: added php tags

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Quote Originally Posted by anoopmail View Post
    Instead of $_SESSION["q{$row["eqid"]}_correct_answer"] you may store it as array wghich is easy to manipulate using loops.
    PHP Code:
    $_SESSION["correct_answer"][{$row["eqid"]}] = $row["correctanswer"] ; 
    PHP Code:
    if(isset($_POST['submit'])){
    foreach(
    $_SESSION["correct_answer"] as $eqid => $crt_answer){
    $radio_name 'q' $eqid ;
    if(
    $_POST[$radio_name] == $crt_answer){
      echo 
    sprintf('%s Correct'$eqid) ;
    }else{
      echo 
    sprintf('%s Incorrect'$eqid) ;
    }

    }

    Of cause this code in not optimized , can be better.
    Thanks, i will try it out.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •