<?php
//检查文件是否已加载
if(!function_exists(pageft)){
//定义函数$totle为数据总量,$displaypg为每页显示数
function pageft($totle,$displaypg,$url=''){global $page,$firstcount,$pagenav,$_SERVER;$GLOBALS["displaypg"]=$displaypg;
//用$_GET来接受页参数
if (isset($_GET['page'])){
$page=intval($_GET['page']);
}else{
$page=1;
}
//$_SERVER["REQUEST_URI"]访问此页面的URI(路径)
if(!$url){ $url=$_SERVER["REQUEST_URI"];}
//解析URI,获得数组形式传送给$parse_url
$parse_url=parse_url($url);
//$parse_url["query"],获得?后的变量
$url_query=$parse_url["query"];if($url_query){
$url_query=ereg_replace("(^|&)page=$page","",$url_query);
$url=str_replace($parse_url["query"],$url_query,$url);//在URL后加page查询信息,但待赋值:
if($url_query) $url.="&page"; else $url.="page";
}else {
$url.="?page";
}$lastpg=ceil($totle/$displaypg); //最后页,也是总页数
$page=min($lastpg,$page);
$prepg=$page-1; //上一页
$nextpg=($page==$lastpg ? 0 : $page+1); //下一页
$firstcount=($page-1)*$displaypg;//开始分页导航条代码:
$pagenav="显示第 <B>".($totle?($firstcount+1):0)."</B>-<B>".min($firstcount+$displaypg,$totle)."</B> 条记录,共 $totle 条记录<BR>";//如果只有一页则跳出函数:
if($lastpg<=1) return false;$pagenav.=" <a href='$url=1'>首页</a> ";
if($prepg) $pagenav.=" <a href='$url=$prepg'>前页</a> "; else $pagenav.=" 前页 ";
if($nextpg) $pagenav.=" <a href='$url=$nextpg'>后页</a> "; else $pagenav.=" 后页 ";
$pagenav.=" <a href='$url=$lastpg'>尾页</a> ";//下拉跳转列表,循环列出所有页码:
$pagenav.=" 到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
for($i=1;$i<=$lastpg;$i++){
if($i==$page) $pagenav.="<option value='$i' selected>$i</option>\n";
else $pagenav.="<option value='$i'>$i</option>\n";
}
$pagenav.="</select> 页,共 $lastpg 页";
}
}
?>
pageft.php
============================================================
1.php
<?php
require ("conn.php");
include("pageft.php");
$result=mysql_query("select * from userinfo");
$total=mysql_num_rows($result);pageft($total,5);$result=mysql_query("select * from userinfo limit $firstcount,$displaypg ");
while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
?>
<table width="488" height="174" border="1">
<tr>
<td width="42" height="26"> Id:</td>
<td width="169" align="center"><?php print $row["id"];?></td>
<td width="63">Nickname:</td>
<td width="186" align="center"><?php print $row["nickname"];?></td>
</tr>
<tr>
<td height="25"> Name:</td>
<td align="center"><?php print $row["name"];?></td>
<td>Birthdry:</td>
<td align="center"><?php print $row["birthday"];?></td>
</tr>
<tr>
<td height="24">Email:</td>
<td align="center"><?php print $row["mail"];?></td>
<td> Photo:</td>
<td align="center"><?php print $row["phone"];?></td>
</tr>
<tr>
<td height="27"> Logo:</td>
<td rowspan="3"><img align="middle" src="<?php print $row["logo"]?>"></td>
<td> question:</td>
<td align="center"><?php print $row["question"];?></td>
</tr>
<tr>
<td rowspan="2"> </td>
<td height="26"> answer:</td>
<td align="center"><?php print $row["answer"];?></td>
</tr>
<tr>
<td height="30" colspan="2" align="center"><?php print $row["lastlogin"];?></td>
</tr>
</table>
<?}
echo $pagenav;
?>
//检查文件是否已加载
if(!function_exists(pageft)){
//定义函数$totle为数据总量,$displaypg为每页显示数
function pageft($totle,$displaypg,$url=''){global $page,$firstcount,$pagenav,$_SERVER;$GLOBALS["displaypg"]=$displaypg;
//用$_GET来接受页参数
if (isset($_GET['page'])){
$page=intval($_GET['page']);
}else{
$page=1;
}
//$_SERVER["REQUEST_URI"]访问此页面的URI(路径)
if(!$url){ $url=$_SERVER["REQUEST_URI"];}
//解析URI,获得数组形式传送给$parse_url
$parse_url=parse_url($url);
//$parse_url["query"],获得?后的变量
$url_query=$parse_url["query"];if($url_query){
$url_query=ereg_replace("(^|&)page=$page","",$url_query);
$url=str_replace($parse_url["query"],$url_query,$url);//在URL后加page查询信息,但待赋值:
if($url_query) $url.="&page"; else $url.="page";
}else {
$url.="?page";
}$lastpg=ceil($totle/$displaypg); //最后页,也是总页数
$page=min($lastpg,$page);
$prepg=$page-1; //上一页
$nextpg=($page==$lastpg ? 0 : $page+1); //下一页
$firstcount=($page-1)*$displaypg;//开始分页导航条代码:
$pagenav="显示第 <B>".($totle?($firstcount+1):0)."</B>-<B>".min($firstcount+$displaypg,$totle)."</B> 条记录,共 $totle 条记录<BR>";//如果只有一页则跳出函数:
if($lastpg<=1) return false;$pagenav.=" <a href='$url=1'>首页</a> ";
if($prepg) $pagenav.=" <a href='$url=$prepg'>前页</a> "; else $pagenav.=" 前页 ";
if($nextpg) $pagenav.=" <a href='$url=$nextpg'>后页</a> "; else $pagenav.=" 后页 ";
$pagenav.=" <a href='$url=$lastpg'>尾页</a> ";//下拉跳转列表,循环列出所有页码:
$pagenav.=" 到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
for($i=1;$i<=$lastpg;$i++){
if($i==$page) $pagenav.="<option value='$i' selected>$i</option>\n";
else $pagenav.="<option value='$i'>$i</option>\n";
}
$pagenav.="</select> 页,共 $lastpg 页";
}
}
?>
pageft.php
============================================================
1.php
<?php
require ("conn.php");
include("pageft.php");
$result=mysql_query("select * from userinfo");
$total=mysql_num_rows($result);pageft($total,5);$result=mysql_query("select * from userinfo limit $firstcount,$displaypg ");
while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
?>
<table width="488" height="174" border="1">
<tr>
<td width="42" height="26"> Id:</td>
<td width="169" align="center"><?php print $row["id"];?></td>
<td width="63">Nickname:</td>
<td width="186" align="center"><?php print $row["nickname"];?></td>
</tr>
<tr>
<td height="25"> Name:</td>
<td align="center"><?php print $row["name"];?></td>
<td>Birthdry:</td>
<td align="center"><?php print $row["birthday"];?></td>
</tr>
<tr>
<td height="24">Email:</td>
<td align="center"><?php print $row["mail"];?></td>
<td> Photo:</td>
<td align="center"><?php print $row["phone"];?></td>
</tr>
<tr>
<td height="27"> Logo:</td>
<td rowspan="3"><img align="middle" src="<?php print $row["logo"]?>"></td>
<td> question:</td>
<td align="center"><?php print $row["question"];?></td>
</tr>
<tr>
<td rowspan="2"> </td>
<td height="26"> answer:</td>
<td align="center"><?php print $row["answer"];?></td>
</tr>
<tr>
<td height="30" colspan="2" align="center"><?php print $row["lastlogin"];?></td>
</tr>
</table>
<?}
echo $pagenav;
?>
// 实现分页效果
//-----------------------------
class PageChange
{
/*
**变量$page:所请求页码.
**变量$page_size:页面结果行数.
**变量$result_amount:从数据库提取出的结果总数.
**变量$page_count:由$page_size,$result_amount计算得到的页面数量.
**变量$linkbar:由以上变量生成的分页导航.
**变量$linkid:浏览器GET变量.
*/
var $page,$page_size,$result_amount,$page_count,$linkbar,$linkid;
/*
**PageChange构造函数,获取$_GET["page"],$page_size,.
*/
function PageChange($inputPage,$inputPageSize,$inputResultAmount,$linkid)
{
$this->page=$inputPage;
$this->page_size=$inputPageSize;
$this->result_amount=$inputResultAmount;
if($linkid=="")
{
$this->linkid="";
}
else
{
$this->linkid=$linkid."&";
}
}
/*
**函数ReturnPage(),返回请求页数page.
*/
function ReturnPage()
{
return $this->page;
}
/*
**函数ReturnPageSize(),返回请求页数page_size.
*/
function ReturnPageSize()
{
return $this->page_size;
}
/*
**函数ReturnResultAmount(),返回从数据库中提取的结果总数.
*/
function ReturnResultAmount()
{
return $this->result_amount;
}
/*
**函数ReturnPageCount(),返回总页数page_count.
*/
function ReturnPageCount()
{
if($this->result_amount)
{
if($this->result_amount<$this->page_size){$this->page_count=1;}
if($this->result_amount%$this->page_size){$this->page_count=(int)($this->result_amount/$this->page_size)+1;}
else{$this->page_count=$this->result_amount/$this->page_size;}
}
else
{$this->page_count=0;}
return $this->page_count;
}
/*
**函数PrintPageBar(),打印分页导航.
*/
function PrintPageBar()
{
$page=$this->page;
$page_count=$this->page_count; $this->linkbar="("."$page".'/'."$page_count".")";
if($page==1)
{$this->linkbar.=" 第一页 | 上一页 | ";}
else{$this->linkbar.= ' <a href=?'.$this->linkid.'page=1>第一页</a> | <a href=?'.$this->linkid.'page='.($page-1).'>上一页</a> | ';}
if(($page==$page_count)||($page_count==0)){$this->linkbar.='下一页 | 尾页';}
else{$this->linkbar.= '<a href=?'.$this->linkid.'page='.($page+1).'>下一页</a> | <a href=?'.$this->linkid.'page='.$page_count.'>尾页</a>';} echo "<div id='pagebar'>";
echo "$this->linkbar";
echo "</div>";
}
}
$p = new page($recordcount, 20, 5);//(总记录数,每页记录数,每面页码个数)
$p->ar = array("┊首 页", "┊上一页", "┊下一页", "┊末 页");//可以设置成图片HTML代码
$smarty->assign("pages",$p->show_page());还具有limit的功能:$limit = $p -> limit()//分页类
class page{
var $pagecount; //总页数
var $limit = ""; //分页时用到的limit参数
var $pagearg = "page"; //分页参数名称
var $pagesize = 20; //每页显示记录数
var $recordcount; //总记录数
var $pagenum; //当前页码
var $argstr = ""; //GET参数字符串
var $perpage = 5; //每次显示页码个数
var $ar = array("[«]", "[‹]", "[›]", "[»]");
/*
* 功能:构造函数
* 参数 $recordcount 为记录总数
* 参数 $pagesize 为每页显示记录数,默认为20
* 参数 $perpage 为每次显示页码个数,默认为5
*/
function page($recordcount, $pagesize = 20, $perpage = 5){
$this->pagesize = $pagesize;
$this->recordcount = $recordcount;
$this->perpage = $perpage;
$this->pagecount = @ceil($recordcount/$pagesize);//总页数
$this->pagenum = $this->currentpage();//当前页码
$this->argstr = $this->newarg();//GET参数字符串
$this->limit = " LIMIT ".($this->pagenum-1)*$pagesize.",".$pagesize;//分页时用到的limit参数
}
/*
* 功能:取得当前页码函数
*/
function currentpage(){
if(isset($_GET[$this->pagearg])){
if($_GET[$this->pagearg] <= 0) {
$page = 1;
}else if($_GET[$this->pagearg] > $this->pagecount){
$page = $this->pagecount;
}else{
$page = $_GET[$this->pagearg];
}
}else {
$page = 1;
}
return $page;
}
/*
* 功能:重新整理GET参数
*/
function newarg(){
$str = "";
$urlar = $_GET;
unset($urlar[$this->pagearg]);
if($urlar){
foreach($urlar as $key=>$val){
if($str == "") {
$str = "?$key=$val";
}else {
$str .= "&$key=$val";
}
}
$str .= "&$this->pagearg=";
}else{
$str = "?$this->pagearg=";
}
return $str;
}
/*
* 功能:返回MYSQL语句的limit部分代码
*/
function limit(){
return $this->limit;
}
/*
* 功能:分页字符输出函数
*/
function show_page(){
$trunpage = " 共[".$this->recordcount."]条记录┊共[".$this->pagecount."]页 ";
$page = $this->pagenum;
//向前翻
if($page == 1){
$trunpage .= $this->ar[0]." ".$this->ar[1]." ";
}else {
$trunpage .= '<a href="'.$this->argstr.'1">'.$this->ar[0].'</a> <a href="'.$this->argstr.($page-1).'">'.$this->ar[1].'</a> ';
}
//中间数字翻页
$numpage = 1;
while($page - $this->perpage * $numpage > 0) $numpage++;
$startpage = ($numpage - 1) * $this->perpage;
$endpage = $this->pagecount < $numpage * $this->perpage ? $this->pagecount : $numpage * $this->perpage;
for($i = $startpage + 1; $i <= $endpage; $i++){
$trunpage .= $i != $page ? "<a href=\"".$this->argstr.$i."\">$i</a> " : "<strong>".$i."</strong> ";
}
//向后翻
if($page == $this->pagecount || $this->pagecount == 0){
$trunpage .= $this->ar[2]." ".$this->ar[3]." ";
}else {
$trunpage .= '<a href="'.$this->argstr.($page+1).'">'.$this->ar[2].'</a> <a href="'.$this->argstr.$this->pagecount.'">'.$this->ar[3].'</a> ';
}
//跳转
$select = "<select onchange='location.href=this.options[this.selectedIndex].value'>\n";
for($i = 1; $i <= $this->pagecount; $i++){
$select .= "<option value=\"".$this->argstr.$i."\">$i</option>\n";
}
$select .= "</select>";
return $trunpage.$select;
}
}//END CLASS