


Use PHP to develop question answer acceptance and recommendation functions in a knowledge question and answer website.
Use PHP to develop question answer acceptance and recommendation functions in knowledge Q&A websites
In knowledge Q&A websites, users ask questions and other users provide answers. It often happens that one or more of the multiple answers is recognized as the best answer by the questioner or other users. Therefore, in order to better display and manage the answers to questions, in this article we will introduce how to develop the adoption and recommendation functions of question answers using PHP.
- Database design
First, we need to design a database to store information about questions and answers. Here we create three tables. The
-
questions
table stores question information, such as question ID, title, content, questioner ID, question time, etc. -
answers
The table stores answer information, such as answer ID, question ID, answer content, answerer ID, answer time, etc. -
accepted_answers
The table is used to store accepted answer information, which includes question ID and accepted answer ID.
The following is an example of the database structure:
CREATE TABLE questions ( id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(255), content TEXT, owner_id INT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE answers ( id INT PRIMARY KEY AUTO_INCREMENT, question_id INT, content TEXT, owner_id INT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE accepted_answers ( question_id INT PRIMARY KEY, answer_id INT );
- Question page development
On the question details page, we need to display the title of the question , content and related answers. For the current question, we also need to show the accepted answer (if any).
First, we need to get the problem information and display it on the page. We can use the following code example:
<?php // 获取问题 ID $questionId = $_GET['id']; // 获取问题信息 $questionQuery = "SELECT * FROM questions WHERE id = :id"; $questionStmt = $pdo->prepare($questionQuery); $questionStmt->bindParam(':id', $questionId, PDO::PARAM_INT); $questionStmt->execute(); $question = $questionStmt->fetch(PDO::FETCH_ASSOC); // 显示问题标题和内容 echo "<h1>" . $question['title'] . "</h1>"; echo "<p>" . $question['content'] . "</p>"; // 获取问题的回答信息 $answersQuery = "SELECT * FROM answers WHERE question_id = :question_id"; $answersStmt = $pdo->prepare($answersQuery); $answersStmt->bindParam(':question_id', $questionId, PDO::PARAM_INT); $answersStmt->execute(); $answers = $answersStmt->fetchAll(PDO::FETCH_ASSOC); // 显示回答列表 foreach ($answers as $answer) { echo "<div>"; echo "<p>" . $answer['content'] . "</p>"; // 检查答案是否被采纳 if ($answer['id'] === $question['accepted_answer_id']) { echo "<span>已采纳</span>"; } else { // 显示采纳按钮 echo "<a href='accept-answer.php?question=" . $questionId . "&answer=" . $answer['id'] . "'>采纳该答案</a>"; } echo "</div>"; } ?>
In the above code, we first obtain the problem information and display it on the page. Then, get the answers related to the question and display them one by one. For each answer, we check whether it was accepted. If it is accepted, the "Adopted" mark will be displayed; otherwise, a label will be displayed. Clicking the label will call accept-answer.php
to accept the answer.
- Processing of accepted answers
In the accept-answer.php
file, we need to process the answer acceptance request and update accepted_answers
table.
Here is a code example to handle an accepted answer request:
<?php // 获取问题 ID 和答案 ID $questionId = $_GET['question']; $answerId = $_GET['answer']; // 将答案 ID 更新为被采纳 $acceptAnswerQuery = "UPDATE accepted_answers SET answer_id = :answer_id WHERE question_id = :question_id"; $acceptAnswerStmt = $pdo->prepare($acceptAnswerQuery); $acceptAnswerStmt->bindParam(':answer_id', $answerId, PDO::PARAM_INT); $acceptAnswerStmt->bindParam(':question_id', $questionId, PDO::PARAM_INT); $acceptAnswerStmt->execute(); // 返回问题详情页 header("Location: question.php?id=" . $questionId); ?>
In the above code, we first get the ID of the question and answer. Then, update the answer ID to the accepted answer by executing a SQL statement. Finally, we redirect to the issue details page and refresh the page to show the latest adoption status.
Through the above steps, we have successfully implemented the function of adopting and recommending answers to questions in the knowledge question and answer website. Users can adopt answers they approve of in the question details page and improve the quality of questions and answers through this feature. This helps users better capture and share knowledge.
The above is the detailed content of Use PHP to develop question answer acceptance and recommendation functions in a knowledge question and answer website.. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
