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.
##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 pageThe 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['name']. '|'.$row['age'].'
'; } }//html添加页数部分1; 2; 3; 4; 5; 总共页,当前在页
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!