PHP implements the question answer collection and sharing functions in the knowledge Q&A website.

王林
Release: 2023-07-01 19:02:01
Original
1225 people have browsed it

PHP realizes the collection and sharing functions of question answers in the knowledge question and answer website

1. Background
With the rapid development of the Internet, the knowledge question and answer website has gradually become an important platform for people to obtain knowledge and exchange experiences. On such a site, users can post their own questions and other users can answer those questions. In order to provide a better user experience, we will introduce in this article how to use PHP to implement the question answer collection and sharing functions in the knowledge Q&A website.

2. Implementation of question and answer collection function
In the knowledge question and answer website, users can collect answers to questions and answers they are interested in for later viewing. The following is a code example to implement the question answer collection function:

  1. First, we need to create a database table to store the user's collection information. You can create a table named "bookmarks", containing the following fields:

    CREATE TABLE bookmarks (
     id INT AUTO_INCREMENT PRIMARY KEY,
     user_id INT,
     answer_id INT,
     created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    Copy after login
  2. After the user logs in, obtain the user's ID through the PHP session mechanism, and then combine the user ID and question answer Insert the ID into the "bookmarks" table:

    // 获取当前用户的 ID
    $userId = $_SESSION['user_id'];
    
    // 获取问题答案的 ID
    $answerId = $_GET['answer_id'];
    
    // 插入数据到数据库表中
    $query = "INSERT INTO bookmarks (user_id, answer_id) VALUES ($userId, $answerId)";
    $result = mysqli_query($connection, $query);
    Copy after login
  3. When the user needs to view the answers to his favorite questions, we can query the database table based on the user's ID and display the relevant answers to the questions. :

    // 获取当前用户的 ID
    $userId = $_SESSION['user_id'];
    
    // 查询数据库表,获取用户收藏的问题答案
    $query = "SELECT * FROM bookmarks WHERE user_id = $userId";
    $result = mysqli_query($connection, $query);
    
    // 循环显示问题答案
    while ($row = mysqli_fetch_assoc($result)) {
     $answerId = $row['answer_id'];
    
     // 根据答案的 ID 查询相关的问题答案信息,并显示在页面上
     $query = "SELECT * FROM answers WHERE id = $answerId";
     $answerResult = mysqli_query($connection, $query);
    
     // 显示问题答案信息
     $answerRow = mysqli_fetch_assoc($answerResult);
     echo $answerRow['content'];
    }
    Copy after login

3. Implementation of question and answer sharing function
In addition to collecting questions and answers, users can also share answers to questions that they think are valuable to other users. The following is a code example to implement the question and answer sharing function:

  1. On the question and answer page, we can add a "Share" button, and when the button is clicked, a JavaScript function is triggered and passed through AJAX Request to send the ID of the question answer to the server side:

    <button onclick="shareAnswer(<?php echo $answerId ?>)">分享</button>
    
    <script>
    function shareAnswer(answerId) {
     // 发送 AJAX 请求
     var xhr = new XMLHttpRequest();
     xhr.open('POST', 'share.php');
     xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     xhr.onload = function() {
         if (xhr.status === 200) {
             alert('问题答案分享成功!');
         } else {
             alert('问题答案分享失败!');
         }
     };
     xhr.send('answer_id=' + answerId);
    }
    </script>
    Copy after login
  2. On the server side, obtain the ID of the question answer through PHP and insert it into a database table named "shares" Medium:

    // 获取问题答案的 ID
    $answerId = $_POST['answer_id'];
    
    // 插入数据到数据库表中
    $query = "INSERT INTO shares (answer_id) VALUES ($answerId)";
    $result = mysqli_query($connection, $query);
    if ($result) {
     echo 'success';
    } else {
     echo 'failure';
    }
    Copy after login
  3. When other users view the answer to the question, we can display the number of times the answer to the question has been shared:

    // 获取问题答案的 ID
    $answerId = $_GET['answer_id'];
    
    // 查询数据库表,获取问题答案的分享次数
    $query = "SELECT COUNT(*) AS shares_count FROM shares WHERE answer_id = $answerId";
    $result = mysqli_query($connection, $query);
    $row = mysqli_fetch_assoc($result);
    
    // 显示问题答案的分享次数
    echo $row['shares_count'] . "次分享";
    Copy after login

4. Summary
Through the above code examples, we can implement the question and answer collection and sharing functions in the knowledge Q&A website. The collection function allows users to save answers to questions they are interested in for later viewing; the sharing function allows users to pass valuable questions and answers to other users. The implementation of these functions is very important to improve the user experience of the knowledge question and answer website, and also facilitates communication and sharing between users. I hope this article can be helpful to developers!

The above is the detailed content of PHP implements the question answer collection and sharing functions in the knowledge Q&A website.. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!