Heim > php教程 > PHP源码 > Hauptteil

AJAX模式下的分页

PHP中文网
Freigeben: 2016-05-25 17:15:20
Original
959 Leute haben es durchsucht

AJAX模式下的分页

<?php
/**
演示
require_once(&#39;../libs/classes/page.class.php&#39;);
$page=new page(array(&#39;total&#39;=>1000,&#39;perpage&#39;=>20));
echo &#39;mode:1<br>&#39;.$page->show();
echo &#39;<hr>mode:2<br>&#39;.$page->show(2);
echo &#39;<hr>mode:3<br>&#39;.$page->show(3);
echo &#39;<hr>mode:4<br>&#39;.$page->show(4);
echo &#39;<hr>开始AJAX模式:&#39;;
$ajaxpage=new page(array(&#39;total&#39;=>1000,&#39;perpage&#39;=>20,&#39;ajax&#39;=>&#39;ajax_page&#39;,&#39;page_name&#39;=>&#39;test&#39;));
echo &#39;mode:1<br>&#39;.$ajaxpage->show();
*/
class minupage
{
/**
* config ,public
*/
var $page_name="p";//page标签,用来控制url页。比如说xxx.php?PB_page=2中的PB_page
var $next_page=&#39;>&#39;;//下一页
var $pre_page=&#39;<&#39;;//上一页
var $first_page=&#39;First&#39;;//首页
var $last_page=&#39;Last&#39;;//尾页
var $pre_bar=&#39;<<&#39;;//上一分页条
var $next_bar=&#39;>>&#39;;//下一分页条
var $format_left=&#39;[&#39;;
var $format_right=&#39;]&#39;;
var $is_ajax=false;//是否支持AJAX分页模式

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

/**
* constructor构造函数
*
* @param array $array[&#39;total&#39;],$array[&#39;perpage&#39;],$array[&#39;nowindex&#39;],$array[&#39;url&#39;],$array[&#39;ajax&#39;]...
*/
function minupage($array)
{
if(is_array($array)){
     if(!array_key_exists(&#39;total&#39;,$array))$this->error(__FUNCTION__,&#39;need a param of total&#39;);
     $total=intval($array[&#39;total&#39;]);
     $perpage=(array_key_exists(&#39;perpage&#39;,$array))?intval($array[&#39;perpage&#39;]):10;
     $nowindex=(array_key_exists(&#39;nowindex&#39;,$array))?intval($array[&#39;nowindex&#39;]):&#39;&#39;;
     $url=(array_key_exists(&#39;url&#39;,$array))?$array[&#39;url&#39;]:&#39;&#39;;
}else{
     $total=$array;
     $perpage=10;
     $nowindex=&#39;&#39;;
     $url=&#39;&#39;;
}
if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.&#39; is not a positive integer!&#39;);
if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.&#39; is not a positive integer!&#39;);
if(!empty($array[&#39;page_name&#39;]))$this->set(&#39;page_name&#39;,$array[&#39;page_name&#39;]);//设置pagename
$this->_set_nowindex($nowindex);//设置当前页
$this->_set_url($url);//设置链接地址
$this->totalpage=ceil($total/$perpage);
$this->offset=($this->nowindex-1)*$perpage;
if(!empty($array[&#39;ajax&#39;]))$this->open_ajax($array[&#39;ajax&#39;]);//打开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=&#39;&#39;)
{
if($this->nowindex<$this->totalpage){
   return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
}
return &#39;<span class="&#39;.$style.&#39;">&#39;.$this->next_page.&#39;</span>&#39;;
}

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

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

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

function nowbar($style=&#39;&#39;,$nowindex_style=&#39;&#39;)
{
$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=&#39;&#39;;
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(&#39;<span class="&#39;.$nowindex_style.&#39;">&#39;.$i.&#39;</span>&#39;);
   }else{
    break;
   }
   $return.="\n";
}
unset($begin);
return $return;
}
/**
* 获取显示跳转按钮的代码
*
* @return string
*/
function select($url)
{
$return=&#39;<select name=";PB_Page_Select" >&#39;;
for($i=1;$i<=$this->totalpage;$i++)
{
   if($i==$this->nowindex){
    $return.=&#39;<option value=&#39;.$url.$i.&#39; selected>&#39;.$i.&#39;</option>&#39;;
   }else{
    $return.=&#39;<option value=&#39;.$url.$i.&#39;>&#39;.$i.&#39;</option>&#39;;
   }
}
unset($i);
$return.=&#39;</select>&#39;;
return $return;
}

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

/**
* 控制分页显示风格(你可以增加相应的风格)
*
* @param int $mode
* @return string
*/
function show($mode=1,$url=&#39;&#39;)
{
switch ($mode)
{
   case &#39;1&#39;:
    $this->next_page=&#39;下一页&#39;;
    $this->pre_page=&#39;上一页&#39;;
    return $this->pre_page().$this->nowbar().$this->next_page().&#39;第&#39;.$this->select($url).&#39;页&#39;;
    break;
   case &#39;2&#39;:
    $this->next_page=&#39;下一页&#39;;
    $this->pre_page=&#39;上一页&#39;;
    $this->first_page=&#39;首页&#39;;
    $this->last_page=&#39;尾页&#39;;
    return $this->first_page().$this->pre_page().&#39;[第&#39;.$this->nowindex.&#39; 页]&#39;.
    $this->next_page().$this->last_page().&#39;第 &#39;.$this->select($url).&#39;页&#39;;
    break;
   case &#39;3&#39;:
    $this->next_page=&#39;下一页&#39;;
    $this->pre_page=&#39;上一页&#39;;
    $this->first_page=&#39;首页&#39;;
    $this->last_page=&#39;尾页&#39;;
    return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
    break;
   case &#39;4&#39;:
    $this->next_page=&#39;下一页&#39;;
    $this->pre_page=&#39;上一页&#39;;
    return $this->pre_page().$this->nowbar().$this->next_page();
    break;
   case &#39;5&#39;:
    return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
    break;
}

}
/*----------------private function (私有方法)-----------------------------------------------------------*/
/**
* 设置url头地址
* @param: String $url
* @return boolean
*/
function _set_url($url="")
{
if(!empty($url)){
      //手动设置
   $this->url=$url.((stristr($url,&#39;?&#39;))?&#39;&&#39;:&#39;?&#39;).$this->page_name."=";
}else{
      //自动获取
   if(empty($_SERVER[&#39;QUERY_STRING&#39;])){
       //不存在QUERY_STRING时
    $this->url=$_SERVER[&#39;REQUEST_URI&#39;]."?".$this->page_name."=";
   }else{
       //
    if(stristr($_SERVER[&#39;QUERY_STRING&#39;],$this->page_name.&#39;=&#39;)){
        //地址存在页面参数
     $this->url=str_replace($this->page_name.&#39;=&#39;.$this->nowindex,&#39;&#39;,$_SERVER[&#39;REQUEST_URI&#39;]);
     $last=$this->url[strlen($this->url)-1];
     if($last==&#39;?&#39;||$last==&#39;&&#39;){
         $this->url.=$this->page_name."=";
     }else{
         $this->url.=&#39;&&#39;.$this->page_name."=";
     }
    }else{
        //
     $this->url=$_SERVER[&#39;REQUEST_URI&#39;].&#39;&&#39;.$this->page_name.&#39;=&#39;;
    }//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(&#39;<a href="">1</a>&#39;)将返回[<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=&#39;&#39;){
$style=(empty($style))?&#39;&#39;:&#39;class="&#39;.$style.&#39;"&#39;;
if($this->is_ajax){
      //如果是使用AJAX模式
   return &#39;<a &#39;.$style.&#39; href="javascript:&#39;.$this->ajax_action_name.&#39;(\&#39;&#39;.$url.&#39;\&#39;)">&#39;.$text.&#39;</a>&#39;;
}else{
   return &#39;<a &#39;.$style.&#39; href="&#39;.$url.&#39;">&#39;.$text.&#39;</a>&#39;;
}
}
/**
   * 出错处理方式
*/
function error($function,$errormsg)
{
     die(&#39;Error in file <b>&#39;.__FILE__.&#39;</b> ,Function <b>&#39;.$function.&#39;()</b> :&#39;.$errormsg);
}
}
?>
Nach dem Login kopieren


                   

以上就是的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage