


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).
-
Article table (article):
- id: article ID (primary key)
- title: article title
- content: Article content
- category_id: Category ID (foreign key)
- ## Tag table (tags):
- id: Tag ID (primary key)
- name: Tag name
- Categories table (categories):
- id: Category ID ( Primary key)
- name: Category name
- Add article page
<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>
- Processing article submission
<?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; ?>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 categoriesFinally, 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
<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>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
<?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 id="标签">标签</h3> <ul> <?php while ($tagRow = mysqli_fetch_assoc($tagsResult)) { echo "<li>".$tagRow['name']."</li>"; } ?> </ul> <h3 id="分类">分类</h3> <ul> <li><?php echo $categoryRow['name']; ?></li> </ul>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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.