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).
Article table (article):
<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>
<?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; ?>
<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>
<?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>
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!