Develop question search functionality in a trivia website using PHP.

王林
Release: 2023-07-02 10:44:02
Original
1217 people have browsed it

Use PHP to develop the question search function in the knowledge Q&A website

In the knowledge Q&A website, users often need to use the search function to find specific questions and answers. This article will introduce how to use PHP to develop the question search function in a knowledge Q&A website and provide corresponding code examples.

First, we need a question and answer database table to store the questions asked by the user and the corresponding answers. Suppose we have created a table called "questions" with the following fields: id (a unique identifier for the question), title (the title of the question), content (the content of the question), and answer (the answer to the question).

Next, we need a search page to allow users to enter keywords to search. First, create a file named "search.php" and add the following code example in the file:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>问题搜索</title>
</head>
<body>
    <h1>问题搜索</h1>
    <form action="search.php" method="GET">
        <input type="text" name="keyword" placeholder="输入关键词">
        <input type="submit" value="搜索">
    </form>

    <?php
        // 判断是否接收到关键词
        if(isset($_GET['keyword'])) {
            // 连接数据库
            $conn = new mysqli('localhost', 'username', 'password', 'database');

            // 获取用户输入的关键词
            $keyword = $_GET['keyword'];

            // 查询数据库中匹配关键词的问题
            $sql = "SELECT * FROM questions WHERE title LIKE '%" . $keyword . "%' OR content LIKE '%" . $keyword . "%'";
            $result = $conn->query($sql);

            // 遍历查询结果并输出
            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    echo "<h2>" . $row['title'] . "</h2>";
                    echo "<p>" . $row['content'] . "</p>";
                    echo "<p>回答:" . $row['answer'] . "</p>";
                }
            } else {
                echo "未找到匹配的问题";
            }

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

</body>
</html>
Copy after login

In the above code, we first create a search form to allow users to enter keywords. Then, the PHP code determines whether the keyword is received and stores it in the "keyword" variable.

Next, we connect to the database and use SQL query statements to search for questions matching keywords in the "questions" table. The query statement uses the LIKE operator to fuzzy match the title and content of the question. The query results are stored in the "result" variable.

We then use a loop to iterate through the query results and output the title, content, and answers of the matching questions.

If the query result is empty, we will output the prompt message "No matching question found".

Finally, we close the database connection.

Now, we have completed the development of the basic question search function. Users can enter keywords on the search page to search and obtain questions and answers that match the keywords.

I hope this article has helped you understand how to use PHP to develop the question search function in a knowledge question and answer website. You can adjust and optimize accordingly according to your needs. Happy coding!

The above is the detailed content of Develop question search 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!