Hi all. I have two tables (question and answer) with q_id as the link. I would like to output the data that each table holds alternately like this:
[Question 1]
[Question 1 Choice of Answers]
[Question 2]
[Question 2 Choice of Answers]
...and so on. I've managed to filter the answer sets by q_id but now they do not tally with the question. I attached an image of the output. The $id alternates between the questions and answer sets. Commenting out the $id++; line has the same outcome.
I hope someone can point me in the right direction. Thanks in advance for the help.
-Atikah-
TABLES
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`)
)
INSERT INTO `answer` (`a_id`, `q_id`, `a_text`, `a_value`) VALUES
(1, 1, 'Data', 1),
(2, 1, 'System data', 0),
(3, 1, 'Information', 0),
(4, 1, 'Storage', 0),
(7, 2, 'Integrated buses for data and instructions', 0),
(8, 2, 'Faster for a given circuit complexity because instruction fetches and data access contend for a single memory pathway', 0),
(9, 2, 'CPU can both read an instruction and perform data memory access at the same time without a cache', 1),
(10, 2, 'Efficient execution due to the non-overlapping of Operand Fetch and Instruction Fetch', 0),
(11, 3, 'I, II and IV', 0),
(12, 3, 'I and II', 1),
(13, 3, 'III and IV', 0),
(14, 3, 'II and III', 0),
(15, 4, 'True', 1),
(16, 4, 'False', 0);
OUTPUT PAGE
<!-- ANSWER CHOICE IN SUBTABLE -->
<?php
ini_set('display_errors',1);
error_reporting(E_ALL ^ E_NOTICE);
session_start();
if(isset($_SESSION['idno']))
{
?>
<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="">
<p>Questions</p>
<table width="900" border="1" cellpadding="10">
<?php
include("dbconn.php");
//$id = $_SESSION['s_id'];
$sql = "SELECT q_id, q_qstn_no, q_text FROM question";
$query = mysql_query($sql) or die("MySQL Error: " . mysql_error());
$row = mysql_num_rows($query);
if($row != 0){
while($data = mysql_fetch_assoc($query)){
$row1 = mysql_fetch_array($query);
$id = $row1["q_id"];
echo $id;
//$sql_id = "SELECT q_id FROM question";
//$query_id = mysql_query($sql_id) or die("MySQL Error: " . mysql_error());
?>
<tr>
<td width="25" align="center" valign="top"><?php echo $data['q_qstn_no']; ?></td>
<td><?php echo $data['q_text']; ?>
<br><table width="750" border="1">
<?php
$sql2 = "SELECT a_id, a_text, a_value FROM answer WHERE answer.q_id = '$id'";
$query2 = mysql_query($sql2) or die("MySQL Error: " . mysql_error());
$row_ans = mysql_num_rows($query2);
if($row_ans != 0){
while($data_ans = mysql_fetch_assoc($query2)){
?>
<tr>
<td><label><input name="answer" type="radio" value="<?php echo $data_ans['a_text']; ?>">
<?php echo $data_ans['a_text']; ?></label></td>
</tr>
<?php
$id++;
}
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</form>
</body>
</html>
<?php
}
else
{
header("Location:login.php");
}
?>