Home headlines php paging query

php paging query

Jun 28, 2018 pm 02:02 PM

* Principle of paging query

* Analysis of paging principle:

* 1. The role of LIMIT parameter: offset and display quantity

* 2. If Control the number displayed on each page

* 3. Receive GET parameters, use p to represent the current page number, and display 3 items per page

* 4. Required parameters:

* (1).totalPage Total number of pages

* (2).totalNumber How many pieces of data are there in total

* (3).pageSize How many pieces of data are displayed on each page

* (4)currentPage current page

* (5)*.rangeStart starting page

* (6)*.rangeEnd last page

* 5. Current The calculation formula of offset: (number of pages-1)*number displayed on each page

* offset = (page-1)*num

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

$page = isset($_GET['p'])? $_GET['p']:1;

$page = ($page == 0 ) ? 1 : $page;

$num = 5;

$offset = ($page-1)*$num;

//1.获取到所有数据,用表格显示出来

$pdo = new PDO('mysql:host=localhost;dbname=php','root', 'root');

//$sql = "SELECT id,name,email FROM user1 LIMIT 0,3";

$sql = "SELECT `staff_id`,`name`,`sex`,`age`,`salary` FROM `staff` LIMIT :offset, :num";

$stmt = $pdo->prepare($sql);

$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);

$stmt->bindParam(':num', $num, PDO::PARAM_INT);

$stmt->execute();

$stmt->setFetchMode(PDO::FETCH_ASSOC);

$res = $stmt->fetchAll();

echo &#39;<h2 align="center">员工信息表</h2>&#39;;

echo &#39;<table border="1" cellspacing="0" cellpadding="5" width="70%" align="center">&#39;;

echo &#39;<tr bgcolor="lightgreen"><th>ID</th><th>用户名</th><th>性别</th><th>年龄</th><th>工资</th></tr>&#39;;

foreach ($res as $row) {

    echo &#39;<tr align="center">&#39;;

    echo &#39;<td>&#39;.$row[&#39;staff_id&#39;].&#39;</td><td>&#39;.$row[&#39;name&#39;].&#39;</td>&#39;;

    $row[&#39;sex&#39;] = $row[&#39;sex&#39;]==0 ? &#39;男&#39; : &#39;<span style="color:red">女</span>&#39;;

    echo &#39;<td>&#39;.$row[&#39;sex&#39;].&#39;</td>&#39;;

    echo &#39;<td>&#39;.$row[&#39;age&#39;].&#39;</td><td>&#39;.$row[&#39;salary&#39;].&#39;</td>&#39;;

    echo &#39;</tr>&#39;;

}

echo &#39;</table>&#39;;

//计算共计多少页?

$stmt2 = $pdo->prepare("SELECT * FROM staff");

$stmt2->execute();

$totalPage = ceil($stmt2->rowCount() / $num);

$page = ($page == $totalPage) ? ($totalPage-1) : $page;

echo &#39;<style>a {margin-left: 10px;text-decoration: none}a:hover{color:red}</style>&#39;;

echo &#39;<h3 align="center">&#39;;

echo &#39;<a href="http://php.io/0427/page.php?p=1">首页</a>&#39;;

echo &#39;<a href="http://php.io/0427/page.php?p=&#39;;

echo (($page-1)==0)?1:($page-1);

echo &#39;">上一页</a>&#39;;

for ($i=1; $i<=$totalPage; $i++) {

    echo &#39;<a href="http://php.io/0427/page.php?p=&#39;.$i.&#39;">&#39;.$i.&#39;</a>&#39;;

}

echo &#39;<a href="http://php.io/0427/page.php?p=&#39;.($page+1).&#39;">下一页</a>&#39;;

echo &#39;<a href="http://php.io/0427/page.php?p=&#39;.$totalPage.&#39;">尾页</a>&#39;;

echo &#39;</h3>&#39;;

Copy after login
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)