• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP分页

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

<!--PageSupportClass.php:数据分页显示类------------------------------------>
<?php
/*********************************************
类名: 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
"<div align=center>";
echo
"<form action=".$_SERVER['PHP_SELF']." method=\"get\">";

echo
"<font color = red size ='4'>第".$this->current_page."页/共".$this->total_pages."页</font>";
echo
"    ";

echo
"跳到<input type=\"text\" size=\Ř\" name=\"current_page\" value='".$this->current_page."'/>页";
echo
"<input type=\"submit\" value=\"提交\"/>";
    

//生成导航链接
if ($this->current_page > 1) {
  echo
"<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->first.">首页</A>|";
  echo
"<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->prev.">上一页</A>|";
}

if( $this->current_page < $this->total_pages) {
  echo
"<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->next.">下一页</A>|";
  echo
"<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->last.">末页</A>";
}

echo
"</form>";
echo
"</div>";

}

  
/*********************************************
函数名:full_navigate()
功能: 显示首页、下页、上页、未页  
生成导航链接 如1 2 3 ... 10 11
***********************************************/

function full_navigate()
{
echo
"<div align=center>";
echo
"<form action=".$_SERVER['PHP_SELF']." method=\"get\">";

echo
"<font color = red size ='4'>第".$this->current_page."页/共".$this->total_pages."页</font>";
echo
"    ";

echo
"跳到<input type=\"text\" size=\Ř\" name=\"current_page\" value='".$this->current_page."'/>页";
echo
"<input type=\"submit\" value=\"提交\"/>";
    
//生成导航链接 如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;$i<$this->current_page;$i++){
echo
"<a href=".$_SERVER['PHP_SELF']."?page=".$i.">[".$i ."]</a> ";
}

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

    $displayCount = $this->display_count;
if($this->total_pages > $displayCount&&($this->current_page+$displayCount)<$this->total_pages){
$displayCount = $this->current_page+$displayCount;
}else{
$displayCount = $this->total_pages;
    }

for($i=$this->current_page+1;$i<=$displayCount;$i++){
echo
"<a href=".$_SERVER['PHP_SELF']."?current_page=".$i.">[".$i ."]</a> ";
    }

//生成导航链接
if ($this->current_page > 1) {
  echo
"<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->first.">首页</A>|";
  echo
"<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->prev.">上一页</A>|";
}

if( $this->current_page < $this->total_pages) {
  echo
"<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->next.">下一页</A>|";
  echo
"<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->last.">末页</A>";
}

echo
"</form>";
echo
"</div>";

}

}
?>

调用:

<?php

include_once(
"../config_jj/sys_conf.inc");
include_once(
"../PageSupportClass.php");//分页类
include_once('../Smarty_JsnhClass.php');

$smarty = new Smarty_Jsnh();
include_once(
"../include/Smarty_changed_dir.php");
$smarty->assign('title',
"Smarty新闻分页测试");

<?php

$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 news ");
$pageSupport->read_data();
//读数据

if ($pageSupport->current_records > 0)
//如果数据不为空,则组装数据
{
for ($i=0; $i<$pageSupport->current_records; $i++)
{
$title = $pageSupport->result[$i][
"title"];
$id = $pageSupport->result[$i][
"id"];

$news_arr[$i] = array('news' => array('id' => $id,'title' => $title));

}
}

//关闭数据库
mysql_close($db);

$pageinfo_arr = array(
'total_records' => $pageSupport->total_records,
'current_page'  => $pageSupport->current_page,
'total_pages'   => $pageSupport->total_pages,
'first'         => $pageSupport->first,
'prev'          => $pageSupport->prev,
'next'          => $pageSupport->next,
'last'          => $pageSupport->last
                );

$smarty->assign('results', $news_arr);
$smarty->assign('pageSupport', $pageinfo_arr);
$smarty->display('news/list.tpl');

?>
模板list.tpl
{* I am a Smarty comment, I don't exist in the compiled output  *}
{*
{$pageSupport.total_records}<br/>
{$pageSupport.current_page}<br/>
{$pageSupport.total_pages}<br/>
{$pageSupport.first}<br/>
{$pageSupport.prev}<br/>
{$pageSupport.next}<br/>
{$pageSupport.last}<br/>
*}
<html>
<head>
<meta http-equiv=
"Content-Type" content="text/html; charset=gbk" />
<title>{$title}</title>
</head>
<body>

{foreach item=o from=$results}
    {$o.news.id}  {$o.news.title}
    <br>  
{foreachelse}
    没有您要查看的数据!
{/foreach}  

<br/>


{if ( $pageSupport.total_records > 0 )}

<form action=
"" method="get">
共{$pageSupport.total_records}记录
第{$pageSupport.current_page}页/共{$pageSupport.total_pages}页
{if ( $pageSupport.current_page > 1 )}
   <A href=?current_page={$pageSupport.first}>首页</A>
   <A href=?current_page={$pageSupport.prev}>上一页</A>
{/if}

{if ( $pageSupport.current_page < $pageSupport.total_pages )}
   <A href=?current_page={$pageSupport.next}>下一页</A>
   <A href=?current_page={$pageSupport.last}>末页</A>
{/if}

  跳到<input type=
"text" size="4" name="current_page" value="{$pageSupport.current_page}"/>页
  <input type=
"submit" value="GO"/>
</form>

{/if}


</body>
</html>


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
【PHP】防止视频资源被下载的解决办法发布时间:2022-07-10
下一篇:
php求和发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap