PHP development: How to implement the article like function

王林
Release: 2023-09-20 14:26:06
Original
1232 people have browsed it

PHP development: How to implement the article like function

PHP development: How to implement the article like function, specific code examples are required

Introduction:
In modern social networks and web applications, the like function has already Become a common social interaction feature. Whether it is news, Weibo, blogs or community forums, users can express their love and support for the content by liking it. This article will introduce how to use PHP to develop and implement the article like function, and give specific code examples.

1. Design the database structure
Before starting to implement the article like function, you first need to design the database structure. We can create two tables, one to store article information and the other to store like information.

  1. Article table (article)
    Field:
  2. id: article ID (primary key)
  3. title: article title
  4. content: Article content
  5. Like table (like)
    Field:
  6. id: Like ID (primary key)
  7. article_id: Liked article ID
  8. user_id: ID of the like user

2. Back-end logic to implement the like function
The back-end logic to implement the like function mainly includes two parts: adding likes and canceling like.

  1. Add likes
    When a user clicks the like button of an article, the backend needs to perform the following operations:
  • Determine whether the user has already liked it After reading this article, you can check the like table to determine whether there are relevant records.
  • If the user has already liked the article, no action will be performed.
  • If the user has not liked the article, insert a new record in the likes table.

PHP code example:

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

// 获取文章ID和用户ID
$articleId = $_POST['articleId'];
$userId = $_POST['userId'];

// 查询点赞表
$query = "SELECT * FROM like WHERE article_id = $articleId AND user_id = $userId";
$result = mysqli_query($conn, $query);

// 判断是否已经点赞过该文章
if (mysqli_num_rows($result) > 0) {
    // 用户已经点赞过该文章,不执行任何操作
} else {
    // 用户没有点赞过该文章,插入一条新记录
    $insertQuery = "INSERT INTO like (article_id, user_id) VALUES ($articleId, $userId)";
    mysqli_query($conn, $insertQuery);
}

// 关闭数据库连接
mysqli_close($conn);
?>
Copy after login
  1. Cancel like
    When the user cancels the like, the backend needs to perform the following operations:
  • To determine whether the user has liked the article, you can query the like table to determine whether there are related records.
  • If the user has not liked the article, no action will be performed.
  • If the user has already liked the article, delete the relevant record in the like table.

PHP code example:

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

// 获取文章ID和用户ID
$articleId = $_POST['articleId'];
$userId = $_POST['userId'];

// 查询点赞表
$query = "SELECT * FROM like WHERE article_id = $articleId AND user_id = $userId";
$result = mysqli_query($conn, $query);

// 判断是否已经点赞过该文章
if (mysqli_num_rows($result) > 0) {
    // 用户已经点赞过该文章,删除相关记录
    $deleteQuery = "DELETE FROM like WHERE article_id = $articleId AND user_id = $userId";
    mysqli_query($conn, $deleteQuery);
} else {
    // 用户没有点赞过该文章,不执行任何操作
}

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

3. Front-end implementation of the like function
Add a like button to the front-end page and interact with the back-end. You can use Ajax to Send an asynchronous request.

HTML code example:

<button class="like-button" data-article-id="1">点赞</button>
Copy after login

JavaScript code example:

// 点赞按钮点击事件
$(".like-button").on("click", function() {
    var articleId = $(this).data("article-id");
    var userId = 1; // 假设用户ID为1

    // 发送异步请求到后端
    $.ajax({
        type: "POST",
        url: "like.php",
        data: { articleId: articleId, userId: userId },
        success: function(response) {
            // 更新点赞按钮状态
            $(this).addClass("active");
            alert("点赞成功!");
        }
    });
});
Copy after login

IV. Summary
Through the above implementation steps, we can achieve article likes in PHP development Function. From the writing of back-end logic to the implementation of front-end interaction, we can achieve the effect of users liking articles and getting feedback on the interface. I hope this article can be helpful to the implementation of the like function in PHP development.

Special Note:
The code examples in this article are for reference only. Please make appropriate modifications and optimizations according to specific conditions during the actual development process to ensure the security and performance of the code.

The above is the detailed content of PHP development: How to implement the article like function. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!