Home > Backend Development > PHP Tutorial > PHP implements question tag statistics and search functions in the knowledge question and answer website.

PHP implements question tag statistics and search functions in the knowledge question and answer website.

WBOY
Release: 2023-07-01 22:36:01
Original
1423 people have browsed it

PHP implements the question label statistics and search function in the knowledge question and answer website

In the knowledge question and answer website, the question label is an important identifier to help users quickly find related questions. In this article, we will use PHP to implement a simple knowledge question and answer website and add question tag statistics and search functions.

First, we need to create a database table to store the problem information. We can use the following SQL statement to create a table named "questions":

CREATE TABLE `questions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `content` text NOT NULL,
  `tags` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

The table contains the following fields:

  • id: The unique question Identity, using a self-increasing integer type.
  • title: The title of the question, using string type.
  • content: The content of the question, using text type.
  • tags: Tags of the question, separate multiple tags with commas, and use string type.

Next, we need to create a PHP file to handle the tag statistics and search functions of the problem. Assume that our PHP file is named "question_search.php". The following is the content of the file:

<?php
// 连接数据库
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("连接数据库失败: " . $conn->connect_error);
}

// 统计问题标签
$sql = "SELECT tags, COUNT(*) as count FROM questions GROUP BY tags";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo "标签:" . $row["tags"] . ",问题数量:" . $row["count"] . "<br>";
    }
} else {
    echo "暂无标签";
}

// 搜索问题
$search = $_GET["search"] ?? "";
if ($search !== "") {
    $search = mysqli_real_escape_string($conn, $search);
    $sql = "SELECT * FROM questions WHERE title LIKE '%$search%' OR content LIKE '%$search%'";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            echo "标题:" . $row["title"] . "<br>内容:" . $row["content"] . "<br>标签:" . $row["tags"] . "<br><br>";
        }
    } else {
        echo "暂无搜索结果";
    }
}

// 关闭数据库连接
$conn->close();
?>
Copy after login

The functions of the above code are as follows:

  1. Connect to the database: based on your own database information Configuration of database connection.
  2. Count question labels: Use SQL query statements to count the number of each label in the question table and output the results.
  3. Search questions: Get the search keywords passed by the user through the GET request, use SQL query statements to search for questions that contain the keywords in the title and content, and output the results.

In the front-end page of the knowledge question and answer website, you can implement question tag statistics and search functions by introducing this PHP file. The following is an example of the front-end code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>知识问答网站</title>
</head>
<body>
    <h1>知识问答网站</h1>

    <h2>问题标签统计</h2>
    <?php include("question_search.php"); ?>

    <h2>搜索问题</h2>
    <form action="question_search.php" method="get">
        <input type="text" name="search" placeholder="请输入关键词">
        <button type="submit">搜索</button>
    </form>
</body>
</html>
Copy after login

By introducing the "question_search.php" file, the page will display question tag statistics and a search box. Users can enter keywords and click the search button, and the search results will be displayed on the page.

Through the above code examples, we have successfully implemented the question tag statistics and search functions in a simple knowledge question and answer website. You can make appropriate adjustments and expansions according to actual needs to meet more functional requirements.

The above is the detailed content of PHP implements question tag statistics and search functions in the knowledge question and answer website.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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