在之前的教程中,我们已经用PHP和MySQL开发了客户关系管理(CRM)系统。在本教程中,我们将用PHP和MySQL开发在线考试系统。
在线考试系统是一个可以用于教育目的的网络应用程序。考试系统允许创建带有问题和选项的完整考试。用户可以在规定时间内完成考试。该系统还允许其用户看到他们的考试结果。
在线考试系统总是有需求的,因为大多数的考试或测试都是在网上进行的。因此,如果你是一个开发者,正在寻找开发在线考试系统的解决方案,那么你就来对地方了。在本教程中,你将学习如何用PHP和MySQL开发自己的在线考试系统。
另外,请阅读。
- 用PHP和MySQL开发用户管理系统
- 用jQuery、PHP和MySQL建立服务台系统
- 用PHP和MySQL构建在线投票系统
- 用PHP和MySQL构建学校管理系统
- 用PHP和MySQL构建项目管理系统
- 用PHP和MySQL搭建医院管理系统
在这里,我们将开发一个在线考试系统并涵盖以下内容。
管理员可以做以下工作。
- 添加/编辑考试的问题和选项。
- 管理用户。
用户可以做以下事情。
- 报名参加考试。
- 查看自己的考试。
- 完成考试。
- 查看考试结果。
因此,让我们开始用PHP和MySQL开发CRM系统。主要的文件有。
- user.php
- exam.php
- enroll.php
- questions.php
- view.php
- process_exam.php
- User.php:一个包含用户方法的类。
- Exam.php:一个包含与考试有关的方法的类。
- Questions.php:一个包含与问题有关的方法的类。
步骤1:创建MySQL数据库表
首先,我们将为我们的在线考试系统创建MySQL数据库表。主要的表有以下几个。
我们将创建online_exam_user 表来存储用户信息。
CREATE TABLE `online_exam_user` (
`id` int(11) UNSIGNED NOT NULL,
`first_name` varchar(255) DEFAULT NULL,
`last_name` varchar(255) DEFAULT NULL,
`gender` enum('Male','Female') NOT NULL,
`email` varchar(255) DEFAULT NULL,
`password` varchar(64) NOT NULL,
`mobile` varchar(12) NOT NULL,
`address` text NOT NULL,
`created` datetime NOT NULL DEFAULT current_timestamp(),
`role` enum('user','admin') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我们将创建online_exam_exams 表来存储考试信息。
CREATE TABLE `online_exam_exams` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`exam_title` varchar(250) NOT NULL,
`exam_datetime` datetime NOT NULL,
`duration` varchar(30) NOT NULL,
`total_question` int(5) NOT NULL,
`marks_per_right_answer` varchar(30) NOT NULL,
`marks_per_wrong_answer` varchar(30) NOT NULL,
`created_on` datetime NOT NULL,
`status` enum('Pending','Created','Started','Completed') NOT NULL,
`exam_code` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我们将创建online_exam_question 表来存储考试问题信息。
CREATE TABLE `online_exam_question` (
`id` int(11) NOT NULL,
`exam_id` int(11) NOT NULL,
`question` text NOT NULL,
`answer` enum('1','2','3','4') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我们将创建online_exam_option 表来存储问题选项信息。
CREATE TABLE `online_exam_option` (
`id` int(11) NOT NULL,
`question_id` int(11) NOT NULL,
`option` int(2) NOT NULL,
`title` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我们将创建online_exam_question_answer 表来存储考试问题的答案信息。
CREATE TABLE `online_exam_question_answer` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`exam_id` int(11) NOT NULL,
`question_id` int(11) NOT NULL,
`user_answer_option` enum('0','1','2','3','4') NOT NULL,
`marks` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
第二步:管理考试部分
我们将在exam.php 文件中实现管理考试的功能。我们将创建HTML来添加、编辑和删除考试。
<div>
<div class="panel-heading">
<div class="row">
<div class="col-md-10">
<h3 class="panel-title"></h3>
</div>
<div class="col-md-2" align="right">
<button type="button" id="addExam" class="btn btn-info" title="Add Exam"><span class="glyphicon glyphicon-plus"></span></button>
</div>
</div>
</div>
<table id="examListing" class="table table-bordered table-striped">
<thead>
<tr>
<th>Id</th>
<th>Exam Title</th>
<th>Date Time</th>
<th>Duration</th>
<th>Total Question</th>
<th>R/Q Mark</th>
<th>W/Q Mark</th>
<th>Status</th>
<th>Questions</th>
<th>Enroll</th>
<th>Result</th>
<th></th>
<th></th>
</tr>
</thead>
</table>
</div>
<div id="examModal" class="modal fade">
<div class="modal-dialog">
<form method="post" id="examForm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><i class="fa fa-plus"></i> Edit Exam</h4>
</div>
<div class="modal-body">
<div class="form-group"
<label for="project" class="control-label">Examm Title</label>
<input type="text" class="form-control" id="exam_title" name="exam_title" placeholder="Exam title" required>
</div>
<div class="form-group"
<label for="project" class="control-label">Due Date</label>
<input type="datetime-local" class="form-control" id="exam_datetime" name="exam_datetime" placeholder="Exam date" >
</div>
<div class="form-group"
<label for="project" class="control-label">Duration</label>
<select name="exam_duration" id="exam_duration" class="form-control">
<option value="">Select</option>
<option value="5">5 Minute</option>
<option value="30">30 Minute</option>
<option value="60">1 Hour</option>
<option value="120">2 Hour</option>
<option value="180">3 Hour</option>
</select>
</div>
<div class="form-group"
<label for="project" class="control-label">Total Question</label>
<select name="total_question" id="total_question" class="form-control">
<option value="">Select</option>
<option value="5">5 Question</option>
<option value="10">10 Question</option>
<option value="25">25 Question</option>
<option value="50">50 Question</option>
<option value="100">100 Question</option>
<option value="200">200 Question</option>
<option value="300">300 Question</option>
</select>
</div>
<div class="form-group"
<label for="project" class="control-label">Marks For Right Answer</label>
<select name="marks_right_answer" id="marks_right_answer" class="form-control">
<option value="">Select</option>
<option value="1">+1 Mark</option>
<option value="2">+2 Mark</option>
<option value="3">+3 Mark</option>
<option value="4">+4 Mark</option>
<option value="5">+5 Mark</option>
</select>
</div>
<div class="form-group"
<label for="project" class="control-label">Marks For Wrong Answer</label>
<select name="marks_wrong_answer" id="marks_wrong_answer" class="form-control">
<option value="">Select</option>
<option value="1">-1 Mark</option>
<option value="1.25">-1.25 Mark</option>
<option value="1.50">-1.50 Mark</option>
<option value="2">-2 Mark</option>
</select>
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="id" id="id" />
<input type="hidden" name="action" id="action" value="" />
<input type="submit" name="save" id="save" class="btn btn-info" value="Save" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>
我们将在exam.js 中实现功能,打开添加新考试表格的模式,以添加新考试。
$('#addExam').click(function(){
$('#examModal').modal({
backdrop: 'static',
keyboard: false
});
$('#examForm')[0].reset();
$("#examModal").on("shown.bs.modal", function () {
$('.modal-title').html("<i class='fa fa-plus'></i> Add Exam");
$('#action').val('addExam');
$('#save').val('Save');
});
});
在exam_action.php 文件中,我们将检查添加考试的动作,并从Exam.php 类中调用insert() 方法来添加考试。
if(!empty($_POST['action']) && $_POST['action'] == 'addExam') {
$exam->exam_title = $_POST["exam_title"];
$exam->exam_datetime = $_POST["exam_datetime"];
$exam->duration = $_POST["exam_duration"];
$exam->total_question = $_POST["total_question"];
$exam->marks_per_right_answer = $_POST["marks_right_answer"];
$exam->marks_per_wrong_answer = $_POST["marks_wrong_answer"];
$exam->insert();
}
我们将在类Exam.php 中实现方法insert() ,将新的考试添加到数据库。
public function insert(){
if($this->exam_title) {
$stmt = $this->conn->prepare("
INSERT INTO ".$this->examTable."(`user_id`, `exam_title`, `exam_datetime`, `duration`, `total_question`, `marks_per_right_answer`,`marks_per_wrong_answer`)
VALUES(?,?,?,?,?,?,?)");
$this->exam_title = htmlspecialchars(strip_tags($this->exam_title));
$this->exam_datetime = htmlspecialchars(strip_tags($this->exam_datetime));
$this->duration = htmlspecialchars(strip_tags($this->duration));
$this->total_question = htmlspecialchars(strip_tags($this->total_question));
$this->marks_per_right_answer = htmlspecialchars(strip_tags($this->marks_per_right_answer));
$this->marks_per_wrong_answer = htmlspecialchars(strip_tags($this->marks_per_wrong_answer));
$stmt->bind_param("isssiss", $_SESSION["userid"], $this->exam_title, $this->exam_datetime, $this->duration, $this->total_question, $this->marks_per_right_answer, $this->marks_per_wrong_answer);
if($stmt->execute()){
return true;
}
}
}
我们将在类Exam.php 中实现方法update() ,以更新考试。
public function update(){
if($this->id && $_SESSION["userid"]) {
$stmt = $this->conn->prepare("
UPDATE ".$this->examTable."
SET exam_title= ?, exam_datetime = ?, duration = ?, total_question = ?, marks_per_right_answer = ?, marks_per_wrong_answer = ?
WHERE id = ? AND user_id = ?");
$this->id = htmlspecialchars(strip_tags($this->id));
$this->exam_title = htmlspecialchars(strip_tags($this->exam_title));
$this->exam_datetime = htmlspecialchars(strip_tags($this->exam_datetime));
$this->duration = htmlspecialchars(strip_tags($this->duration));
$this->total_question = htmlspecialchars(strip_tags($this->total_question));
$this->marks_per_right_answer = htmlspecialchars(strip_tags($this->marks_per_right_answer));
$this->marks_per_wrong_answer = htmlspecialchars(strip_tags($this->marks_per_wrong_answer));
$stmt->bind_param("sssissii", $this->exam_title, $this->exam_datetime, $this->duration, $this->total_question, $this->marks_per_right_answer, $this->marks_per_wrong_answer, $this->id, $_SESSION["userid"]);
if($stmt->execute()){
return true;
}
}
}
我们将在类Exam.php 中实现方法delete() ,以删除考试。
public function delete(){
if($this->id && $_SESSION["userid"]) {
$stmt = $this->conn->prepare("
DELETE FROM ".$this->examTable."
WHERE id = ? AND user_id = ?");
$this->id = htmlspecialchars(strip_tags($this->id));
$stmt->bind_param("ii", $this->id, $_SESSION["userid"]);
if($stmt->execute()){
return true;
}
}
}
我们还将在Exam.php 类中实现方法listExam() ,以列出所有的考试。
public function listExam(){
$sqlQuery = "
SELECT id, exam_title, exam_datetime, duration, total_question, marks_per_right_answer, marks_per_wrong_answer, status
FROM ".$this->examTable."
WHERE user_id = '".$_SESSION["userid"]."' ";
if(!empty($_POST["search"]["value"])){
$sqlQuery .= ' AND (name LIKE "%'.$_POST["search"]["value"].'%" ';
$sqlQuery .= ' OR email LIKE "%'.$_POST["search"]["value"].'%" ';
$sqlQuery .= ' OR gender LIKE "%'.$_POST["search"]["value"].'%" ';
$sqlQuery .= ' OR mobile LIKE "%'.$_POST["search"]["value"].'%" ';
$sqlQuery .= ' OR address LIKE "%'.$_POST["search"]["value"].'%" ';
$sqlQuery .= ' OR age LIKE "%'.$_POST["search"]["value"].'%") ';
}
if(!empty($_POST["order"])){
$sqlQuery .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
} else {
$sqlQuery .= 'ORDER BY id ASC ';
}
if($_POST["length"] != -1){
$sqlQuery .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$stmt = $this->conn->prepare($sqlQuery);
$stmt->execute();
$result = $stmt->get_result();
$stmtTotal = $this->conn->prepare("SELECT * FROM ".$this->examTable." WHERE id = '".$_SESSION["userid"]."'");
$stmtTotal->execute();
$allResult = $stmtTotal->get_result();
$allRecords = $allResult->num_rows;
$displayRecords = $result->num_rows;
$records = array();
while ($exam = $result->fetch_assoc()) {
$rows = array();
$rows[] = $exam['id'];
$rows[] = $exam['exam_title'];
$rows[] = $exam['exam_datetime'];
$rows[] = $exam['duration'];
$rows[] = $exam['total_question'];
$rows[] = $exam['marks_per_right_answer'];
$rows[] = $exam['marks_per_wrong_answer'];
$rows[] = $exam['status'];
$rows[] = '<a type="button" name="view" href="questions.php?exam_id='.$exam["id"].'" class="btn btn-info btn-xs add_question"><span class="glyphicon" title="Add Question">Questions</span></a>';
$rows[] = '<a type="button" name="update" href="enroll.php?exam_id='.$exam["id"].'" class="btn btn-primary btn-xs enroll"><span class="glyphicon glyphicon-user" title="Enroll">Enroll</span></a>';
$rows[] = '<button type="button" name="delete" id="'.$exam["id"].'" class="btn btn-success btn-xs result" ><span class="glyphicon" title="Result">Result</span></button>';
$rows[] = '<button type="button" name="update" id="'.$exam["id"].'" class="btn btn-warning btn-xs update"><span class="glyphicon glyphicon-edit" title="Edit"></span></button>';
$rows[] = '<button type="button" name="delete" id="'.$exam["id"].'" class="btn btn-danger btn-xs delete" ><span class="glyphicon glyphicon-remove" title="Delete"></span></button>';
$records[] = $rows;
}
$output = array(
"draw" => intval($_POST["draw"]),
"iTotalRecords" => $displayRecords,
"iTotalDisplayRecords" => $allRecords,
"data" => $records
);
echo json_encode($output);
}
第三步:管理考试问题
我们将创建HTML来实现questions.php 中的功能,以添加、编辑和删除问题。我们还将处理添加问题选项的功能。
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="exam.php">Exam</a></li>
<li class="breadcrumb-item active" aria-current="page">Questions</li>
</ol>
</nav>
<br>
<div>
<div class="panel-heading">
<div class="row">
<div class="col-md-10">
<h3 class="panel-title"></h3>
</div>
<div class="col-md-2" align="right">
<button type="button" id="addQuestions" class="btn btn-info" title="Add Questions"><span class="glyphicon glyphicon-plus"></span></button>
</div>
</div>
</div>
<table id="questionsListing" data-exam-id="<?php echo $_GET['exam_id']; ?>" class="table table-bordered table-striped">
<thead>
<tr>
<th>Id</th>
<th>Question</th>
<th>Right Option</th>
<th></th>
<th></th>
</tr>
</thead>
</table>
</div>
<div id="questionsModal" class="modal fade">
<div class="modal-dialog">
<form method="post" id="questionsForm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><i class="fa fa-plus"></i> Edit questions</h4>
</div>
<div class="modal-body">
<div class="form-group">
<div class="row">
<label class="col-md-4 text-right">Question Title <span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="text" name="question_title" id="question_title" autocomplete="off" class="form-control" />
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-md-4 text-right">Option 1 <span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="text" name="option_title_1" id="option_title_1" autocomplete="off" class="form-control" />
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-md-4 text-right">Option 2 <span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="text" name="option_title_2" id="option_title_2" autocomplete="off" class="form-control" />
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-md-4 text-right">Option 3 <span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="text" name="option_title_3" id="option_title_3" autocomplete="off" class="form-control" />
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-md-4 text-right">Option 4 <span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="text" name="option_title_4" id="option_title_4" autocomplete="off" class="form-control" />
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-md-4 text-right">Answer <span class="text-danger">*</span></label>
<div class="col-md-8">
<select name="answer_option" id="answer_option" class="form-control">
<option value="">Select</option>
<option value="1">1 Option</option>
<option value="2">2 Option</option>
<option value="3">3 Option</option>
<option value="4">4 Option</option>
</select>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="id" id="id" />
<input type="hidden" name="exam_id" id="exam_id" />
<input type="hidden" name="action" id="action" value="" />
<input type="submit" name="save" id="save" class="btn btn-info" value="Save" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>
在questions.js ,我们将实现打开问题添加模式的功能。
$('#addQuestions').click(function(){
$('#questionsModal').modal({
backdrop: 'static',
keyboard: false
});
$("#questionsModal").on("shown.bs.modal", function () {
$('#questionsForm')[0].reset();
$('.modal-title').html("<i class='fa fa-plus'></i> Add Questions");
$('#exam_id').val($('#questionsListing').attr('data-exam-id'));
$('#action').val('addQuestions');
$('#save').val('Save');
});
});
我们将检查动作addQuestions ,并实现从类Questions.php 中调用方法insert() 的功能,以添加带有选项的问题。
if(!empty($_POST['action']) && $_POST['action'] == 'addQuestions') {
$questions->exam_id = $_POST["exam_id"];
$questions->question_title = $_POST["question_title"];
$options = array();
for($count = 1; $count <= 4; $count++) {
$options[$count] = $_POST['option_title_' . $count];
}
$questions->option = $options;
$questions->answer_option = $_POST["answer_option"];
$questions->insert();
}
我们将在类Questions.php 中实现方法insert() ,以添加带选项的问题。
public function insert(){
if($this->exam_id && $this->question_title && $this->answer_option) {
$stmt = $this->conn->prepare("
INSERT INTO ".$this->questionTable."(`exam_id`, `question`, `answer`)
VALUES(?,?,?)");
$this->question_title = htmlspecialchars(strip_tags($this->question_title));
$this->answer_option = htmlspecialchars(strip_tags($this->answer_option));
$stmt->bind_param("iss", $this->exam_id, $this->question_title, $this->answer_option);
if($stmt->execute()){
$lastInsertQuestionId = $this->conn->insert_id;
$stmt1 = $this->conn->prepare("
INSERT INTO ".$this->optionTable."(`question_id`, `option`, `title`)
VALUES(?,?,?)");
foreach($this->option as $key => $value) {
$stmt1->bind_param("iis", $lastInsertQuestionId, $key, $value);
$stmt1->execute();
}
return true;
}
}
}
第四步:实现考试报名
我们将创建HTML来显示考试下拉列表,允许用户报名参加考试以完成考试。
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<select name="exam_list" id="exam_list" class="form-control input-lg">
<option value="">Select Exam</option>
<?php echo $exam->getExamList(); ?>
</select>
<br />
<span id="exam_details"></span>
</div>
<div class="col-md-3"></div>
</div>
我们将实现考试报名的功能。
$(document).on('click', '#enrollExam', function(){
var exam_id = $('#enrollExam').attr('data-exam_id');
$.ajax({
url:"user_exam_action.php",
method:"POST",
data:{action:'enrollExam', exam_id:exam_id},
beforeSend:function() {
$('#enrollExam').attr('disabled', 'disabled');
$('#enrollExam').text('please wait');
},
success:function() {
$('#enrollExam').attr('disabled', false);
$('#enrollExam').removeClass('btn-warning');
$('#enrollExam').addClass('btn-success');
$('#enrollExam').text('Enroll success');
}
});
});
在user_exam_action.php ,我们将检查动作enrollExam ,并从类Exam.php 中调用方法enrollToExam() ,以报名参加考试。
if(!empty($_POST['action']) && $_POST['action'] == 'enrollExam') {
$exam->exam_id = $_POST['exam_id'];
$exam->enrollToExam();
}
我们将在类Exam.php 中实现方法enrollToExam() ,为用户报名参加考试以完成考试。
public function enrollToExam(){
if($this->exam_id) {
$stmt = $this->conn->prepare("
INSERT INTO ".$this->enrollTable."(`user_id`, `exam_id`)
VALUES(?,?)");
$this->exam_id = htmlspecialchars(strip_tags($this->exam_id));
$stmt->bind_param("ii", $_SESSION["userid"], $this->exam_id);
if($stmt->execute()){
$stmtAnswer = $this->conn->prepare("
INSERT INTO ".$this->questionAnswerTable."(`user_id`, `exam_id`, `question_id`)
VALUES(?,?,?)");
$sqlQuery = "
SELECT id
FROM ".$this->questionTable."
WHERE exam_id = ?";
$stmtQuestion = $this->conn->prepare($sqlQuery);
$stmtQuestion->bind_param("i", $this->exam_id);
$stmtQuestion->execute();
$result = $stmtQuestion->get_result();
while ($question = $result->fetch_assoc()) {
$stmtAnswer->bind_param("iii", $_SESSION["userid"], $this->exam_id, $question['id']);
$stmtAnswer->execute();
}
return true;
}
}
}
第五步:实现查看考试
我们将创建一个HTML来显示考试问题和完成考试的选项。我们还将用计时器显示问题的时间,以显示考试的剩余时间,在该时间内完成考试。
<?php
if($examDetails['status'] == 'Started') {
?>
<div class="col-md-8">
<div class="card">
<div class="card-body">
<div id="single_question_area"></div>
</div>
</div>
<br />
<div id="question_navigation_area"></div>
</div>
<div class="col-md-4">
<br />
<div align="center">
<div id="examTimer" data-timer="<?php echo $remainingMinutes; ?>" style="max-width:400px; width: 100%; height: 200px;"></div>
</div>
<br />
<div id="user_details_area"></div>
</div>
<?php } ?>
我们将在user_exam_action.php 中实现函数loadQuestion() ,以加载考试题目。
function loadQuestion(question_id = '') {
var examId = $('#processExamId').attr('data-exam_id');
$.ajax({
url:"user_exam_action.php",
method:"POST",
data:{exam_id:examId, question_id:question_id, action:'loadQuestion'},
success:function(data){
$('#single_question_area').html(data);
}
})
}
我们将检查动作loadQuestion ,并从类Exam.php 中调用方法loadQuestions() 来加载考试题。
if(!empty($_POST['action']) && $_POST['action'] == 'loadQuestion') {
$exam->exam_id = $_POST['exam_id'];
$exam->question_id = $_POST['question_id'];
$exam->loadQuestions();
}
我们将在类Exam.php 中实现方法loadQuestions() ,以加载带有选项的考试题。
public function loadQuestions() {
if($this->exam_id) {
$whereSql = '';
if($this->question_id) {
$whereSql = "questions.id = '".$this->question_id."'";
} elseif($this->exam_id) {
$whereSql = "questions.exam_id = '".$this->exam_id."'";
}
$sqlQuery = "
SELECT questions.id as question_id, questions.question, questions.answer
FROM ".$this->questionTable." AS questions
WHERE $whereSql ORDER BY questions.id ASC LIMIT 1";
$stmtQuestion = $this->conn->prepare("
SELECT * FROM ".$this->optionTable."
WHERE question_id = ?");
$sqlQueryPrev = $this->conn->prepare("
SELECT id
FROM ".$this->questionTable."
WHERE id < ? AND exam_id = ? ORDER BY id DESC LIMIT 1");
$sqlQueryNext = $this->conn->prepare("
SELECT id
FROM ".$this->questionTable."
WHERE id > ? AND exam_id = ? ORDER BY id ASC LIMIT 1");
$stmt = $this->conn->prepare($sqlQuery);
$stmt->execute();
$result = $stmt->get_result();
$output = '';
while ($questions = $result->fetch_assoc()) {
$output .= '
<h2>'.$questions["question"].'</h2>
<hr />
<div class="row">';
$stmtQuestion->bind_param("i", $questions["question_id"]);
$stmtQuestion->execute();
$resultQuestion = $stmtQuestion->get_result();
$count = 1;
while ($options = $resultQuestion->fetch_assoc()) {
$output .= '
<div class="col-md-6" style="margin-bottom:32px;">
<div class="radio">
<label><h4><input type="radio" name="option_1" class="answer_option" data-question_id="'.$questions["question_id"].'" data-id="'.$count.'"/> '.$options["title"].'</h4></label>
</div>
</div>';
$count = $count + 1;
}
$output .= '</div>';
$sqlQueryPrev->bind_param("ii", $questions["question_id"], $this->exam_id);
$sqlQueryPrev->execute();
$resultPrev = $sqlQueryPrev->get_result();
$previousId = '';
$nextId = '';
while ($preQuestion = $resultPrev->fetch_assoc()) {
$previousId = $preQuestion['id'];
}
$sqlQueryNext->bind_param("ii", $questions["question_id"], $this->exam_id);
$sqlQueryNext->execute();
$resultNext = $sqlQueryNext->get_result();
while ($nextQuestion = $resultNext->fetch_assoc()) {
$nextId = $nextQuestion['id'];
}
$previousDisable = '';
$nextDisable = '';
if($previousId == "") {
$previousDisable = 'disabled';
}
if($nextId == "") {
$nextDisable = 'disabled';
}
$output .= '
<br /><br />
<div>
<button type="button" name="previous" class="btn btn-info btn-lg previous" id="'.$previousId.'" '.$previousDisable.'>Previous</button>
<button type="button" name="next" class="btn btn-warning btn-lg next" id="'.$nextId.'" '.$nextDisable.'>Next</button>
</div>
<br /><br />';
}
echo $output;
}
}
你可能也喜欢。
- 使用PHP和MySQL的用户管理系统
- 用Ajax、PHP和MySQL添加、编辑和删除数据库
- 用jQuery、PHP和MySQL构建服务台系统
- 用PHP和MySQL构建在线投票系统
- 用PHP和MySQL构建学校管理系统
- 用CodeIgniter添加编辑删除数据表
- 使用CodeIgniter创建RESTful API
- 用PHP构建可重复使用的验证码脚本
- 使用Ajax、PHP和MySQL进行产品搜索过滤
- 用jQuery、PHP和MySQL在模版中上传和裁剪图片
- 用PHP和MySQL构建推送通知系统
- 用PHP和MySQL构建项目管理系统
- 用PHP和MySQL建立医院管理系统
- 用PHP和MySQL建立电子报系统
- 用Ajax和PHP实现骨架屏幕加载效果
- 用PHP和MySQL建立讨论区
- 用PHP和MySQL构建客户关系管理(CRM)系统
- 用PHP和MySQL建立在线考试系统
- 用PHP和MySQL建立费用管理系统
你可以从演示链接中查看实时演示,也可以从下面的下载链接中下载脚本。
演示 下载
The postOnline Exam System with PHP & MySQLfirst appeared onWD.