PHP分页函数代码(简单实用型)_PHP教程
程序员文章站
2022-04-09 09:27:55
...
准备数据:
新建一个数据库 test
执行下面的语句(新建一个表 test :id、sex、name 三个字段)
CREATE TABLE `test` (
`id` INT( 4 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`sex` INT( 1 ) NOT NULL ,
`name` VARCHAR( 20 ) NOT NULL ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_bin;
添加数据到 test 表,比如:
1 1 小强
2 0 小红
3 0 小丽
4 1 小兵
5 1 张三
6 0 李四
7 0 吴欣
写PHP语句(index.php):
// Page分页函数
$page = $_GET["page"];
function Page($rows,$page_size){
global $page,$select_from,$select_limit,$pagenav;
$page_count = ceil($rows/$page_size);
if($page if($page >= $page_count) $page = $page_count;
$select_limit = $page_size;
$select_from = ($page - 1) * $page_size.',';
$pre_page = ($page == 1)? 1 : $page - 1;
$next_page= ($page == $page_count)? $page_count : $page + 1 ;
$pagenav .= "第 $page/$page_count 页 共 $rows 条记录 ";
$pagenav .= "首页 ";
$pagenav .= "前一页 ";
$pagenav .= "后一页 ";
$pagenav .= "末页";
$pagenav.=" 跳到
浏览 index.php 页面,如图:
该说byebye了,确实好用!
PHP简单分页函数
写了一个php简单的分页函数,数据库调用也写到里面了 ,用的人可以自行删减!
function getask(){
$sql = "select * from cms_ask where ansower ' ' ";
//这里要改成方法
$q_sq = mysql_query($sql);
$count = mysql_num_rows($q_sq);
$page_size = 8;
$page_current = isset($GLOBALS['page']) ? intval($GLOBALS['page']) : 1;
$page_count = ceil($count / $page_size);
$page_start = $page_current - 4;
$page_end = $page_current + 4;
if ($page_current $page_start = 1;
$page_end = 5;
}
if ($page_current > $page_count - 4) {
$page_start = $page_count - 8;
$page_end = $page_count;
}
if ($page_start $page_start = 1;
if ($page_end > $page_count)
$page_end = $page_count;
$pagebar = "";
$sql = "select * from cms_ask where ansower ' ' order by id desc limit " . (($page_current - 1) * $page_size) . "," . $page_size;
$row=$this -> user -> getall("$sql");
foreach($row as $v){
echo '
}
$pagebar .= "
echo $pagebar;
}
新建一个数据库 test
执行下面的语句(新建一个表 test :id、sex、name 三个字段)
CREATE TABLE `test` (
`id` INT( 4 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`sex` INT( 1 ) NOT NULL ,
`name` VARCHAR( 20 ) NOT NULL ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_bin;
添加数据到 test 表,比如:
1 1 小强
2 0 小红
3 0 小丽
4 1 小兵
5 1 张三
6 0 李四
7 0 吴欣
写PHP语句(index.php):
复制代码 代码如下:
// Page分页函数
$page = $_GET["page"];
function Page($rows,$page_size){
global $page,$select_from,$select_limit,$pagenav;
$page_count = ceil($rows/$page_size);
if($page if($page >= $page_count) $page = $page_count;
$select_limit = $page_size;
$select_from = ($page - 1) * $page_size.',';
$pre_page = ($page == 1)? 1 : $page - 1;
$next_page= ($page == $page_count)? $page_count : $page + 1 ;
$pagenav .= "第 $page/$page_count 页 共 $rows 条记录 ";
$pagenav .= "首页 ";
$pagenav .= "前一页 ";
$pagenav .= "后一页 ";
$pagenav .= "末页";
$pagenav.=" 跳到
浏览 index.php 页面,如图:
该说byebye了,确实好用!
PHP简单分页函数
写了一个php简单的分页函数,数据库调用也写到里面了 ,用的人可以自行删减!
复制代码 代码如下:
function getask(){
$sql = "select * from cms_ask where ansower ' ' ";
//这里要改成方法
$q_sq = mysql_query($sql);
$count = mysql_num_rows($q_sq);
$page_size = 8;
$page_current = isset($GLOBALS['page']) ? intval($GLOBALS['page']) : 1;
$page_count = ceil($count / $page_size);
$page_start = $page_current - 4;
$page_end = $page_current + 4;
if ($page_current $page_start = 1;
$page_end = 5;
}
if ($page_current > $page_count - 4) {
$page_start = $page_count - 8;
$page_end = $page_count;
}
if ($page_start $page_start = 1;
if ($page_end > $page_count)
$page_end = $page_count;
$pagebar = "";
$sql = "select * from cms_ask where ansower ' ' order by id desc limit " . (($page_current - 1) * $page_size) . "," . $page_size;
$row=$this -> user -> getall("$sql");
foreach($row as $v){
echo '
- 问:'.$v["question"].'
- 答:'.$v["ansower"].date("Y-m-d H:i:s",$v["postTime"]).'
}
$pagebar .= "
";
$pagebar .= "
$pagebar .= "
"; $pagebar .= "
- ";
- FIRST ';
- " . $i . " ";
- " . $i . " ";
- END ";
if ($page_current != 1) {
$pagebar .= '
}
for ($i = $page_start; $i if ($i == $page_current) {
$pagebar .= "
} else {
$pagebar .= "
}
}
if ($page_current != $page_count) {
$pagebar .= "
}
$pagebar .= "
$pagebar .= "
echo $pagebar;
}
推荐阅读
-
PHP如何通过传引用的思想实现无限分类(代码简单),php引用_PHP教程
-
PHP杂谈《重构-改善既有代码的设计》之重新组织你的函数_PHP教程
-
PHP删除Array数组里指定的key(完整版,已封装成函数,附测试代码)_PHP教程
-
Web端PHP代码函数覆盖率测试解决方案_PHP教程_编程技术
-
php简单对象与数组的转换函数代码(php多层数组和对象的转换)_php技巧
-
比较好用的PHP防注入漏洞过滤函数代码_PHP教程
-
php删除文件夹及其文件夹下所有文件的函数代码_PHP教程
-
PHP分页初探 一个最简单的PHP分页代码的简单实现,分页初探
-
分享PHP函数实现数字与文字分页代码_php实例
-
做了CDN获取用户真实IP的函数代码(PHP与Asp设置方式)_PHP教程