php development of online exam training system
Note: This article is to record learning and development ideas and procedures It has no commercial value. Understand the development ideas. Commercial use requires a second upgrade!
(Recommended learning: php training)
The rendering is as follows:
Question bank home page display
Online test of test questions
Score statistics
The purpose of passing this small project is to practice the basics of PHP Knowledge, realize storage and loading of test question information through array operations, introduce template files, and realize the call layout of test question data.
1. Since the small project does not use a database, the test questions are stored in the PHP file in the form of arrays. Part of the code is as follows:
Note: The code below is very chaos. . You can download the program source code at the end of the article and view it against this blog post.
<?php // 考试题库二 return [ // 题库标题 'title' => 'PHP基础语法考试题(二)', // 答题时限(秒数) 'timeout' => 1200, // 题库 'data' => [ // 判断题 'binary' => [ 'name' => '判断题', // 题型名称 'score' => 24, // 题型分数 'data' => [ 1 => [ 'question' => '在".php"后缀的文件中,所有的PHP代码都只能写在"<?php ?>"标记内。', 'answer' => 'no' ], 2=> [ 'question' => '标量类型包括布尔型、整型、字符串型和数组型。', 'answer' => 'no' ], 3=> [ 'question' => 'var_dump是PHP中用于打印变量或表达式的类型与值等相关信息的函数。', 'answer' => 'yes' ] ] ], // 单选题 'single' => [ 'name' => '单选题', 'score' => 40 , 'data' => [ 1 => [ 'question' => '下列选项中,不区分大小写的标识符是( )。', 'option' => [ '函数名','常量名','变量名','属性名' ], 'answer' => 'A' ], 2=> [
2. So how to obtain the test question data? Just import the file
$data=require "./data/1.php";
Note that the above test question file is an array returned by return. When we import the file, we directly assign the test question array data to the $data variable, and then operate $ data to display the question bank data.
3. Variable question bank file.
All question bank files are placed in the /data/ directory. Each test item corresponds to a .php file. If you want to know how much test data there is, that is, how many .php files there are, here we The glob() function is used, and the code is as follows:
//统计题库目录下的“.php”文件个数,此处要求题库文件名必须是连续的数字$count = count(glob('./data/*.php'));
The glob() function returns an array of file lists. The output result of running the glob() function dump is as follows:
array(3) { [0]=> string(12) "./data/1.php" [1]=> string(12) "./data/2.php" [2]=> string(12) "./data/3.php"}
Because The file names for storing data are all consecutive numbers, which makes it easier for us to read test question information through loops.
4. Use of anonymous functions.
An anonymous function is a function without a function name, also called a closure function. It is often used to destroy the value of function parameters. For temporarily defined functions, using anonymous functions does not require considering function naming conflicts. question. The use of anonymous functions here is purely for practice purposes. We can also encapsulate the following code into a functional function:
$func = function ($data) use(&$func){ $result = []; foreach ($data as $k=>$v){ //如果是数组,则继续递归,如果是字符串,则转义 $result[$k] = is_array($v) ? $func($v) : (is_string($v) ? toHtml($v) : $v); } return $result;};
Use anonymous functions to recursively escape special characters in test question data.
5. Escape HTML special characters.
If HTML tags appear in the test questions, they will be parsed by the browser and cannot be output as they are. Therefore, it is necessary to character escape the HTML tags that may exist in the question bank. The code is as follows:
//HTML特殊字符转义function toHtml($str){ $str=htmlspecialchars($str,ENT_QUOTES); return str_replace(' ',' ',$str);}
Here we use the function htmlspecialchars() to escape possible special characters, so that when outputting, we can output it in the format we want.
The corresponding htmlspecialchars() function cannot escape spaces and newlines. To escape spaces, you need to use the str_replace() function to directly replace them. For newlines, you can use the nl2br function.
6. return returns two value forms.
The specific code is as follows:
function getDataInfo($data){ $count = []; //保存某种题型的题目数量 $score = []; //每道题的分值 foreach ($data as $k=>$v) { $count[$k]=count($v['data']); $score[$k]=round($v['score']/$count[$k]); } return [$count,$score]; //使用list()接收返回值:list($count,$score);顺序依次对应}
The code finally uses return to return two result variables. To receive these two return values, you need to use list() to receive one Array assignment assigns elements in the array to variables in sequence.
7. Encapsulate the functions to be used into functional functions for easy calling and avoid code duplication.
Here, all the functions used are encapsulated into functions as much as possible. When used, just introduce the function file to facilitate management and improve code utilization.
8. Use templates
After using PHP to process the data, use
//引入HTML模板require './view/index.html';
to introduce the HTML template in the PHP file. In the template, We can also insert PHP tags to output the desired data and display the data dynamically.
在线考试系统请选择题库$v): ?> 时间:分钟 总分:分开始考试
Directly output a variable: =$value?>
Loop to output a set of variables: $v) : ?>~~~~~~~
9. Pay attention to the use of ternary operator. :?
当做一些简单的判断输出时,使用三元运算符: ?可以大大的提升效率,简化代码。
<!-- 答题情况 --><td><?=$v ? '对' : '错'?></td><!-- 得分 --><td><?=$v ? $score[$type] : 0?></td>
上述代码中,$v是一个bool值,通过三元运算符我们可以省略一大堆if判断,使代码看上去更加整洁。
10、页面中的计时倒计时、关闭页面提示、未做考题提交提示、考试时间结束自动交卷功能是通过jQuery、javascript来实现的,这里不作说明。
程序源码下载地址:
http://www.seoalphas.com/data/upload/ueditor/20170629/595494a1f01ea.rar
注:php中文网提供大量免费、原创、高清的php视频教程,并且会定期举行php公益培训!
The above is the detailed content of php development online exam training system. For more information, please follow other related articles on the PHP Chinese website!