Home  >  Article  >  Backend Development  >  How to query content using PHP pagination

How to query content using PHP pagination

PHPz
PHPzOriginal
2023-04-10 14:12:57628browse

PHP is a popular programming language used for developing web applications. PHP is a very useful tool when it comes to extracting data from databases and displaying them in a more readable way. In most cases, querying data from a database is better than loading all the data onto a single page. In this case, paginated display is very useful because it allows the user to access more data in an acceptable manner without causing the page to load slowly. In this article, we will show how to use PHP pagination to query content and display them correctly on a web page.

The first step is to connect to the database. In order to connect to the database, you need to know the following database connection parameters: hostname, username, password, and database name. In this example, we will connect to the database named "mydatabase":

<?php
$servername = "localhost";
$username = "myusername";
$password = "mypassword";
$dbname = "mydatabase";

// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);

// 检查连接是否成功
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}
echo "连接成功";
?>

Next, we need to get the content from the database. Use SQL queries to get content from the database. In this example, we will get the articles from a table called "articles":

// 获取文章总数
$sql = "SELECT COUNT(*) FROM articles";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_row($result);
$total_articles = $row[0];

// 分页查询文章
$per_page = 10;
if (isset($_GET["page"])) {
    $page = intval($_GET["page"]);
} else {
    $page = 1;
}
$start = ($page-1) * $per_page;
$sql = "SELECT * FROM articles LIMIT $start, $per_page";
$result = mysqli_query($conn, $sql);

In this example, we first get the total number of articles from the database, and then calculate the number of articles displayed on each page . Next, we use the $_GET variable to get the number of pages the user requested and figure out where to start displaying the article. Finally, we use the LIMIT statement to get the paginated articles from the database.

Next, we need to display the content correctly on the web page:

// 在网页上显示文章
echo "<ul>";
while ($row = mysqli_fetch_assoc($result)) {
    echo "<li><a href=&#39;".$row["url"]."&#39;>".$row["title"]."</a></li>";
}
echo "</ul>";

// 显示分页导航
$total_pages = ceil($total_articles / $per_page);
echo "<div class=&#39;pagination&#39;>";
if ($page > 1) {
    echo "<a href=&#39;?page=".($page-1)."&#39;>上一页</a>";
}
for ($i=1; $i<=$total_pages; $i++) {
    if ($page == $i) {
        echo "<span class=&#39;current&#39;>".$i."</span>";
    } else {
        echo "<a href=&#39;?page=".$i."&#39;>".$i."</a>";
    }
}
if ($page < $total_pages) {
    echo "<a href=&#39;?page=".($page+1)."&#39;>下一页</a>";
}
echo "</div>";

In this example, we use a loop statement to display the articles queried from the database. Each article is displayed as a hyperlink that points to the article's URL address. Next, we display paginated navigation, including previous page, next page, and page number. Please note that we use CSS styles to beautify our paginated navigation.

Through the examples shown in this article, you should now understand how to use PHP to query content from a database and display them on a web page in pages. This approach allows you to provide a better user experience without sacrificing page loading speed, allowing users to find the information they need faster.

The above is the detailed content of How to query content using PHP pagination. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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