Developed question scoring and sorting functionality in a trivia website using PHP.

PHPz
Release: 2023-07-01 22:56:02
Original
1402 people have browsed it

Use PHP to develop question scoring and sorting functions in the knowledge Q&A website

With the development of the Internet, the knowledge Q&A website has now become one of the important channels for people to obtain knowledge and solve problems. These websites give users the freedom to ask, answer and comment, but how to score and rank questions becomes a key issue. This article will introduce how to use PHP to develop question scoring and sorting functions in a knowledge question and answer website.

  1. Database Design

First, we need to create relevant tables in the database to store questions and scoring data. Suppose we have two tables, one is the questions table, which contains fields such as question ID, title, and content, and the other is the ratings table, which contains fields such as rating ID, question ID, user ID, and rating value.

CREATE TABLE questions (
  id INT AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(255),
  content TEXT
);

CREATE TABLE ratings (
  id INT AUTO_INCREMENT PRIMARY KEY,
  question_id INT,
  user_id INT,
  rating INT
);
Copy after login
  1. Question rating function

In the knowledge question and answer website, users can rate questions to express their satisfaction with the questions. We can add a rating component at the bottom of the question details page, and users can rate the question by clicking a button.

<?php
// 获取当前问题的ID
$questionId = $_GET['id'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  // 获取用户ID和评分值
  $userId = $_SESSION['user_id'];
  $rating = $_POST['rating'];

  // 将评分插入到数据库中
  $query = "INSERT INTO ratings (question_id, user_id, rating) VALUES ('$questionId', '$userId', '$rating')";
  // 执行插入操作

  if ($result) {
    // 评分成功,刷新页面或跳转回问题详情页
  } else {
    // 评分失败,显示错误信息
  }
}
?>

<!-- 评分组件 -->
<form action="" method="POST">
  <input type="hidden" name="rating" value="1">
  <button type="submit">赞同</button>
</form>

<form action="" method="POST">
  <input type="hidden" name="rating" value="-1">
  <button type="submit">反对</button>
</form>
Copy after login
  1. Question sorting function

In order to enable users to better browse and search questions, we need to sort questions according to certain rules. Common sorting rules include sorting by time, popularity, rating value, etc. Here, we use the rating value as the sorting rule.

<?php
// 查询问题和评分表格,并按评分值降序排列
$query = "SELECT q.id, q.title, q.content, AVG(r.rating) AS average_rating
          FROM questions q
          LEFT JOIN ratings r ON q.id = r.question_id
          GROUP BY q.id
          ORDER BY average_rating DESC";
// 执行查询操作

while ($row = mysqli_fetch_assoc($result)) {
  // 显示问题列表
  echo '<h3>' . $row['title'] . '</h3>';
  echo '<p>' . $row['content'] . '</p>';
  echo '<p>评分:' . $row['average_rating'] . '</p>';
}
?>
Copy after login

Through the above code example, we show how to use PHP to develop question scoring and sorting functions in a knowledge Q&A website. Using the rating function, users can evaluate questions and provide references for other users. Using the sorting function, users can browse and search questions based on their rating and quickly find high-quality answers. Of course, issues such as security and performance optimization also need to be taken into consideration in actual development. I hope this article will be helpful to PHP developers in developing question scoring and sorting functions in knowledge Q&A websites.

The above is the detailed content of Developed question scoring and sorting functionality in a trivia website using PHP.. 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!