How to use PHP to develop simple article labeling and classification functions

WBOY
Release: 2023-09-20 17:16:01
Original
877 people have browsed it

How to use PHP to develop simple article labeling and classification functions

How to use PHP to develop simple article labeling and classification functions

In the process of website development, article labeling and classification functions are very common requirements. By adding tags and categories to articles, users can browse and filter them according to their needs. This article will introduce how to use PHP to develop simple article tags and classification functions through specific code examples.

1. Database design

First, we need to design a database table to store articles and corresponding tags and classification information. We can design three tables: article table (article), tags table (tags) and category table (categories).

  1. Article table (article):

    • id: article ID (primary key)
    • title: article title
    • content: Article content
    • category_id: Category ID (foreign key)
  2. ## Tag table (tags):

      id: Tag ID (primary key)
    • name: Tag name
  3. Categories table (categories):

      id: Category ID ( Primary key)
    • name: Category name
2. Adding tags to articles and classifying functions

Next, let’s add tags to articles and classification functions. When users publish an article, they can choose the tag and category of the article.

    Add article page
First, create a page to add articles (add_article.php). The page contains a form where users can fill in the title, content, and Tags and categories:

<form action="add_article.php" method="POST">
    <input type="text" name="title" placeholder="文章标题"><br><br>
    <textarea name="content" placeholder="文章内容"></textarea><br><br>
    <input type="text" name="tags" placeholder="标签(多个标签用逗号分隔)"><br><br>
    <select name="category">
        <option value="">选择分类</option>
        <!-- 根据分类表动态生成选项 -->
    </select><br><br>
    <input type="submit" value="发布文章">
</form>
Copy after login

    Processing article submission
In the add_article.php file, we need to process the form data submitted by the user and add the title, content, and tags of the article and classification are stored in the database:

<?php
// 连接数据库
$conn = mysqli_connect("localhost", "username", "password", "database");

// 处理提交的数据
$title = $_POST['title'];
$content = $_POST['content'];
$tags = $_POST['tags'];
$category = $_POST['category'];

// 插入文章数据
$result = mysqli_query($conn, "INSERT INTO article (title, content, category_id) VALUES ('$title', '$content', '$category')");

// 获取插入文章的ID
$articleId = mysqli_insert_id($conn);

// 处理标签
$tagArray = explode(",", $tags);
foreach ($tagArray as $tag) {
    $tag = trim($tag);
    // 查询标签是否存在
    $tagResult = mysqli_query($conn, "SELECT id FROM tags WHERE name = '$tag'");
    if (mysqli_num_rows($tagResult) == 0) {
        // 标签不存在,插入新的标签
        mysqli_query($conn, "INSERT INTO tags (name) VALUES ('$tag')");
        // 获取插入标签的ID
        $tagId = mysqli_insert_id($conn);
    } else {
        $tagId = mysqli_fetch_assoc($tagResult)['id'];
    }
    // 插入标签和文章的关联关系
    mysqli_query($conn, "INSERT INTO article_tags (article_id, tag_id) VALUES ($articleId, $tagId)");
}

// 关闭数据库连接
mysqli_close($conn);

// 跳转到文章详情页
header("Location: article.php?id=$articleId");
exit;
?>
Copy after login

In the above code, we first connect to the database, and then obtain the article data submitted by the user. Next, we insert the title, content, and category of the article into the article table. Then, we split the label field into an array by commas and iterate through each label in the array. For each tag, we first query whether the tag already exists in the database. If it does not exist, insert a new tag and obtain the ID of the tag. Finally, we insert the article ID and tag ID into the article tag table.

3. Display article tags and categories

Finally, let’s implement the function of displaying article tags and categories. Users can click on the article tag or category to view the corresponding article list.

    Display article list page
First, create a page that displays the article list (articles.php). The page contains all article titles and corresponding tags and categories. :

<ul>
    <?php
    // 连接数据库
    $conn = mysqli_connect("localhost", "username", "password", "database");

    // 查询文章数据
    $result = mysqli_query($conn, "SELECT * FROM article");

    // 输出文章列表
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<li><a href='article.php?id=".$row['id']."'>".$row['title']."</a>";
        
        // 查询文章的标签
        echo "<ul>";
        $tagResult = mysqli_query($conn, "SELECT t.name FROM tags t INNER JOIN article_tags at ON t.id = at.tag_id WHERE at.article_id = ".$row['id']);
        while ($tagRow = mysqli_fetch_assoc($tagResult)) {
            echo "<li>".$tagRow['name']."</li>";
        }
        echo "</ul>";

        // 查询文章的分类
        echo "<ul>";
        $categoryResult = mysqli_query($conn, "SELECT c.name FROM categories c WHERE c.id = ".$row['category_id']);
        while ($categoryRow = mysqli_fetch_assoc($categoryResult)) {
            echo "<li>".$categoryRow['name']."</li>";
        }
        echo "</ul>";
        
        echo "</li>";
    }

    // 关闭数据库连接
    mysqli_close($conn);
    ?>
</ul>
Copy after login

In the above code, we first connect to the database, and then query all article data. Next, we use a loop to output the title of each article and query the tags and categories of the corresponding article. Finally, we close the database connection.

    Display article details
When the user clicks on the article title, we need to display the detailed content of the article and the corresponding tags and categories.

Create a page (article.php) that displays the details of the article. The page contains the title, content, tags and categories of the article:

<?php
// 获取文章ID
$articleId = $_GET['id'];

// 连接数据库
$conn = mysqli_connect("localhost", "username", "password", "database");

// 查询文章数据
$articleResult = mysqli_query($conn, "SELECT * FROM article WHERE id = $articleId");
$articleRow = mysqli_fetch_assoc($articleResult);

// 查询文章的标签
$tagsResult = mysqli_query($conn, "SELECT t.name FROM tags t INNER JOIN article_tags at ON t.id = at.tag_id WHERE at.article_id = $articleId");

// 查询文章的分类
$categoryResult = mysqli_query($conn, "SELECT c.name FROM categories c WHERE c.id = ".$articleRow['category_id']);
$categoryRow = mysqli_fetch_assoc($categoryResult);

// 关闭数据库连接
mysqli_close($conn);
?>

<h1><?php echo $articleRow['title']; ?></h1>
<p><?php echo $articleRow['content']; ?></p>

<h3>标签</h3>
<ul>
    <?php
    while ($tagRow = mysqli_fetch_assoc($tagsResult)) {
        echo "<li>".$tagRow['name']."</li>";
    }
    ?>
</ul>

<h3>分类</h3>
<ul>
    <li><?php echo $categoryRow['name']; ?></li>
</ul>
Copy after login

In the above code, we first obtain the ID of the article, and connect to the database. Then, we query the article data, tags and classification data and output them to the page. Finally, we close the database connection.

Through the above steps, we have realized the use of PHP to develop simple article tags and classification functions. Users can select tags and categories when publishing articles, and browse the corresponding article list and details by clicking on tags and categories. Hope this article is helpful to you!

The above is the detailed content of How to use PHP to develop simple article labeling and classification functions. 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