PHP 完全なページング クラス (ソース コード付き)_PHP チュートリアル

WBOY
リリース: 2016-07-13 16:57:03
オリジナル
1051 人が閲覧しました

ASP よりも PHP でページングを実装する方がはるかに簡単です。私たちの中心は、現在のページを直接取得し、各ページのサイズを決定し、データベースで制限を使用してページング クエリを実装することです。クラス実装プログラムの詳細。


ajax 呼び出しの場合:

//$total、合計数 (int); $size、ページごとに表示される数 (int)、$url、リンク (文字列);

$page = new Page(array('total'=>$total,'perpage'=>$size,'nowindex'=>$page,'url' => $url,'ajax' => ; 'ビデオGoToPage'));

//変数 $page_html はページング HTML、パラメータ 4 は 4 番目のページング表示スタイルです

$page_html = $page->show(4);
//次に、jQuery パッケージと js コードをページに追加します:

関数 videoGoToPage(u)

{
もし(!u)
{
false を返します;
}
$.ajax({
タイプ: 「POST」、
URL:「」+あなた
データ:「」、
成功: function(msg){
//alert( “保存されたデータ: ” + msg );
$(“#tanglei”).html(msg);
}
});
}

ajax 呼び出しではない場合:

//配列内の「ajax」項目を削除するだけです

$page = 新しいページ(array('total'=>$total,'perpage'=>$size,'nowindex'=>$page,'url' => $url));

$page_html = $page->show(4);

注: URL については、擬似静的を使用しているため、たとえば、私のページのリンクは、最初のページを意味する search-1.html、2 番目のページを意味する search-2.html、そして $url 変数です

$url = 'search-';

として記述する必要があります

ページング クラスは、次の「ページ数.html」を自動的に完成させます。ここで、必要に応じてページング クラスを変更できます。

page.class.php を以下のみんなと共有します

コードは次のとおりですコードをコピー
/**
* ファイル名: ext_page.class.php
* @package:phpbean
* @author :feifengxlq
* @copyright :Copyright 2006 feifengxlq
* @license:バージョン 2.0
* @create:2006-5-31
* @modify:2006-6-1
* @modify:feifengxlq 2006-11-4
* 説明: 超強力なページング クラス、4 つのページング モード、デフォルトのページング スタイルは Baidu や Google に似ています。
* 2.0 で追加された機能: カスタム スタイル、カスタム スタイルのサポート、PHP4 と PHP5 の両方のサポート
* 詳細を確認するには、http://www.bKjia.c0m をご覧ください。 * 例:
* 4 つのページング モード:
require_once('../libs/classes/page.class.php');
$page=新しいページ(array('total'=>1000,'perpage'=>20));
echo 'mode:1
'.$page->show();
echo '
mode:2
'.$page->show(2);
echo '
mode:3
'.$page->show(3);
echo '
mode:4
'.$page->show(4);
AJAX をオンにする:
$ajaxpage=新しいページ(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
echo 'mode:1
'.$ajaxpage->show();
継承されたカスタム ページング表示モードを使用します:
デモ:[url=http://www.phpobject.net/blog]http://www.phpobject.net/blog[/url]
​*/
クラスページ
{
/**
  * 設定、パブリック
 */
var $page_name="p";//URL ページを制御するために使用されるページ タグ。たとえば、xxx.php?PB_page=2 の PB_page
var $next_page='>';//次のページ
var $pre_page='<';//前のページ
var $first_page='最初';//ホームページ
var $last_page='Last';//最後のページ
var $pre_bar='<<';//前のページングバー
var $next_bar='>>';//次のページングバー
var $format_left='';
var $format_right='';
var $is_ajax=false;//AJAX ページング モードがサポートされているかどうか
/**

  *プライベート
  *
 */
var $pagebarnum=10;//レコードバーの数を制御します。
var $totalpage=0;//総ページ数
var $ajax_action_name='';//AJAX アクション名
var $nowindex=1;//現在のページ
var $url="";// URL アドレスヘッダー
var $offset=0;

/**
  * コンストラクター构造関数数
  *
  * @param 配列 $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']...
 */
 関数ページ($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']:'';
  }その他{
     $total=$array;
     $perpage=10;
     $nowindex='';
     $url='';
  }
  //if((!is_int($total))||($totalerror(__FUNCTION__,$total.' は正の整数ではありません!');
  if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' は正の整数ではありません!');
  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模式
 }
 /**
※指定した変数名の値をクラスに設定します。変更がこのクラスに属さない場合は例外がスローされます
*
* @param 文字列 $var
* @param string $value
​*/
 関数セット($var,$value)
 {
  if(in_array($var,get_object_vars($this)))
     $this->$var=$value;
  他に{
   $this->error(__FUNCTION__,$var." は PB_Page に属していません!");
  }

}
 /**
* 逆 AJAX モードをオンにする
*
* @param string $action デフォルトの Ajax トリガー アクション。
​*/
 関数open_ajax($action)
 {
  $this->is_ajax=true;
  $this->ajax_action_name=$action;
 }
 /**
*「次のページ」を表示するコードを取得します
*
* @param string $style
* @戻り文字列
​*/
 関数 next_page($style='')
 {
  if($this->nowindextotalpage){
   return $this->_get_link($this->_get_url($this->>nowindex+1),$this->next_page,$style);
  }
  return ''.$this->next_page.'';
 }

