* 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
$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 '<h2 id="员工信息表">员工信息表</h2>'; echo '<table border="1" cellspacing="0" cellpadding="5" width="70%" align="center">'; echo '<tr bgcolor="lightgreen"><th>ID</th><th>用户名</th><th>性别</th><th>年龄</th><th>工资</th></tr>'; foreach ($res as $row) { echo '<tr align="center">'; echo '<td>'.$row['staff_id'].'</td><td>'.$row['name'].'</td>'; $row['sex'] = $row['sex']==0 ? '男' : '<span style="color:red">女</span>'; echo '<td>'.$row['sex'].'</td>'; echo '<td>'.$row['age'].'</td><td>'.$row['salary'].'</td>'; echo '</tr>'; } echo '</table>'; //计算共计多少页? $stmt2 = $pdo->prepare("SELECT * FROM staff"); $stmt2->execute(); $totalPage = ceil($stmt2->rowCount() / $num); $page = ($page == $totalPage) ? ($totalPage-1) : $page; echo '<style>a {margin-left: 10px;text-decoration: none}a:hover{color:red}</style>'; echo '<h3 align="center">'; echo '<a href="http://php.io/0427/page.php?p=1">首页</a>'; echo '<a href="http://php.io/0427/page.php?p='; echo (($page-1)==0)?1:($page-1); echo '">上一页</a>'; for ($i=1; $i<=$totalPage; $i++) { echo '<a href="http://php.io/0427/page.php?p='.$i.'">'.$i.'</a>'; } echo '<a href="http://php.io/0427/page.php?p='.($page+1).'">下一页</a>'; echo '<a href="http://php.io/0427/page.php?p='.$totalPage.'">尾页</a>'; echo '</h3>';

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)