Home > Common Problem > php development online exam training system

php development online exam training system

angryTom
Release: 2023-05-18 13:51:01
forward
2605 people have browsed it

php development online exam training system

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:

php development online exam training system

Question bank home page display

php development online exam training system

Online test of test questions

php development online exam training system

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 [   
// 题库标题  
&#39;title&#39; => &#39;PHP基础语法考试题(二)&#39;,   
// 答题时限(秒数) 
&#39;timeout&#39; => 1200,   
// 题库   
&#39;data&#39; => [      
// 判断题 
&#39;binary&#39; => [       
          &#39;name&#39; => &#39;判断题&#39;, 
        // 题型名称
&#39;score&#39; => 24,     
        // 题型分数 
&#39;data&#39; => [          
          1 => [       
          &#39;question&#39; => &#39;在".php"后缀的文件中,所有的PHP代码都只能写在"<?php ?>"标记内。&#39;,               &#39;answer&#39; => &#39;no&#39;            ], 
        2=> [           
            &#39;question&#39; => &#39;标量类型包括布尔型、整型、字符串型和数组型。&#39;,               &#39;answer&#39; => &#39;no&#39;            ], 
        3=> [    
                  &#39;question&#39; => &#39;var_dump是PHP中用于打印变量或表达式的类型与值等相关信息的函数。&#39;,              
         &#39;answer&#39; => &#39;yes&#39;        
            ]        
         ]    
          ],      
        // 单选题  
&#39;single&#39; => [     
            &#39;name&#39; => &#39;单选题&#39;,         &#39;score&#39; => 40
        ,         &#39;data&#39; => [ 
                   1 => [               
        &#39;question&#39; => &#39;下列选项中,不区分大小写的标识符是( )。&#39;,               &#39;option&#39; => [                  &#39;函数名&#39;,&#39;常量名&#39;,&#39;变量名&#39;,&#39;属性名&#39;               ],               &#39;answer&#39; => &#39;A&#39;            ], 2=> [
Copy after login

2. So how to obtain the test question data? Just import the file

$data=require "./data/1.php";
Copy after login

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(&#39;./data/*.php&#39;));
Copy after login

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"}
Copy after login

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;};
Copy after login

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(&#39; &#39;,&#39; &#39;,$str);}
Copy after login

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[&#39;data&#39;]);        $score[$k]=round($v[&#39;score&#39;]/$count[$k]);    }    return [$count,$score];         //使用list()接收返回值:list($count,$score);顺序依次对应}
Copy after login

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 &#39;./view/index.html&#39;;
Copy after login

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): ?>
         时间:分钟 总分:分开始考试
Copy after login

Directly output a variable:

Loop to output a set of variables: $v) : ?>~~~~~~~

9. Pay attention to the use of ternary operator. :?

当做一些简单的判断输出时,使用三元运算符: ?可以大大的提升效率,简化代码。

<!-- 答题情况 --><td><?=$v ? &#39;对&#39; : &#39;错&#39;?></td><!-- 得分 --><td><?=$v ? $score[$type] : 0?></td>
Copy after login

上述代码中,$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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template