How to implement the import and export function of test papers in online answering requires specific code examples
With the development of technology, online answering systems are increasingly popular among students and Teachers' favor plays an important role in teaching. The online question answering system can not only improve students' learning enthusiasm, but also enable teachers to make efficient corrections. However, a good online answering system should have the function of importing and exporting test papers to improve the flexibility and convenience of the system. This article will introduce how to implement the import and export functions of test papers in the online answering system and provide specific code examples.
1. Implementation of the test paper import function
Before implementing the test paper import function, we first need to clarify the data structure of the test paper. A test paper usually includes a test paper title, a list of questions and a list of answers. The question list contains multiple questions, and each question has a question type, question content and an option list. The answers to each question are included in the answer list. In the database, three tables can be used to represent the data structure of the test paper: test paper table, question table and answer table.
Create a table named paper to store test paper information. The table structure is as follows:
CREATE TABLE paper (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL
);
Create a table named question to store question information. The table structure is as follows:
CREATE TABLE question (
id INT PRIMARY KEY AUTO_INCREMENT,
paper_id INT NOT NULL,
type ENUM('Single choice question', 'Multiple choice question', ' Fill in the blanks') NOT NULL,
content TEXT NOT NULL,
options TEXT,
FOREIGN KEY (paper_id) REFERENCES paper(id)
);
Create a table named answer to store the answer information of the question. The table structure is as follows:
CREATE TABLE answer (
id INT PRIMARY KEY AUTO_INCREMENT,
question_id INT NOT NULL,
answer TEXT NOT NULL,
FOREIGN KEY (question_id) REFERENCES question( id)
);
The above is the table structure of the database. Next we need to implement the function of importing test papers.
First, we need to create a page for importing test papers. The page should contain an upload button for selecting a file and a submit button.
<form action="import.php" method="POST" enctype="multipart/form-data"> <input type="file" name="file" accept=".json"> <button type="submit">导入试卷</button> </form>
In the page to import the test paper, we need to process the request to import the test paper and parse the uploaded file. On the server side, we can use PHP's json_decode function to parse the JSON file and use SQL statements to store the test paper data in the database.
$file = $_FILES['file']['tmp_name']; $json = file_get_contents($file); $data = json_decode($json, true); $title = $data['title']; $questions = $data['questions']; // 存储试卷信息 $sql = "INSERT INTO paper (title) VALUES ('$title')"; $result = mysqli_query($conn, $sql); $paper_id = mysqli_insert_id($conn); // 存储题目信息 foreach ($questions as $question) { $type = $question['type']; $content = $question['content']; $options = $question['options']; $sql = "INSERT INTO question (paper_id, type, content, options) VALUES ('$paper_id', '$type', '$content', '$options')"; $result = mysqli_query($conn, $sql); $question_id = mysqli_insert_id($conn); // 存储答案信息 $answer = $question['answer']; $sql = "INSERT INTO answer (question_id, answer) VALUES ('$question_id', '$answer')"; $result = mysqli_query($conn, $sql); }
2. Implementation of the test paper export function
The implementation of the test paper export function is relatively simple. You only need to take out the test paper data from the database and export it in JSON format.
First, we need to create a page for exporting test papers and add a button to trigger the export.
<button onclick="exportPaper()">导出试卷</button> <script> function exportPaper() { window.location.href = 'export.php'; } </script>
In the page to export the test paper, we need to process the request to export the test paper and retrieve the test paper data from the database. Then, the test paper data is output to the user in JSON format.
header('Content-Type: application/json'); header('Content-Disposition: attachment; filename="paper.json"'); $data = array(); // 获取试卷信息 $sql = "SELECT * FROM paper"; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_assoc($result); $data['title'] = $row['title']; // 获取题目信息 $sql = "SELECT * FROM question WHERE paper_id = " . $row['id']; $result = mysqli_query($conn, $sql); $questions = array(); while ($row = mysqli_fetch_assoc($result)) { $question = array( 'type' => $row['type'], 'content' => $row['content'], 'options' => $row['options'], 'answer' => $row['answer'] ); $questions[] = $question; } $data['questions'] = $questions; echo json_encode($data);
Through the above code examples, we can implement the import and export functions of test papers in the online answering system. Users can import test papers into the system in JSON format and answer questions in the system; they can also export test papers from the system to facilitate backup, sharing and printing of test papers.
The above is the detailed content of How to implement the import and export functions of test papers in online answering. For more information, please follow other related articles on the PHP Chinese website!