【威尼斯正规官网】php+ajax分页代码
分类:博客热点

htmlheadscript language=javascriptfunction createXMLHttp(){ if(window.ActiveXObject){ return new ActiveXObject(Microsoft.XMLHttp); } else if(window.XMLHttpRequest){ return new XMLHttpRequest(); }}

function outPege($ps教程,$page)
{
// $ps 累加新闻数 $page 当前page数 $max 列表的最大数 $min 列表的最小值
$max      = ($page >= 5)? $page+5:10;
if($max > $ps)$max =$ps;
$min      =($page-5);
if($min<1) $min =1;
if($page>1)$pagelist = "<a href="?page=1">首页</a>";
for($i=$min;$i<=$max;$i++){
  $pagelist.= ($i!=$page)? "<a href="?page={$i}">{$i}</a>":"<a href="#">{$page}</a>";
}
$pagelist.= ($page>=$ps)?'':"<a href="?page={$ps}">尾页</a>";
return $pagelist;
}

function Pager(){ var that=this;

//方法二

this.link=function(url){ that.xmlHttp = createXMLHttp(); that.xmlHttp.onreadystatechange = that.receive; that.xmlHttp.open(GET, url, true); that.xmlHttp.send(null); }

/**
* 呈现分页函数
* records总记录数,currentpage当前页数,rows每页记录数,listpage页号列表长度。
*/
function showpage ($records,$currentpage,$rows=20,$listpages=7) {
if($records<1)return '';
if($rows<1)$rows=20;
if($listpages<3)$listpages=7;
$action=$_SERVER['QUERY_STRING']; //获得地点栏中?后的剧情
if($action卡塔尔国{ //将page=...的内容去掉
$action=preg_replace("/&page [^&]*/","",$action);
$action=preg_replace("/ page [^&]*&*/","",$action);
}
if($action)$action.='&';
$maxpage=ceil($records/$rows卡塔尔; //进一法取整得到最大页数。
$middlepage=floor($listpages/2卡塔尔国; //舍去法取整获得要呈现的页数的五成。
if($currentpage<1||$currentpage>$maxpage)$currentpage=1;
$startpage=$currentpage-$middlepage;
if($maxpage-$currentpage<=$middlepage)$startpage=$maxpage-$listpages;
if($startpage<2卡塔尔(قطر‎$startpage=2; //开端页总括实现
$endpage=$startpage+$listpages;
if($endpage>$maxpage卡塔尔国$endpage=$maxpage; //截止页总括完毕
if($currentpage!=1){
$pagestr="<a href='?{$action}page=1'><span>1</span></a> ";
}else{
$pagestr="<span>1</span> ";
}

this.receive=function(){ if((that.xmlHttp.readyState == 4)){ if(that.xmlHttp.status == 200){ that.reaction(that.xmlHttp.responseXML); }else{ that.recover(); } } }}/script/headbodydiv id='page_content'/divdiv id='page_bar'/divscript language=javascriptvar a = new Pager();var pages = 0;

 

a.recover = function (){ go = function (u){ go = function (){ }; a.link(u); };};

if($maxpage==1)return $pagestr."  <span>{$currentpage}/{$maxpage}</span>";
if($startpage!=2)$pagestr.="< ";
for($i=$startpage;$i<$endpage;$i++){
if($i==$currentpage){
$pagestr.="<span>$i</span> ";
}else{
$pagestr.="<a href='?{$action}page=$i'><span>$i</span></a> ";
}
}
if($endpage!=$maxpage)$pagestr.="> ";
if($currentpage>=$maxpage){
$pagestr.="<span>$maxpage</span>";
}else{
$pagestr.="<a href='?{$action}page=$maxpage'><span>$maxpage</span></a>";
}
$pagestr.="  <span>{$currentpage}/{$maxpage}</span>";
return $pagestr;
}

a.reaction = function (xml){ document.getElementById('page_content').innerHTML = xml.getElementsByTagName('content')[0].childNodes[0].nodeValue;

 

if(xml.getElementsByTagName('count')[0].childNodes[0].nodeValue != pages){ s = 'tabletr'; for(i = 1; i = xml.getElementsByTagName('count')[0].childNodes[0].nodeValue; i++) { if(i == xml.getElementsByTagName('current')[0].childNodes[0].nodeValue) s += 'tdb' + i + '/b/td'; else s += 'tda href=javascript:go(/'page.php?page='

//分页类三

  • i + '/')' + i + '/a/td'; } s += '/tr/table'; document.getElementById('page_bar').innerHTML = s; }

<?php
/**
 *-------------------------翻页类----------------------*
 */
class PageClass
{
 private $myde_count;       //总记录数
 var $myde_size;        //每页记录数
 private $myde_page;        //当前页
 private $myde_page_count;  //总页数
 private $page_url;         //页面url
 private $page_i;           //起始页
 private $page_ub;          //结束页
 var $page_limit;
 
 function __construct($myde_count=0, $myde_size=1, $myde_page=1,$page_url卡塔尔(قطر‎//布局函数
 { 
  
  $this -> myde_count = $this -> numeric($myde_count);
  $this -> myde_size  = $this -> numeric($myde_size);
  $this -> myde_page  = $this -> numeric($myde_page);
  $this -> page_limit = ($this -> myde_page * $this -> myde_size) - $this -> myde_size;
  
  $this -> page_url       = $page_url;
  
  if($this -> myde_page < 1) $this -> myde_page =1;
  
  if($this -> myde_count < 0) $this -> myde_page =0;
  
  $this -> myde_page_count  = ceil($this -> myde_count/$this -> myde_size);
  
  if($this -> myde_page_count < 1) $this -> myde_page_count = 1;
  
  if($this -> myde_page > $this -> myde_page_count) $this -> myde_page = $this -> myde_page_count;
  
  $this -> page_i = $this -> myde_page - 2;

a.recover();}

 

a.link('page.php');/script/body/html

  
        $this -> page_ub = $this -> myde_page + 2;
  
        if($this -> page_i < 1){
  
            $this -> page_ub = $this -> page_ub + (1 - $this -> page_i);
   
            $this -> page_i = 1;
        }
       
        if($this -> page_ub > $this -> myde_page_count){
  
            $this -> page_i = $this -> page_i - ($this -> page_ub - $this -> myde_page_count);
   
            $this -> page_ub = $this -> myde_page_count;
   
            if($this -> page_i < 1) $this -> page_i = 1;
        }
 }
 
 
 private function numeric($id卡塔尔(قطر‎ //剖断是不是为数字
 {
  if (strlen($id)){
      if (!ereg("^[0-9]+$",$id)){
    $id = 1;
      }else{
    $id = substr($id,0,11);
    }
  }else{
   $id = 1;
  }
  return $id;
 }
 
 private function page_replace($page卡塔尔国 //地址替换
 {
  return str_replace("{page}", $page, $this -> page_url);
 }
 
 
 private function myde_home() //首页
 {
  if($this -> myde_page != 1){
  
   return "    <li class="page_a"><a href="".$this -> page_replace(1).""  title="首页" >首页</a></li> ";
   
  }else{
  
   return "    <li>首页</li> ";
   
  }
 }
 
 private function myde_prev() //上一页
 {
  if($this -> myde_page != 1){
  
   return "    <li class="page_a"><a href="".$this -> page_replace($this->myde_page-1) .""  title="上一页" >上一页</a></li> ";
   
  }else{
  
   return "    <li>上一页</li> ";
   
  }
 }
 
 private function myde_next() //下一页
 {
  if($this -> myde_page != $this -> myde_page_count){
  
    return "    <li class="page_a"><a href="".$this -> page_replace($this->myde_page+1) .""  title="下一页" >下一页</a></li> ";
    
  }else{

page.php代码.

 

?phpclass class_page{ private $record_count, $perpage; private $page_count, $page_current; function __construct($perpage, $record_count, $page_current) { $this-perpage = $perpage; $this-record_count = $record_count; $this-page_count = ($record_count == 0) ? 1 : ceil($record_count / $perpage); $this-page_current = ($page_current $this-page_count) ? 1 : $page_current; }

  
   return "    <li>下一页</li> ";
   
  }
 }
 
 private function myde_last() //尾页
 {
  if($this -> myde_page != $this -> myde_page_count){
  
    return "    <li class="page_a"><a href="".$this -> page_replace($this -> myde_page_count).""  title="尾页" >尾页</a></li> ";
    
  }else{
  
   return "    <li>尾页</li> ";
   
  }
 }
 
 function myde_write($id='page') //输出
 {
  $str  = "<div id="".$id."" class="pages">   <ul>   ";
  
  $str .= "  <li>总记录:<span>".$this -> myde_count."</span></li> ";
  
  $str .= "    <li><span>".$this -> myde_page."</span>/<span>".$this -> myde_page_count."</span></li> ";
  
  $str .= $this -> myde_home();
  
  $str .= $this -> myde_prev();
  
  for($page_for_i = $this -> page_i;$page_for_i <= $this -> page_ub; $page_for_i++){
  
   if($this -> myde_page == $page_for_i){
   
             $str .= "    <li class="on">".$page_for_i."</li> ";
    
   }
   
   else{
   
    $str .= "    <li class="page_a"><a href="".$this -> page_replace($page_for_i)."" title="第".$page_for_i."页">";
    
    $str .= $page_for_i . "</a></li> ";
    
   }
        }
  $str .= $this -> myde_next();
  
  $str .= $this -> myde_last();
  
  $str .= "    <li class="pages_input"><input type="text" value="".$this -> myde_page.""";
  
  $str .= " onkeydown="javascript教程: if(event.keyCode==13){ location='";
  
  $str .= $this -> page_replace("'+this.value+'")."';return false;}"";
  
  $str .= " title="输入您想要达到的页码" /></li> ";
  
  $str .= "  </ul>   <div class="page_clear"></div> </div>";
  
  return $str;
 }
}

function __get($name) { switch($name) { case 'page_count': return $this-page_count; case 'page_current': return $this-page_current; case 'record_start': return ($this-page_current - 1) * $this-perpage; } }}

 

header('Content-Type: text/xml; charset=gbk');$page = new class_page(20, 150, is_numeric($_GET['page']) ? $_GET['page'] : 1);echo '?xml version=1.0?';echo 'page';echo 'count' . $page-page_count . '/count';echo 'current' . $page-page_current . '/current';echo 'content' . pow($page-page_current, 2) . '/content';echo '/page';?

/*-------------------------实例--------------------------------*
$page = new PageClass(1000,5,$_GET['page'],'?page={page}'卡塔尔国;//用于动态
$page = new PageClass(1000,5,$_GET['page'],'list-{page}.html'卡塔尔国;//用于静态或许伪静态
$page -> myde_write();//显示
*/

 

//分页类四

<?php
/**
演示*/
require_once('../libs/classes/page.class.php');
$page=new page(array('total'=>1000,'perpage'=>20));
echo 'mode:1<br>'.$page->show();
echo '<hr>mode:2<br>'.$page->show(2);
echo '<hr>mode:3<br>'.$page->show(3);
echo '<hr>mode:4<br>'.$page->show(4);
echo '<hr>开始AJAX模式:';
$ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
echo 'mode:1<br>'.$ajaxpage->show();

class minupage
{
/**
* config ,public
*/
var $page_name="p";//page标签,用来支配url页。比方说xxx.php?PB_page=2中的PB_page
var $next_page='>';//下一页
var $pre_page='<';//上一页
var $first_page='First';//首页
var $last_page='Last';//尾页
var $pre_bar='<<';//上一分页条
var $next_bar='>>';//下一分页条
var $format_left='[';
var $format_right=']';
var $is_ajax=false;//是不是扶植AJAX分页形式

/**
* private
*
*/
var $pagebarnum=10;//调整记录条的个数。
var $totalpage=0;//总页数
var $ajax_action_name='';//AJAX动作名
var $nowindex=1;//当前页
var $url="";//url地址头
var $offset=0;

/**
* constructor布局函数
*
* @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']...
*/
function minupage($array)
{
if(is_array($array)){
     if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'need a param of total');
     $total=intval($array['total']);
     $perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;
     $nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
     $url=(array_key_exists('url',$array))?$array['url']:'';
}else{
     $total=$array;
     $perpage=10;
     $nowindex='';
     $url='';
}
if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.' is not a positive integer!');
if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' is not a positive integer!');
if(!empty($array['page_name']))$this->set('page_name',$array['page_name']);//设置pagename
$this->_set_nowindex($nowindex卡塔尔;//设置当前页
$this->_set_url($url卡塔尔;//设置链接地址
$this->totalpage=ceil($total/$perpage);
$this->offset=($this->nowindex-1)*$perpage;
if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//打开AJAX模式
}
/**
* 设定类中钦点变量名的值,倘使改造量不归属那一个类,将throw三个exception
*
* @param string $var
* @param string $value
*/
function set($var,$value)
{
if(in_array($var,get_object_vars($this)))
     $this->$var=$value;
else {
   $this->error(__FUNCTION__,$var." does not belong to PB_Page!");
}

}
/**
* 打开倒AJAX模式
*
* @param string $action 暗中认可ajax触发的动作。
*/

 

function open_ajax($action)
{
$this->is_ajax=true;
$this->ajax_action_name=$action;
}
/**
* 获取突显"下一页"的代码
*
* @param string $style
* @return string
*/
function next_page($style='')
{
if($this->nowindex<$this->totalpage){
   return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
}
return '<span class="'.$style.'">'.$this->next_page.'</span>';
}

 

/**
* 获取突显“上一页”的代码
*
* @param string $style
* @return string
*/
function pre_page($style='')
{
if($this->nowindex>1){
   return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
}
return '<span class="'.$style.'">'.$this->pre_page.'</span>';
}

/**
* 获取展现“首页”的代码
*
* @return string
*/
function first_page($style='')
{
if($this->nowindex==1){
      return '<span class="'.$style.'">'.$this->first_page.'</span>';
}
return $this->_get_link($this->_get_url(1),$this->first_page,$style);
}

/**
* 获取展现“尾页”的代码
*
* @return string
*/
function last_page($style='')
{
if($this->nowindex==$this->totalpage){
      return '<span class="'.$style.'">'.$this->last_page.'</span>';
}
return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
}

function nowbar($style='',$nowindex_style='')
{
$plus=ceil($this->pagebarnum/2);
if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
$begin=$this->nowindex-$plus+1;
$begin=($begin>=1)?$begin:1;
$return='';
for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
{
   if($i<=$this->totalpage){
    if($i!=$this->nowindex)
        $return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));
    else
        $return.=$this->_get_text('<span class="'.$nowindex_style.'">'.$i.'</span>');
   }else{
    break;
   }
   $return.=" ";
}
unset($begin);
return $return;
}
/**
* 获取呈现跳转按键的代码
*
* @return string
*/
function select($url)
{
$return='<select name=";PB_Page_Select" >';
for($i=1;$i<=$this->totalpage;$i++)
{
   if($i==$this->nowindex){
    $return.='<option value='.$url.$i.' selected>'.$i.'</option>';
   }else{
    $return.='<option value='.$url.$i.'>'.$i.'</option>';
   }
}
unset($i);
$return.='</select>';
return $return;
}

/**
* 获取mysql教程 语句中limit必要的值
*
* @return string
*/
function offset()
{
return $this->offset;
}

/**
* 调节分页展现风格(你能够扩张对应的作风)
*
* @param int $mode
* @return string
*/
function show($mode=1,$url='')
{
switch ($mode)
{
   case '1':
    $this->next_page='下一页';
    $this->pre_page='上一页';
    return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select($url).'页';
    break;
   case '2':
    $this->next_page='下一页';
    $this->pre_page='上一页';
    $this->first_page='首页';
    $this->last_page='尾页';
    return $this->first_page().$this->pre_page().'[第'.$this->nowindex.' 页]'.$this->next_page().$this->last_page().'第 '.$this->select($url).'页';
    break;
   case '3':
    $this->next_page='下一页';
    $this->pre_page='上一页';
    $this->first_page='首页';
    $this->last_page='尾页';
    return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
    break;
   case '4':
    $this->next_page='下一页';
    $this->pre_page='上一页';
    return $this->pre_page().$this->nowbar().$this->next_page();
    break;
   case '5':
    return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
    break;
}

 

 

}
/*----------------private function (私有方法State of Qatar-----------------------------------------------------------*/
/**
* 设置url头地址
* @param: String $url
* @return boolean
*/
function _set_url($url="")
{
if(!empty($url)){
      //手动设置
   $this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";
}else{
      //自动获取
   if(empty($_SERVER['QUERY_STRING'])){
       //不存在QUERY_STRING时
    $this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."=";
   }else{
       //
    if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){
        //地址存在页面参数
     $this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_SERVER['REQUEST_URI']);
     $last=$this->url[strlen($this->url)-1];
     if($last=='?'||$last=='&'){
         $this->url.=$this->page_name."=";
     }else{
         $this->url.='&'.$this->page_name."=";
     }
    }else{
        //
     $this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
    }//end if  
   }//end if
}//end if
}

/**
* 设置当前页面
*
*/
function _set_nowindex($nowindex)
{
if(empty($nowindex)){
   //系统获取
 
   if(isset($_GET[$this->page_name])){
    $this->nowindex=intval($_GET[$this->page_name]);
   }
}else{
      //手动设置
   $this->nowindex=intval($nowindex);
}
}

/**
* 为内定的页面重回地址值
*
* @param int $pageno
* @return string $url
*/
function _get_url($pageno=1)
{
return $this->url.$pageno;
}

/**
* 获取分页展现文字,比如说私下认可情状下_get_text('<a href="">1</a>')将返回[<a href="">1</a>]
*
* @param String $str
* @return string $url
*/
function _get_text($str)
{
return $this->format_left.$str.$this->format_right;
}

 

 

/**
   * 获取链接地址
*/
function _get_link($url,$text,$style=''){
$style=(empty($style))?'':'class="'.$style.'"';
if($this->is_ajax){
      //借使是应用AJAX方式
   return '<a '.$style.' href="javascript:'.$this->ajax_action_name.'(''.$url.'')">'.$text.'</a>';
}else{
   return '<a '.$style.' href="'.$url.'">'.$text.'</a>';
}
}
/**
   * 出错管理形式
*/
function error($function,$errormsg)
{
     die('Error in file <b>'.__FILE__.'</b> ,Function <b>'.$function.'()</b> :'.$errormsg);
}
}

本文由威尼斯在线注册平台发布于博客热点,转载请注明出处:【威尼斯正规官网】php+ajax分页代码

上一篇:文件上传原理,php常用文件上传类 下一篇:多态等等卡塔尔国高端OOP (使用 PHP,直至 PHP5.6 中加进的新特色
猜你喜欢
热门排行
精彩图文