ホームページ  >  記事  >  バックエンド開発  >  シンプルで使いやすい PHP ページング class_PHP チュートリアル

シンプルで使いやすい PHP ページング class_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 10:25:44766ブラウズ

コードをコピーします コードは次のとおりです:

class Page {
    // 分页栏每页显示的页数
    public $rollPage = 6;
    // 页数跳转时要带的参数
    public $parameter  ;
    // 默认列表每页显示行数
    public $listRows = 20;
    // 起始行数
    public $firstRow ;
    // 分页总页面数
    protected $totalPages  ;
    // 总行数
    protected $totalRows  ;
    // 当前页数
    protected $nowPage    ;
    // 分页的栏的总页数
    protected $coolPages   ;
    // 分页显示定制
    protected $config  = array(
     'redirect'=>false,
     'header'=>'条记录',
     'prev'=>'上一页',
     'next'=>'下一页',
     'first'=>'1',
     'last'=>'最后一页',
     'theme'=>'
%upPage% %first%  %prePage%  %linkPage%  %nextPage% %downPage%
共  %totalPage% 页');
    // 默认分页变量名
    protected $varPage;

    /**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     +----------------------------------------------------------
     */
    public function __construct($totalRows,$listRows='',$parameter='') {
        $this->totalRows = $totalRows;
        $this->parameter = $parameter;
        $this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
        if(!empty($listRows)) {
            $this->listRows = intval($listRows);
        }
        $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
        $this->coolPages  = ceil($this->totalPages/$this->rollPage);
        //$_GET验证
        $this->nowPage = intval($_GET[$this->varPage]);
        $this->nowPage = $this->nowPage > 0 ? $this->nowPage : 1;

        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow = $this->listRows*($this->nowPage-1);
    }

    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name]    =   $value;
        }
    }

/**
+------------------------------------------------ ----------
* ページ単位で出力を表示する
--------------------
* @access public
+ ------------------ -------------------------------- --------
*/
public function show() {

if(0 == $this->totalRows) return '';

//パラメータの処理
$p = $this->varPage ;
$url = $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
$parse = parse_url( $ url); '?'。http_build_query($ params); start = 1 $ $ loll page;
; nowPage -$ $> ) {
$ thefirst = "& lt; a class = ' firstPage' href='".$url."&".$p."=1' >".$this->config['first'] ."";
.$p. $islast){
$theEnd = "".$this-> config['last']. ""; &".$p."=$upRow'>".$this->config ['prev']."";
}

if ($downRow <= $this->totalPages){
$downPage = "".$this->config['next']."";
}

if($start==3){
$linkPage .= "2";
}
if($start>=4){
$linkPage .= "2 ...";
}
//1 2 3 4 5
for($i=$start;$i<=$end;$i++){
if($i!=$this->nowPage){
$linkPage .= " ".$i." ";
}else{
$linkPage .= " ".$i."";
}
if($i==$end){
if($i<$this->totalRows){
$linkPage .= " ...";
}
}
}

$pageStr = str_replace(
array('%header%','%nowPage%', '%totalRow%', '%totalPage%','%upPage%','%first%','%prePage%','%linkPage%','%nextPage%','%downPage%','%end%'),
array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$theFirst,$prePage,$linkPage,$nextPage, $downPage,$theEnd),$this->config['テーマ']);

//显示模式 普通 false 带跳转ture
if (!empty($this->config['redirect'])){
$html = $pageStr;

}else{
//传递パラメータ
if ($this->totalPages > 1){
$redirect = " 到第

";
if ($params){
foreach($params as $ k => $v){
$string .= "";
}
$redirect = $redirect 。 $string 。 '
';
}else{
$redirect = $redirect . '
';
}
}
// Html文字文字列生成
$html = $pageStr . $リダイレクト;      
}
return $html;
}

}

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/824939.html技術記事次のようにコードをコピーします。 class Page { // ページング バーの各ページに表示されるページ数 public $rollPage = 6 // ページ番号にジャンプするときに取得されるパラメーター public $parameter;各ページの行数を表示します。 .
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:PHP_PHP チュートリアルのパフォーマンス調整次の記事:PHP_PHP チュートリアルのパフォーマンス調整

関連記事

続きを見る