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