Hi all. I'm doing a page for multiple choice questions. The question and answer choice are in two separate database tables (question and answer). The layout of the page is like this:

Question 1
Answer choice (4 or 2 radio buttons)

Question 2
Answer choice (4 or 2 radio buttons)

...and so on. Managed this part with the help from people here too (link) but I overlooked what was going on with the radio buttons. I was doing the code for points calculation and couldn't get the no. of correct answers.

TABLE STRUCTURE

CREATE TABLE IF NOT EXISTS `question` (
  `q_id` int(10) NOT NULL AUTO_INCREMENT,
  `q_qstn_no` int(11) NOT NULL,
  `q_text` varchar(300) NOT NULL,
  `q_chpt` int(11) NOT NULL,
  PRIMARY KEY (`q_id`)
)
CREATE TABLE IF NOT EXISTS `answer` (
  `a_id` int(6) NOT NULL AUTO_INCREMENT,
  `q_id` int(10) NOT NULL,
  `a_text` varchar(255) NOT NULL,
  `a_value` tinyint(1) NOT NULL,
  PRIMARY KEY (`a_id`)
)

This code below currently detects all the radio buttons (answers for differet questions) as one group. When I select an answer for question 2 it takes away answer for question 1, same goes for question 2 when answering question 3. I tried putting the q_id value as the button value (line 41) hoping that would somewhat distinguish the buttons but it didn't work.

QUESTION PAGE

<!-- ANSWER CHOICE IN SUBTABLE -->
<?php
ini_set('display_errors',1);
error_reporting(E_ALL ^ E_NOTICE);
session_start();
if(isset($_SESSION['tf1_sid']))
{
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
</head>
<body>
    <form id="form1" name="form1" method="post" action="ques_page_calc0.php">
    <p>Questions</p>
    <table width="900" border="0" cellpadding="4">
    <?php
        // db connect
        include("dbconn.php");
        // db query for questions
        $sql_q  = "SELECT q_id, q_qstn_no, q_text FROM question";
        $query_q = mysql_query($sql_q) or die("MySQL Error: " . mysql_error());
        // start loop for questions
        while($data_q = mysql_fetch_array($query_q, MYSQL_ASSOC)){

//        echo "<pre>";
//        print_r($data_q);
//        echo "</pre>";
//        


            echo "<tr><td width='25' align='center' valign='top'><label><input name='q_no' size='1' type='hidden' value=". $data_q['q_qstn_no'] .">". $data_q['q_qstn_no'] ."</label></td>";
            echo "<td>". $data_q['q_text'] ."<br />";
            // db query for answers
            $sql_a = "SELECT a_id, a_text, a_value FROM answer WHERE q_id=".$data_q['q_id'];
            $query_a = mysql_query($sql_a) or die("MySQL Error: " . mysql_error());
            // start loop for answers
            while($data_a = mysql_fetch_array($query_a, MYSQL_ASSOC)){
                echo "<br /><table width='750' border='0'>";
                echo "<tr><td><label><input name='answer' type='radio' value=". $data_a['a_value'] ." id=". $data_q['q_id'] .">". $data_a['a_text'] . "</label></td></tr>";
                echo "</table>";
            }
            echo "</tr>";
        }
        echo "<tr><td><input name='Submit' type='submit' onClick='ques_page_calc0.php' value='submit'></td></tr>";       
        mysql_free_result($query_q);
        mysql_free_result($query_a);
        include("dbconn.php");

?>
    </table>
    </form>
</body>
</html>
<?php
}
else
{
header("Location:s_login.php");
}
?>

Here's the code that I got stuck with calculating the points.

POINTS CALCULATION

<?php
ini_set('display_errors',1);
error_reporting(E_ALL ^ E_NOTICE);
// include db connection file
include("dbconn.php");

