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.
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()); }
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]; //获取总记录数
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); //总页数
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; //默认为第一页 }
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;
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);
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'>«</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'>»</a></li>"; } echo "</ul>";
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 " 姓名 年龄 性别 " . $row['name'] . " "; echo "" . $row['age'] . " "; echo "" . $row['gender'] . " "; echo ""; } ?>
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!