Home  >  Article  >  Backend Development  >  How to implement php paging function

How to implement php paging function

(*-*)浩
(*-*)浩Original
2019-10-09 09:02:463123browse

In a website, if there is too much content in the list page, the paging function is usually set up. Today we will study the paging principle and implement this function through a small case.

How to implement php paging function

##Page Principle

Page display, the data taken out from the mysql database is displayed in regular sections, and the limit in the sql statement is used to follow its starting data to the page Number binding, go to the database to get data according to the number of pages (recommended learning:

PHP video tutorial)

Implementation process

1. Get the current url String, and use parse_url to parse to get the url array

2. Connect to the server, obtain the content collection to be displayed on the list page, count the total number of items displayed, and then calculate the total number of pages of content

3. Determine whether the page is submitted. If not, it defaults to the first page $pageval

4. Assign the calculated limit starting position to the variable $page

5. Use $page and $pageSize Go to the database to get the data

6. Loop through the data collection, and the output is displayed on the page

The specific code is as follows

$url = $_SERVER['REQUEST_URI'];
$url = parse_url($url);
$url = $url['path'];
$pageSize = 4;//连接服务器$link = mysqli_connect('127.0.0.1','root',12345678,'bbs');
$res = mysqli_query($link,'select * from fenye');
$num = mysqli_num_rows($res);
$pageNum = ceil($num/$pageSize);//判断页面是否是提交状态if ( isset($_GET['page']) && $_GET['page'] >1) {
    $pageVal = $_GET['page'];
}else {
    $pageVal = 1;
}//计算起始位置$page = ($pageVal-1)*$pageSize;//去数据库取数据$res = $mysqli_query($link,"select * from fenye limit $page,$pageSize");//如果$res有值,则循环便利结果展示输出在页面if ($res) {    while( $row = mysqli_fetch_assoc($res) ) {    echo $row[&#39;name&#39;]. &#39;|&#39;.$row[&#39;age&#39;].&#39;<br/>&#39;;
    }
}//html添加页数部分<a href="?page=1">1</a>;
<a href="?page=2">2</a>;
<a href="?page=3">3</a>;
<a href="?page=4">4</a>;
<a href="?page=5">5</a>;
总共<?php echo $pageNum; ?>页,当前在<?php echo $pageVal;?>页

The above is the detailed content of How to implement php paging function. 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