PHP函数实现分页含文本分页和数字分页,
PHP函数实现分页含文本分页和数字分页,
最近,在项目中要用到分页。分页功能是经常使用的一个功能,所以,对其以函数形式进行了封装。
// 分页分装 /** * $pageType 分页类型 1是数字分页 2是文本分页 * 可以将$pageTotal,$page,$total等数据作为参数传递,或者在paging作为全局变量(推荐) */ function paging($pageType) { global $pageTotal,$page,$total; if($pageType == 1) { echo '<div id="pagenum">'; echo'<ul>'; for($i=0; $i < $pageTotal; $i++) { if($page == ($i+1)) { echo '<li><a href="blogfriends.php?page='.($i+1).'" class="selected">'.($i+1).'</a></li>'; } else { echo '<li><a href="blogfriends.php?page='.($i+1).'">'.($i+1).'</a></li>'; } } echo'</ul>'; echo'</div>'; } else if($pageType == 2) { echo '<div id="pagetext">'; echo '<ul>'; echo '<li>'.$page.'/'.$pageTotal.'页 | </li>'; echo '<li>共有<strong>'.$total .'</strong>个会员 | </li>'; // 第一页 if($page == 1) { echo '<li>首页 | </li>'; echo '<li>上一页 | </li>'; } else { // $_SERVER["SCRIPT_NAME"]获取当前的脚本名字,方便移植 // 也可以自定义常量,常量值和脚本文件名一致 echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'">首页 </a>| </li>'; echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'?page='.($page - 1).'">上一页 </a>| </li>'; } // 最后一页 if($page == $pageTotal) { echo '<li>下一页 | </li>'; echo '<li>尾页 | </li>'; } else { echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'?page='.($page + 1).'">下一页 </a>| </li>'; echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'?page='.($pageTotal).'">尾页 </a>| </li>'; } echo '</ul>'; echo '</div>'; } }
参数解释:
$pageTotal是总页数,$page是当前页,$total是从数据库中获取的数据总数;
为了简化,将参数全部封装
// 分页参数分装 /** * $sql 可以 获取数据总数的一个sql语句 * $size 每一页显示条数 */ function pageParam($sql,$size) { // 将所有涉及的参数设置全局变量 // $pagestart 某一页从哪里开始 // $total 总记录数 $page 某一页 $pageTotal 总页数 global $pagestart,$pagesize,$total,$page,$pageTotal; $pagesize = $size; // 获取数据总数 $total = mysql_num_rows(queryDB($sql)); // 错误处理,先判断是否存在 if(isset($_GET['page'])) { // 具体某一页 $page = $_GET['page']; // 判断是否为空(0是空)/小于0/是否是数字 if(empty($page) || $page < 0 || !is_numeric($page)) { $page = 1; } else { $page = intval($page); //取整,防止小数出现 } } else { // 初始化显示第1页 $page = 1; } // 数据库清零 if($total == 0) { // 设置为1 $pageTotal = 1; } else { // 分页的总页数(进一取整处理) $pageTotal = ceil($total / $pagesize); } // 页数大于总页码$total if($page > $pageTotal) { $page = $pageTotal; } // 当页从某一条记录开始 $pagestart = ($page - 1) * $pagesize; }
参数解释:
$pagestart是当页从某一条记录开始,$pagesize是每页显示的记录数
在使用中,先调用pageParam,再调用paging
/** * 第一个 可以 获取数据总数的一个sql语句 * 第二个 每一页显示条数 */ pageParam("select userid from user",2); <?php // 分页类型 1是数字分页 2是文本分页 paging(2); ?>
调用的位置根据具体情况选择,文本分页如下:
<?php // 分页类型 1是数字分页 2是文本分页 paging(1); ?>
数字分页如下:
样式自行调整。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



PHP development: How to implement table data sorting and paging functions In web development, processing large amounts of data is a common task. For tables that need to display a large amount of data, it is usually necessary to implement data sorting and paging functions to provide a good user experience and optimize system performance. This article will introduce how to use PHP to implement the sorting and paging functions of table data, and give specific code examples. The sorting function implements the sorting function in the table, allowing users to sort in ascending or descending order according to different fields. The following is an implementation form

CakePHP is a powerful PHP framework that provides developers with many useful tools and features. One of them is pagination, which helps us divide large amounts of data into several pages, making browsing and manipulation easier. By default, CakePHP provides some basic pagination methods, but sometimes you may need to create some custom pagination methods. This article will show you how to create custom pagination in CakePHP. Step 1: Create a custom pagination class First, we need to create a custom pagination class. this

As data continues to grow, tabular display becomes more difficult. Most of the time, the amount of data in a table is so large that it becomes slow to load and users need to constantly browse the page to find the data they want. This article will introduce how to use JavaScript to realize paginated display of table data, making it easier for users to find the data they want. 1. Dynamically create tables. In order to make the paging function more controllable, tables need to be created dynamically. In the HTML page, add a table element similar to the one below.

How to use JavaScript to implement table paging function? With the development of the Internet, more and more websites use tables to display data. In some cases where the amount of data is large, the data needs to be displayed in pages to improve user experience. This article will introduce how to use JavaScript to implement table paging function and provide specific code examples. 1. HTML structure First, we need to prepare an HTML structure to host tables and paging buttons. We can use <tab

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

Vue component practice: Introduction to paging component development In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use. In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples. Technology stack Vue.js2.xJavaScript (ES6) HTML5 and CSS3 development environment

There are two most common ways to paginate PHP arrays: using the array_slice() function: calculate the number of elements to skip, and then extract the specified range of elements. Use built-in iterators: implement the Iterator interface, and the rewind(), key(), current(), next(), and valid() methods are used to traverse elements within the specified range.

How to use Layui to develop a data display page with paging function Layui is a lightweight front-end UI framework that provides simple and beautiful interface components and a rich interactive experience. During development, we often encounter situations where we need to display large amounts of data and perform paging. The following is an example of a data display page with paging function developed using Layui. First, we need to introduce Layui related files and dependencies. Add the following code to the <head> tag of the html page
