Home > Backend Development > PHP Tutorial > PHP implements data paging function

PHP implements data paging function

王林
Release: 2023-06-23 13:10:01
Original
1118 people have browsed it

PHP is a server-side scripting language widely used in web development. When we process a large amount of data in a database, we usually need to use data paging technology to divide the data into pages that can be displayed to save server resources and improve user access efficiency. This article will introduce how to use PHP to implement data paging function.

  1. Database connection

First, we need to connect to the database, which is achieved through the following code:

$servername = "localhost"; //数据库主机名
$username = "root"; //数据库用户名
$password = ""; //数据库密码
$dbname = "myDB"; //数据库名称

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

// 检测连接是否成功
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
Copy after login
  1. Get the total number of records

Next, we need to get the total number of records in the database for paging. This can be achieved through the following code:

$sql = "SELECT COUNT(*) FROM myTable"; //myTable为数据表名
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);

$total_records = $row[0]; //获取总记录数
Copy after login
  1. Calculate paging

After we have the total number of records, we can calculate the number of pages that need to be divided and the records displayed on each page number. For example, if each page displays 10 pieces of data and there are 100 pieces of data in total, then we need to divide it into 10 pages. This can be achieved through the following code:

$records_per_page = 10; //每页记录数
$total_pages = ceil($total_records / $records_per_page); //总页数
Copy after login
  1. Get the current page number

When paging, you need to get the current page number to determine the data range that needs to be displayed. The current page number can be obtained through the following code:

if (isset($_GET['page'])) {
    $current_page = $_GET['page'];
} else {
    $current_page = 1; //默认为第一页
}
Copy after login
  1. Calculate the starting record position

With the current page number and the number of records per page, we can calculate the starting query The location is recorded. For example, if you want to query the data on page 3 and display 10 pieces of data on each page, then the starting record position is:

$starting_position = ($current_page - 1) * $records_per_page;
Copy after login
  1. Query data

Calculate the starting After recording the location, we need to query the database and get the data to display. This can be achieved through the following code:

$sql = "SELECT * FROM myTable LIMIT $starting_position, $records_per_page";
$result = mysqli_query($conn, $sql);
Copy after login
  1. Display the paging navigation bar

Finally, display the paging navigation bar at the bottom of the page. This can be achieved through the following code:

echo "<ul class='pagination'>";

if ($current_page > 1) {
    $prev_page = $current_page - 1;
    echo "<li><a href='{$_SERVER['PHP_SELF']}?page=$prev_page'>&laquo;</a></li>";
}

for ($i = 1; $i <= $total_pages; $i++) {
    if ($i == $current_page) {
        echo "<li class='active'><a href='#'>$i</a></li>";
    } else {
        echo "<li><a href='{$_SERVER['PHP_SELF']}?page=$i'>$i</a></li>";
    }
}

if ($current_page < $total_pages) {
    $next_page = $current_page + 1;
    echo "<li><a href='{$_SERVER['PHP_SELF']}?page=$next_page'>&raquo;</a></li>";
}

echo "</ul>";
Copy after login

The above code uses the style of the Bootstrap framework and can adjust the style and layout by itself.

The complete code is as follows:




    PHP数据分页
    



"; echo ""; echo ""; echo ""; echo ""; } ?>
姓名 年龄 性别
" . $row['name'] . "" . $row['age'] . "" . $row['gender'] . "
Copy after login

It should be noted that in actual application, we need to perform security verification on each step of the code to prevent malicious attacks such as SQL injection.

The above is the detailed content of PHP implements data paging 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