Home Backend Development PHP Tutorial Methods and simple examples of paging display of data in PHP

Methods and simple examples of paging display of data in PHP

Jun 02, 2018 am 09:55 AM
php method show

This article mainly introduces the method and simple examples of paging data display in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

Paging is a frequently used function in background management. Paging display facilitates the management of large amounts of data.

The example code is as follows:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>用户列表</title>
</head>
<body>
<?php 
  $con = mysql_connect("localhost","root","");
  
  mysql_query("set names utf8");
  mysql_select_db("zhiye",$con);
  
  $pageSize = 1;   //每页显示的数量
  $rowCount = 0;   //要从数据库中获取
  $pageNow = 1;    //当前显示第几页
  
  //如果有pageNow就使用,没有就默认第一页。
  if (!empty($_GET['pageNow'])){
    $pageNow = $_GET['pageNow'];
  }
  
  $pageCount = 0;  //表示共有多少页
  
  $sql1 = "select count(id) from user";
  $res1 = mysql_query($sql1);
  
  if($row1=mysql_fetch_row($res1)){
    $rowCount = $row1[0];
  }
  
  //计算共有多少页,ceil取进1
  $pageCount = ceil(($rowCount/$pageSize));
  
  //使用sql语句时,注意有些变量应取出赋值。
  $pre = ($pageNow-1)*$pageSize;
  
  $sql2 = "select * from user limit $pre,$pageSize";
  $res2 = mysql_query($sql2);
 
  while($row=mysql_fetch_assoc($res2)){
    echo $row['user_name']."<br>";
    echo $row['name']."<br>";
    echo $row['email']."<br>";
    echo $row['password']."<br>";
    echo $row['tel']."<br>";
  }
  for ($i=1;$i<=$pageCount;$i++){
    echo "<a href='userList.php?pageNow=$i'>$i</a> ";
  }
?>
</body>
</html>
Copy after login

The above method cannot be used when there is a large amount of data.

<?php 
  $con = mysql_connect("localhost","root","");
  
  mysql_query("set names utf8");
  mysql_select_db("zhiye",$con);
  
  $pageSize = 1;   //每页显示的数量
  $rowCount = 0;   //要从数据库中获取
  $pageNow = 1;    //当前显示第几页
  
  //如果有pageNow就使用,没有就默认第一页。
  if (!empty($_GET['pageNow'])){
    $pageNow = $_GET['pageNow'];
  }
  
  $pageCount = 0;  //表示共有多少页
  
  $sql1 = "select count(id) from user";
  $res1 = mysql_query($sql1);
  
  if($row1=mysql_fetch_row($res1)){
    $rowCount = $row1[0];
  }
  
  //计算共有多少页,ceil取进1
  $pageCount = ceil(($rowCount/$pageSize));
  
  //使用sql语句时,注意有些变量应取出赋值。
  $pre = ($pageNow-1)*$pageSize;
  
  $sql2 = "select * from user limit $pre,$pageSize";
  $res2 = mysql_query($sql2);
  
  //$sql = "select * from user";
  //$res = mysql_query($sql,$con);
 
  while($row=mysql_fetch_assoc($res2)){
    echo $row['user_name']."<br>";
    echo $row['name']."<br>";
    echo $row['email']."<br>";
    echo $row['password']."<br>";
    echo $row['tel']."<br>";
  }
  if($pageNow>1){
    $prePage = $pageNow-1;
    echo "<a href='userList.php?pageNow=$prePage'>pre</a> ";
  }
  if($pageNow<$pageCount){
    $nextPage = $pageNow+1;
    echo "<a href='userList.php?pageNow=$nextPage'>next</a> ";
    echo "当前页{$pageNow}/共{$pageCount}页";
  }
  echo "<br/><br/>";
  ?>
 
  <form action="userList.php">
    <input type="text" name="pageNow">
    <input type="submit" value="GO">
  </form>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

How to implement scheduled execution function based on sleep function in PHP

php Input and output Detailed explanation and example analysis of streams

Detailed explanation of PHP output buffer control

The above is the detailed content of Methods and simple examples of paging display of data in PHP. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles