php mysql 分页 函数,php+mysql分页类

本文介绍了一个用于分页显示MySQL数据库数据的PHP类PageSupport,包括其属性和方法,如构造函数、数据读取、分页导航显示等功能。通过实例化PageSupport对象,设置SQL查询和每页显示数量,可以轻松实现数据的分页显示,并提供了标准和完整两种导航样式。示例代码展示了如何在PHP页面中使用该类进行数据读取和分页导航的展示。
摘要由CSDN通过智能技术生成

封装的类:

/*********************************************

类名: PageSupport

功能:分页显示MySQL数据库中的数据

***********************************************/

class PageSupport{

//属性

var $sql; //所要显示数据的SQL查询语句

var $page_size; //每页显示最多行数

var $start_index; //所要显示记录的首行序号

var $total_records; //记录总数

var $current_records; //本页读取的记录数

var $result; //读出的结果

var $total_pages; //总页数

var $current_page; //当前页数

var $display_count = 30; //显示的前几页和后几页数

var $arr_page_query; //数组,包含分页显示需要传递的参数

var $first;

var $prev;

var $next;

var $last;

//方法

/*********************************************

构造函数:__construct()

输入参数:

$ppage_size:每页显示最多行数

***********************************************/

function PageSupport($ppage_size)

{

$this->page_size=$ppage_size;

$this->start_index=0;

}

/*********************************************

构造函数:__destruct()

输入参数:

***********************************************/

function __destruct()

{

}

/*********************************************

get函数:__get()

***********************************************/

function __get($property_name)

{

if(isset($this->$property_name))

{

return($this->$property_name);

}

else

{

return(NULL);

}

}

/*********************************************

set函数:__set()

***********************************************/

function __set($property_name, $value)

{

$this->$property_name = $value;

}

/*********************************************

函数名:read_data

功能: 根据SQL查询语句从表中读取相应的记录

返回值:属性二维数组result[记录号][字段名]

***********************************************/

function read_data()

{

$psql=$this->sql;

//查询数据,数据库链接等信息应在类调用的外部实现

$result=mysql_query($psql) or die(mysql_error());

$this->total_records=mysql_num_rows($result);

//利用LIMIT关键字获取本页所要显示的记录

if($this->total_records>0)

{

$this->start_index = ($this->current_page-1)*$this->page_size;

$psql=$psql. " LIMIT ".$this->start_index." , ".$this->page_size;

$result=mysql_query($psql) or die(mysql_error());

$this->current_records=mysql_num_rows($result);

//将查询结果放在result数组中

$i=0;

while($row=mysql_fetch_Array($result))

{

$this->result[$i]=$row;

$i++;

}

}

//获取总页数、当前页信息

$this->total_pages=ceil($this->total_records/$this->page_size);

$this->first=1;

$this->prev=$this->current_page-1;

$this->next=$this->current_page+1;

$this->last=$this->total_pages;

}

/*********************************************

函数名:standard_navigate()

功能: 显示首页、下页、上页、未页

***********************************************/

function standard_navigate()

{

echo "

";

echo "

";

echo "第".$this->current_page."页/共".$this->total_pages."页";

echo " ";

echo "跳到页";

echo "";

//生成导航链接

if ($this->current_page > 1) {

echo "first.">首页|";

echo "prev.">上一页|";

}

if( $this->current_page < $this->total_pages) {

echo "next.">下一页|";

echo "last.">末页";

}

echo "

";

echo "

";

}

/*********************************************

函数名:full_navigate()

功能: 显示首页、下页、上页、未页

生成导航链接 如1 2 3 ... 10 11

***********************************************/

function full_navigate()

{

echo "

";

echo "

";

echo "第".$this->current_page."页/共".$this->total_pages."页";

echo " ";

echo "跳到页";

echo "";

//生成导航链接 如1 2 3 ... 10 11

$front_start = 1;

if($this->current_page > $this->display_count){

$front_start = $this->current_page - $this->display_count;

}

for($i=$front_start;$icurrent_page;$i++){

echo "[".$i ."] ";

}

echo "[".$this->current_page."]";

$displayCount = $this->display_count;

if($this->total_pages > $displayCount&&($this->current_page+$displayCount)total_pages){

$displayCount = $this->current_page+$displayCount;

}else{

$displayCount = $this->total_pages;

}

for($i=$this->current_page+1;$i<=$displayCount;$i++){

echo "[".$i ."] ";

}

//生成导航链接

if ($this->current_page > 1) {

echo "first.">首页|";

echo "prev.">上一页|";

}

if( $this->current_page < $this->total_pages) {

echo "next.">下一页|";

echo "last.">末页";

}

echo "

";

echo "

";

}

}

?>

写在php页面里面的代码:

include_once("fenye_php.php"); //引入类

///

$con = mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("myblog", $con); //选取数据库

$PAGE_SIZE=10; //设置每页显示的数目

///

$pageSupport = new PageSupport($PAGE_SIZE); //实例化PageSupport对象

$current_page=$_GET["current_page"];//分页当前页数

if (isset($current_page)) {

$pageSupport->__set("current_page",$current_page);

} else {

$pageSupport->__set("current_page",1);

}

$pageSupport->__set("sql","select * from article ");

$pageSupport->read_data();//读数据

if ($pageSupport->current_records > 0) //如果数据不为空,则组装数据

{

for ($i=0; $icurrent_records; $i++)

{

$title = $pageSupport->result[$i]["title"];

$content = $pageSupport->result[$i]["content"];

$part=substr($content,0,400);

//循环输出每条数据

echo '

'.$title.'
'.$part.'
';

}

}

$pageSupport->standard_navigate(); //调用类里面的这个函数,显示出分页HTML

//关闭数据库

mysql_close($con);

?>

来自:http://blog.csdn.net/phpfenghuo/article/details/23207099

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值