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.
Before designing the function of liking multiple articles, we first need to clarify our functional requirements:
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 );
In the above table, we store the id, article id, user id and creation time of the like record.
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>
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 "您已经点赞过了"; } } ?>
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)); }); });
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!