Home > Article > Backend Development > PHP programming skills: Implement the like function for multiple articles
Title: PHP Programming Skills: Implementing Like Function for Multiple Articles
In website development, the like function is one of the common and important functions. It can Increase user interactivity and improve user experience. In this article, we will discuss how to use PHP to implement the like function for multiple articles. We will use PHP and MySQL databases to implement this function, and provide specific code examples so that readers can better understand the implementation process.
Step 1: Create a database
First, we need to create a database to store article information and the number of likes. We create a database named "articles", containing three fields: id, title, likes. Among them, id is the unique identifier of the article, title is the title of the article, and likes is the number of likes for the article.
CREATE DATABASE articles; USE articles; CREATE TABLE articles ( id INT(11) AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, likes INT(11) DEFAULT 0 );
Step 2: Display the article list
We create a PHP file to display the list of all articles and add a like button to each article. Users can click a button to like the article.
<?php //连接数据库 $servername = "localhost"; $username = "root"; $password = ""; $dbname = "articles"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } //查询数据库获取文章列表 $sql = "SELECT * FROM articles"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "<div>"; echo "<h2>" . $row['title'] . "</h2>"; echo "<button onclick='likeArticle(" . $row['id'] . ")'>点赞</button>"; echo "<span id='likes_" . $row['id'] . "'>" . $row['likes'] . "</span>"; echo "</div>"; } } else { echo "暂无文章"; } $conn->close(); ?>
Step 3: Implement the like function
We create a JavaScript function to handle the like function and update the number of likes to the database through an Ajax request.
function likeArticle(article_id) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("likes_" + article_id).innerHTML = this.responseText; } }; xmlhttp.open("GET", "like_article.php?id=" + article_id, true); xmlhttp.send(); }
Step 4: Update the number of likes
Create a PHP file named "like_article.php" to handle the like request and update the number of likes for the article in the database.
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "articles"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $article_id = $_GET['id']; $sql_update = "UPDATE articles SET likes = likes + 1 WHERE id = $article_id"; if ($conn->query($sql_update) === TRUE) { $sql_select = "SELECT likes FROM articles WHERE id = $article_id"; $result = $conn->query($sql_select); $row = $result->fetch_assoc(); echo $row['likes']; } else { echo "Error updating record: " . $conn->error; } $conn->close(); ?>
Through the above steps, we have successfully implemented the like function for multiple articles. Users can browse articles on the website and like the ones they like. After clicking the button, the number of likes in the database will be updated through Ajax request and displayed to the user in real time. In this way, it not only improves the user experience, but also increases user interactivity.
I hope the above content will be helpful to you, and I hope you can successfully apply these techniques in actual projects and improve your website development capabilities.
The above is the detailed content of PHP programming skills: Implement the like function for multiple articles. For more information, please follow other related articles on the PHP Chinese website!