Home > Backend Development > PHP Tutorial > Use PHP to develop the timed posting and top function of questions in the knowledge Q&A website.

Use PHP to develop the timed posting and top function of questions in the knowledge Q&A website.

PHPz
Release: 2023-07-02 14:42:01
Original
749 people have browsed it

Use PHP to develop the scheduled question publishing and top functions in the knowledge Q&A website

In the knowledge Q&A website, the publishing and sorting of questions are very important functions. Scheduled publishing and top functions can help administrators better manage website content and provide a better user experience. This article will introduce how to use PHP to develop the scheduled question posting and pinning functions in a knowledge Q&A website, and provide corresponding code examples.

  1. Scheduled publishing function

The scheduled publishing function allows the administrator to set the publishing time of the question in advance and automatically publish the question after the specified time is reached. This makes it easier for administrators to prepare questions in advance and arrange content release time reasonably.

First, create a field in the database to store the release time of the question, and add an index to improve query efficiency. You can use the DATETIME type to store date and time information.

ALTER TABLE `questions` ADD `publish_time` DATETIME AFTER `content`;
ALTER TABLE `questions` ADD INDEX (`publish_time`);
Copy after login

Then, in the processing code for issue release, add the logic to determine the release time. When it's before publishing time, save the issue to the database instead of publishing immediately.

<?php
$publish_time = $_POST['publish_time'];
$current_time = date("Y-m-d H:i:s");

if ($publish_time > $current_time) {
    // 保存问题到数据库,而不发布
    $sql = "INSERT INTO questions (title, content, publish_time) VALUES ('$title', '$content', '$publish_time')";
} else {
    // 直接发布问题
    $sql = "INSERT INTO questions (title, content) VALUES ('$title', '$content')";
}

// 执行 SQL 语句
$result = mysqli_query($conn, $sql);

if ($result) {
    echo "问题发布成功!";
} else {
    echo "问题发布失败!";
}
?>
Copy after login
  1. Top function

The pin function can place a specific question at the front of the question list, making it easier for users to find it. Typically, pinned questions appear before other questions and remain pinned for a period of time.

Add a field is_top to the question table in the database to store the status of whether the question is top. Set its data type to Boolean type BIT, which defaults to 0.

In the code displayed in the question list, add the logic to determine whether the question is pinned to the top. Depending on the pinned status, the pinned question is displayed first and then other questions.

<?php
$sql = "SELECT * FROM questions ORDER BY is_top DESC, publish_time DESC";
$result = mysqli_query($conn, $sql);

while ($row = mysqli_fetch_assoc($result)) {
    // 显示问题信息
    // ...
}
?>
Copy after login

Add an interface to set the top status of the question. Through interface calls, administrators can easily pin questions to the top or unpin them.

<?php
$question_id = $_POST['question_id'];
$is_top = $_POST['is_top'];

$sql = "UPDATE questions SET is_top = $is_top WHERE id = $question_id";
$result = mysqli_query($conn, $sql);

if ($result) {
    echo "问题置顶状态更新成功!";
} else {
    echo "问题置顶状态更新失败!";
}
?>
Copy after login

Through the above steps, we can implement the function of regularly publishing and pinning questions in the knowledge question and answer website. These features can improve the efficiency of content management on the website and allow users to better access and participate in discussion of issues.

The above is the detailed content of Use PHP to develop the timed posting and top function of questions 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