if(isset($_POST['Submit']))
{   
    $id = $_SESSION['tf1_sid'];
    echo $id;
    $correct = 0;
    $ev_id = 1;

    //db query to obtain i_id
    $sql_i = "SELECT i_id FROM ins_stud WHERE s_id = '$id'";
    $query_i = mysql_query($sql_i) or die("MySQL Error: " . mysql_error());
    $data_i = mysql_fetch_array($query_i, MYSQL_ASSOC);
    echo $data_i;

    // capture values from HTML form
    if(is_array($_POST['q_no']))
    {
        while($ans=$_POST['answer'])
        {
            //$ans=$_GET['answer'];
            if($ans == 1)
                $correct = $correct + 1;

            echo $correct;


//  // insert answer to table
//  $sql_eval = "INSERT INTO eval_set (ev_id, q_id, response, created) VALUES ('" . $ev_id . "', '" . $ques_no . "', '" . $ans . "', CURDATE())";
//  mysql_query($sql_eval) or die ("Error: " . mysql_error());
        }
    }
//  
//  // insert result to table
//  $sql_result = "INSERT INTO result (r_score, ev_id, s_id, i_id) VALUES ('" . $correct . "', '" . $ev_id . "',  '" . $id . "',  '" . $data_i . "')";
//  mysql_query($sql_result) or die ("Error: " . mysql_error());

//echo "Result: " . $correct . " questions correct.";   

//header("Location:ass_result.php"); 
}

// close db connection
mysql_close($dbconn);
?>

Is there workaround to this? Any help would be greatly appreciated. Thanks.

Regards,
Atikah

I've searched for this problem again and found a help from this post (link). I added an array to answer in line 7. I can select more than one radio button now but for some reason can only do so for either of the three questions.

For example, when I answer the questions in order, when I get to question 4 and select the answer, the selection I made question 1 disappears. Can anyone point me in the right direction? Thanks.

            $sql_a = "SELECT a_id, a_text, a_value FROM answer WHERE q_id=".$data_q['q_id'];
            $query_a = mysql_query($sql_a) or die("MySQL Error: " . mysql_error());
            //$rad = 1;
            // start loop for answers
            while($data_a = mysql_fetch_array($query_a, MYSQL_ASSOC)){
                echo "<br /><table width='750' border='0'>";
                echo "<tr><td><label><input name='answer[]' type='radio' value=". $data_a['a_value'] .">". $data_a['a_text'] . "</label></td></tr>";
                echo "</table>";

            }
            echo "</tr>";
            //$rad++;
Member Avatar for diafol

That php-derived html is difficult to follow. Why so many tables? You seem tot be creating a new table for every answer.

When I answer the questions in order, when I get to question 4 and select the answer, the selection I made question 1 disappears.

Don't make radiobutton names arrays - just the name will do, unless you want something like this:

PSEUDOCODE

foreach question:
    print question
    ...
    loop answers with this: <input name="answer[$q_id]" value="{$data_a['a_value']}" type="radio" />...
end foreach

However, you seem to be running many queries, I'd suggest a JOINed sql so that you retrieve the question data and the answer data in one query. If you ORDER BY q_id, then all answers will be grouped with the question. You just create a new question in HTML when the value of q_id changes. Just one approach though.

Thanks for the reply diafol. I've changed the name and have it concenated to a variable in the code below. This is what you meant right? :)

        // start loop for questions
        $rad2 = 0;
        while($data_q = mysql_fetch_array($query_q, MYSQL_ASSOC)){       

            echo "<tr><td width='25' align='center' valign='top'><label><input name='q_no' size='1' type='hidden' value=". $data_q['q_qstn_no'] .">". $data_q['q_qstn_no'] ."</label></td>";
            echo "<td>". $data_q['q_text'] ."<br />";
            // db query for answers
            $sql_a = "SELECT a_id, a_text, a_value FROM answer WHERE q_id=".$data_q['q_id'];
            $query_a = mysql_query($sql_a) or die("MySQL Error: " . mysql_error());
            //$rad = 0;
            // start loop for answers
            while($data_a = mysql_fetch_array($query_a, MYSQL_ASSOC)){
                echo "<br /><table width='750' border='0'>";
                echo "<tr><td><label><input name='answer_".$rad2."' type='radio' value=". $data_a['a_value'] .">". $data_a['a_text'] . "</label></td></tr>";
                echo "</table>";
            }
            echo "</tr>";
            $rad2++;

As for the JOIN sql, won't the code output the questions multiple times depending on the iteration of the radio buttons if I queried them at once?

675c7605111967bf9bee1631890674c0

I also tried this query below but as you can see it only displays the answers.

SELECT a_id, a_text, a_value FROM answer
JOIN question ON question.q_id=answer.q_id
ORDER BY q_id

Am I missing something here?

That php-derived html is difficult to follow. Why so many tables? You seem tot be creating a new table for every answer.

Were you referring to the POINTS CALCULATION code? What I intend to do is insert student response for each question into the eval_set table (after accumulating the correct response) hence the INSERT INTO in the while loop. The result table only holds the overall result (ie total of correct response). I haven't had the chance to test the code because of the radio button problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.