Advanced PHP Programming: Design and Implementation of Like Function for Multiple Articles

WBOY
Release: 2024-02-28 08:04:02
Original
566 people have browsed it

Advanced PHP Programming: Design and Implementation of Like Function for Multiple Articles

As a PHP developer, we often encounter the need to add a like function to a website or application. This article will introduce how to design and implement a multi-article like function through advanced PHP programming, and provide specific code examples.

1. Functional requirements analysis

Before designing the function of liking multiple articles, we first need to clarify our functional requirements:

  1. Users can view the functions on the website Post multiple articles and like each article.
  2. Users can only like each article once. When the user has already liked it, he cannot like it again.
  3. Users can check how many people like each article and can cancel likes.

2. Database design

In order to implement the like function, we need to store the user's like information in the database. Next, we design a simple database table to store like information:

CREATE TABLE likes (
    id INT AUTO_INCREMENT PRIMARY KEY,
    article_id INT,
    user_id INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Copy after login

In the above table, we store the id, article id, user id and creation time of the like record.

3. Page design

We will create a page containing multiple articles and display a like button under each article. When the user clicks the like button, we will send the like information to the backend through an AJAX request.

The following is a simple page design example:

<!DOCTYPE html>
<html>
<head>
    <title>多篇文章点赞功能</title>
</head>
<body>
    <div>
        <h1>文章标题1</h1>
        <p>文章内容1</p>
        <button class="like-btn" data-article-id="1">点赞</button>
    </div>
    <div>
        <h1>文章标题2</h1>
        <p>文章内容2</p>
        <button class="like-btn" data-article-id="2">点赞</button>
    </div>
</body>
</html>
Copy after login

4. PHP backend implementation

In the PHP backend, we will write code to receive AJAX requests and process them The logic of the like function. The following is a simple PHP code example:

<?php
require('db.php'); // 连接数据库

if ($_POST['action'] == 'like') {
    $article_id = $_POST['article_id'];
    $user_id = 1; // 假设用户id为1

    $sql = "SELECT * FROM likes WHERE article_id = $article_id AND user_id = $user_id";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) == 0) {
        $sql = "INSERT INTO likes (article_id, user_id) VALUES ($article_id, $user_id)";
        mysqli_query($conn, $sql);
        echo "点赞成功";
    } else {
        echo "您已经点赞过了";
    }
}
?>
Copy after login

5. Front-end AJAX request

Finally, we need to write front-end JS code to handle the AJAX request sent by clicking the like button. The following is a simple JS code example:

document.querySelectorAll('.like-btn').forEach((btn) => {
    btn.addEventListener('click', function() {
        const articleId = this.getAttribute('data-article-id');

        fetch('like.php', {
            method: 'POST',
            body: JSON.stringify({action: 'like', article_id: articleId})
        })
        .then(response => response.text())
        .then(data => {
            alert(data);
        })
        .catch(error => console.error('Error:', error));
    });
});
Copy after login

Through the above steps, we can implement a simple like function for multiple articles. When the user clicks the Like button, the backend will receive an AJAX request, check whether the user has already liked it, and then update the database record.

I hope this article will be helpful to the design and implementation of the like function in advanced PHP programming. This is just a simple example. Practical applications may require more complex logic and optimization, and need to be further improved and expanded by developers.

The above is the detailed content of Advanced PHP Programming: Design and Implementation of Like Function for Multiple Articles. 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