/**
*「前ページ」を表示するコードを取得します
*
* @param string $style
* @戻り文字列
​*/
 関数 pre_page($style='')
 {
  if($this->nowindex>1){
   return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
  }
  return ''.$this->pre_page.'';
 }

/**
*「ホームページ」を表示するコードを取得します
*
* @戻り文字列
​*/
 関数 first_page($style='')
 {
  if($this->nowindex==1){
      return ''.$this->first_page.'';
  }
  return $this->_get_link($this->_get_url(1),$this->first_page,$style);
 }

/**
*「最後のページ」を表示するコードを取得します
*
* @戻り文字列
​*/
 関数 last_page($style='')
 {
  if($this->nowindex==$this->totalpage){
      return ''.$this->last_page.'';
  }
  return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
 }

 function nowbar($style='',$nowindex_style='c')
 {
  $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(''.$i.'');
   }else{
    break;
   }
   $return.="n";
  }
  unset($begin);
  return $return;
 }
 /**
  * 获取显示跳转按钮的代码
  *
  * @return string
  */
 function select()
 {
   $return='';
  return $return;
 }

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

 /**
  * 控制分页显示风格(你可以增加相应的风格)
  *
  * @param int $mode
  * @return string
  */
 function show($mode=1)
 {
  switch ($mode)
  {
   case '1':
    $this->next_page='下一页';
    $this->pre_page='上一页';
    return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
    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().'页';
    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='<';
    $this->first_page='<<';
    $this->last_page='>>';
    return $this->first_page().$this->pre_page().$this->nowbar().$this->next_page().$this->last_page();
    break;
   case '5':
    return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
    break;
  }

}
/*-----private function (私有方法)--------------- ---------------------------------*/
 /**
* URLヘッダーアドレスを設定します
* @param: 文字列 $url
* @return boolean
​*/
 関数_set_url($url="")
 {
  if(!empty($url)){
      //手動設置
   $this->url=$url;
  }その他{
      //自動获取
   if(空($_SERVER['QUERY_STRING'])){
       //QUERY_STRING が存在しないとき
    $this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."=";
   }その他{
       //
    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."=";
     }その他{
         $this->url.='&'.$this->page_name."=";
     }
    }その他{
        //
     $this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
    }//終了 if
   }//終了 if
  }//終了 if
 }

/**
* 現在のページを設定します
*
​*/
 関数_set_nowindex($nowindex)
 {
  if(空($nowindex)){
   //系统获取

if(isset($_GET[$this->page_name])){
    $this->nowindex=intval($_GET[$this->page_name]);
   }
  }その他{
      //手動設置
   $this->nowindex=intval($nowindex);
  }
 }

/**
* 指定されたページのアドレス値を返します
*
* @param int $pageno
* @return string $url
​*/
 関数_get_url($pageno=1)
 {
  $this->url.$pageno を返します。 '.html';
 }

/**
* ページネーションの表示テキストを取得します。たとえば、デフォルトでは、_get_text('1') は [1] を返します
*
* @param 文字列 $str
* @return string $url
​*/
 関数_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モードを使用します
   ''.$text.'';
  }その他{
   ''.$text.'';
  }
 }
 /**
※エラー処理方法
​*/
 関数エラー($function,$errormsg)
 {
     die('ファイル '.__FILE__.' ,関数 '.$function.'() :'.$errormsg);
 }
}
?>

php分页类源码下劣包:http://file.bKjia.c0m/download/2013/06/08/pageClass.rar

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/631555.html技術記事 php中要实现分页比起asp中要简单很多了,我们核心就是直接获取当前页面その後判断每页多少再到データ库中利用制限可能实现分页查询了,...
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!