我有问题表,主题表和问题表包含在线考试的主题明智问题。
我需要以主题名称作为标题来获取主题明智的问题,并以主题序列号显示所有问题,例如:数学:Q1,Q2,Q3 英语:Q1,Q2,Q3等。 如何在php和mysql中实现它。问题表和主题表如下。
问题样本数据如下:
<?php
require_once 'config.php';
//$con = mysqli_connect("localhost","root","","database_name");
$query1 = "SELECT q.q_id,q.setq_no, q.qtext_eng, s.sub_id, s.sub_name
FROM question q
INNER JOIN subject s ON s.sub_id = q.sub_id
INNER JOIN questionset qs ON qs.qset_id = q.qset_id
WHERE qs.qset_id =2 ORDER BY s.sub_id";
?>
<table class="table table-bordered">
<thead>
<tr>
<th>Q.No</th>
<th>Q Set number</th>
<th>Q text eng</th>
</tr>
<?php
$result1 = mysqli_query($link,$query1);
while($row1 = mysqli_fetch_array($result1))
{
$subID = $row1['sub_id'];
$subName = $row1['sub_name'];
?>
<h2><?php echo "$subName" ?></h2>
<?php
error_reporting(0);
$sno++;
$qSet = $row1['setq_no'];
$qEng = $row1['qtext_eng'];
?>
<tr>
<td><?php echo $sno; ?></td>
<td><?php echo $qSet; ?></td>
<td><?php echo $qEng; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
答案 0 :(得分:2)
我在这里列出了question
表中的一些列,您可以添加其余的相同方式
<?php
$con = mysqli_connect("localhost","root","","database_name");
$query1 = "SELECT q.q_id,q.setq_no, q.qtext_eng, s.sub_id, s.sub_name
FROM question q
INNER JOIN subject s ON s.sub_id = q.sub_id
INNER JOIN questionset qs ON qs.qset_id = q.qset_id
WHERE qs.qset_id =2 ORDER BY s.sub_id";
$presubID = 0;
<table class="table table-bordered">
while($row1 = mysqli_fetch_array($result1))
{
$subID = $row1['sub_id'];
if($subID != $presubID){
$subName = $row1['sub_name'];
<h2><?php echo "$subName" ?></h2>
$sno=0;
<thead>
<tr>
<th>Q.No</th>
<th>Q Set</th>
<th>Q text eng</th>
</tr>
</thead>
}
$presubID = $subID;
$sno++;
$qSet = $row1['setq_no'];
$qEng = $row1['qtext_eng'];
<tr>
<td><?php echo $sno; ?></td>
<td><?php echo $qSet; ?></td>
<td><?php echo $qEng; ?></td>
</tr>
<?php
}
?>
</table>