Two Universal PHP page-type _php skills

Source: Internet
Author: User

This article for everyone to share a super easy, versatile PHP page class, the specific implementation code as follows

The first type of PHP page-class

<?php * * To change this template, choose Tools |
 Templates * and open the template in the editor.
 * */** * page type * Use mode: * $page = new page ();
 * $page->init (1000, 20);
 * $page->setnotactivetemplate (' <span> {a} </span> ');
 * $page->setactivetemplate (' {a} ');
 * Echo $page->show ();
  * * * * @author the place where the wind resides * */class Page {/** * Total number of articles * * Private $total;
  /** * per page size * * Private $pageSize;
  /** * Total number of pages * * Private $pageNum;
  /** * Current Page * * Private $page;
  /** * address/private $uri;
  /** * Page Variable * * Private $pageParam;
  /** * LIMIT xx,xx * * Private $limit;
  /** * Digital Paging display * * Private $listnum = 8; /** * Page display template * Available variable parameter * {total} totals data bar * {pagesize} per page display number * {start} This page starts with * {end} This page ends the number of bars * {    Pagenum} total number of pages * {frist} Home * {pre} prev * {next} next * {last} end * {list} digital pagination * {goto} Jump button */private $template = ' <div><span> altogether {total} data </span><span> per page display {pagesize} data </span>,<span> this page {start}-{end} piece of data </span><span> Total {pagenum} page </span><ul>{frist}{pre}{list}{next}{last}{goto}</ul>
  </div> '; /** * Currently selected paging link template/Private $activeTemplate = ' <li class= ' active ' ><a href= ' javascript:;
  >{text}</a></li> ';
  /** * Unselected Paging link template/Private $notActiveTemplate = ' <li><a href= ' {URL} ' >{text}</a></li> '; /** * Display text settings * * Private $config = Array (' frist ' => ' home ', ' pre ' => ' prev ', ' Next ' => ' Next ', ' last ' => ')
  Last "); /** * Initialization * @param type $total total number of bars * @param type $pageSize per page size * @param type $param URL additional parameter * @para M type $pageParam Paging variable */Public function init ($total, $pageSize, $param = ', $pageParam = ' page ') {$this->
    Total = Intval ($total);
    $this->pagesize = intval ($pageSize);
    $this->pageparam = $pageParam; $this->uri = $thIs->geturi ($param);
    $this->pagenum = ceil ($this->total/$this->pagesize);
    $this->page = $this->setpage ();
  $this->limit = $this->setlimit (); /** * Set the paging template * @param type $template template configuration/Public function SetTemplate ($template) {$this->te
  Mplate = $template; /** * Settings Select the paging template * @param type $activeTemplate template configuration/Public function setactivetemplate ($activeTemplat
  e) {$this->activetemplate = $activeTemplate; /** * Settings No paging template * @param type $notActiveTemplate template configuration/Public function setnotactivetemplate ($notActive
  Template) {$this->notactivetemplate = $notActiveTemplate;
      /** * returns pagination * @return type */Public function Show () {return Str_ireplace ("{Total}",
      ' {pagesize} ', ' {start} ', ' {end} ', ' {pagenum} ', ' {frist} ', ' {pre} ', ' {next} ', ' {last} ', ' {list} ', ' {goto} ',), Array ($thiS->total, $this->setpagesize (), $this->star (), $this->end (), $this->pagenum,
      $this->frist (), $this->prev (), $this->next (), $this->last (), $this->pagelist (),
  $this->gopage (),), $this->template); /** * Get limit starting number * @return Type/Public function GetOffset () {return ($this->page-1) * $thi
  s->pagesize; /** * Set Limit * @return Type/Private Function Setlimit () {return "limit". ($this->page-1) * $this->pagesize.
  ", {$this->pagesize}"; /** * Get limit * @param type $args * @return type/Public function __get ($args) {if ($args = = "L
    Imit ") {return $this->limit;
    else {return null; }/** * Initializes the current page * @return Int/private Function setpage () {if (!empty ($_get[$this->pageparam)) {if ($_get[$this->pageparam] > 0) {if ($_get[$)This->pageparam] > $this->pagenum) return $this->pagenum;
      else return $_get[$this->pageparam];
  } return 1; /** * Initialize URL * @param type $param * @return string/Private Function GetURI ($param) {$url = $_s erver[' Request_uri ']. (Strpos ($_server[' Request_uri '], "?")? "" : "?") .
    $param;
    $parse = Parse_url ($url);
      if (Isset ($parse ["Query"])) {parse_str ($parse ["Query"], $params);
      unset ($params ["page"]); $url = $parse ["Path"]. "?" .
      Http_build_query ($params);
    return $url;
    else {return $url; }/** * Page start number * @return Int/private Function star () {if ($this->total = = 0) {return
    0;
    else {return ($this->page-1) * $this->pagesize + 1; }/** * Page end number * @return type/Private Function end () {return min ($this->page * $this->pag
  Esize, $this->total); /** * Set Current page largeSmall * @return Type */Private function setpagesize () {return $this->end ()-$this->star () + 1;
    /** * Home * @return type */Private Function frist () {$html = '; if ($this->page = = 1) {$html. = $this->replace ("{$this->uri}&page=1", $this->config[' frist '), true)
    ;
    else {$html. = $this->replace ("{$this->uri}&page=1", $this->config[' frist '), false);
  return $html;
    /** * Previous Page * @return type */Private Function prev () {$html = '; if ($this->page > 1) {$html. = $this->replace ($this->uri. ') &page= '.
    ($this->page-1), $this->config[' pre '], false); else {$html. = $this->replace ($this->uri. ') &page= '.
    ($this->page-1), $this->config[' pre '], true);
  return $html;
    /** * Pagination Number list * @return Type/Private Function pagelist () {$linkpage = "";
$lastlist = Floor ($this->LISTNUM/2);    for ($i = $lastlist; $i >= 1; $i-) {$page = $this->page-$i;
      if ($page >= 1) {$linkpage. = $this->replace ("{$this->uri}&page={$page}", $page, false);
      else {continue;
    $linkpage. = $this->replace ("{$this->uri}&page={$this->page}", $this->page, True);
      for ($i = 1; $i <= $lastlist; $i + +) {$page = $this->page + $i; if ($page <= $this->pagenum) {$linkpage. = $this->replace ("{$this->uri}&page={$page}", $page, FAL
      SE);
      } else {break;
  } return $linkpage;
    /** * Next Page * @return type */Private function Next () {$html = '; if ($this->page < $this->pagenum) {$html. = $this->replace ($this->uri. ') &page= '.
    ($this->page + 1), $this->config[' Next '], false); else {$html. = $this->replace ($this->uri. ') &page= '. ($this->page + 1), $this->config[' next '], true);
  return $html;
    /** * Final page * @return type/Private Function last () {$html = '; if ($this->page = = $this->pagenum) {$html. = $this->replace ($this->uri. ') &page= '.
    ($this->pagenum), $this->config[' last '], true); else {$html. = $this->replace ($this->uri. ') &page= '.
    ($this->pagenum), $this->config[' last '], false);
  return $html;
    /** * Jump button * @return string/Private Function Gopage () {$html = '; $html. = '  <input type= ' text "value=". $this->page. ' Onkeydown= ' Javascript:if (event.keycode==13) {var page= (this.value> '. $this->pagenum. ')?' . $this->pagenum. ': This.value;location=\ '. $this->uri. ' &page=\ ' +page+\ '} ' style= ' width:25px; /><input type= "button" onclick= "Javascript:var page= (this.previoussibling.value> '. $this->pagenum. ')?' . $this->pagenum. ': This.previoussibling.value;location=\ '. $this->uri.
    ' &page=\ ' +page+\ ', ' value= ' go '/> ';
  return $html; /** * Template Replacement * @param type $replace Replace content * @param type $result condition * @return Type/private funct Ion Replace ($url, $text, $result = True) {$template = ($result? $this->activetemplate: $this->notactivetempla
     
     TE);
     $html = Str_replace (' {URL} ', $url, $template);
    $html = Str_replace (' {text} ', $text, $html);
  return $html;
 }
}

The second section of the PHP page class

<?php * * * * This procedure file for the paging program is encapsulated */class Page_link {var $page _max = 10;//The maximum number of page numbers var $page _num = 10;//Total pages va R $length = 20;
  The number of data bars on a page var $isNextPage = true;

  var $isFirstPage = false;
    function Calculation_page_num ($total) {$this->page_num = ceil ($total/$this->length);
  return $this->page_num;
    The function Calculation_min_max ($act _page = 1) {//define the left and right offset $py _left = 0;
    $py _right = 0;
    Defines the left and right boundaries $bj _left = 0;
    $BJ _right = 0;
    Defines the rolling interval boundary $gd _left = 0;
    $GD _right = 0;
      Determine if you want to group if (($this->page_num-$this->page_max) <= 0) {//No need to group $bj _left = 1;
    $BJ _right = $this->page_num;
      else {//parity $tmp = $this->page_max% 2 for grouping//judging capacity;
      if ($tmp = = 1) {//odd $py _left = $py _right = ($this->page_max-1)/2;
 else {//even $py _left = $this->PAGE_MAX/2-1;       $py _right = $this->page_max/2;
      ///Calculate rolling interval $GD _left = 1 + $py _left;
      $GD _right = $this->page_num-$py _right;
        Determines whether the current page falls into the rolling interval if ($act _page >= $gd _left && $act _page <= $gd _right) {//Range
        $BJ _left = $act _page-$py _left;
      $BJ _right = $act _page + $py _right;
          else {//Interval external if (($act _page-$py _left) <= 1) {//left fixed interval
          $BJ _left = 1;
        $BJ _right = $this->page_max;
          else {$BJ _left = $this->page_num-$this->page_max + 1;
        $BJ _right = $this->page_num;
    }} $res = Array ();
    $res [' min '] = $BJ _left;

    $res [' max '] = $BJ _right;
    
  return $res; }//Main method function Make_page ($total, $act _page, $url, $param) {$page _num = $this->calculation_page_num ($to
    TAL);
$arr _min_max = $this->calculation_min_max ($act _page);    
    if (!eregi) ([? | =] $param =) ", $url)) {$url = Strpos ($url,"? ") ===false $url. "?": $url. "
      & "; $url = $url. "
    $param = 0 ";
    } if ($act _page > $page _num) {$act _page = $page _num; Change the URL to a regular $url = Eregi_replace ($param. ' =[0-9]+ ', $param.

    ' =0 ', $url);
    $res = Array ();
    $d = 0; for ($i = $arr _min_max[' min ']; $i <= $arr _min_max[' Max '; $i + +) {if ($i = = $act _page) {$res [
        $d] [' url '] = ';
        $res [$d] [' name '] = $i;
      $res [$d] [' no '] = $i; else {$res [$d] [' url '] = Str_replace ($param. ' =0 ', $param. '=' .
        $i, $url);
        $res [$d] [' name '] = $i;
      $res [$d] [' no '] = $i;
    } $d + +;
    } if ($this->isnextpage) {$res = $this->make_before_next_link ($res, $act _page, $url, $param);
    } if ($this->isfirstpage) {$res = $this->make_first_end_link ($res, $act _page, $url, $param); } return $res; ////with total page function make_page_with_total ($total, $act _page, $url, $param) {$page _num = $this->calculation
    _page_num ($total);
    
    $arr _min_max = $this->calculation_min_max ($act _page); if (!eregi) ([? | =] $param =) ", $url)) {$url = Strpos ($url,"? ") ===false $url. "?": $url. "
      & "; $url = $url. "
    $param = 0 ";
    } if ($act _page > $page _num) {$act _page = $page _num; Change the URL to a regular $url = Eregi_replace ($param. ' =[0-9]+ ', $param.

    ' =0 ', $url);
    $res = Array ();
    $d = 0; for ($i = $arr _min_max[' min ']; $i <= $arr _min_max[' Max '; $i + +) {if ($i = = $act _page) {$res [
        $d] [' url '] = ';
        $res [$d] [' name '] = $i;
      $res [$d] [' no '] = $i; else {$res [$d] [' url '] = Str_replace ($param. ' =0 ', $param. '=' .
        $i, $url);
        $res [$d] [' name '] = $i;
      $res [$d] [' no '] = $i;
    } $d + +;
} if ($this->isnextpage) {      $res = $this->make_before_next_link ($res, $act _page, $url, $param);
    } if ($this->isfirstpage) {$res = $this->make_first_end_link ($res, $act _page, $url, $param);
    $total _num= ceil ($total/$this->length);
    $result [' Total ']= $total _num;
    $result [' DATA ']= $res;
  return $result;

    //Append previous page and next page function Make_before_next_link ($arr, $act, $url, $param) {$tmp = array ();
    $before = $act-1;

    $next = $act + 1;
      if ($before < 1) {$before = 1;
      $tmp [0][' url '] = ';
      $tmp [0][' name '] = "Previous page";
    $tmp [0][' no '] = $before; else {$tmp [0][' url '] = Str_replace ($param. ' =0 ', $param. '=' .
      $before, $url);
      $tmp [0][' name '] = "Previous page";
    $tmp [0][' no '] = $before;
    } $counts = sizeof ($arr);
    $tmp _count = sizeof ($TMP);
      for ($i = 0; $i < $counts; $i + +) {$tmp [$tmp _count][' url '] = $arr [$i] [' url ']; $tmp [$tmp _count][' name '] = $arr[$i]
      [' Name '];
      $tmp [$tmp _count][' no '] = $arr [$i] [' no '];
    $tmp _count++;
      } if ($next > $this->page_num) {$next = $this->page_num;
      $tmp [$tmp _count][' url '] = ';
      $tmp [$tmp _count][' name '] = "Next page";
    $tmp [$tmp _count][' no '] = $next; else {$tmp [$tmp _count][' url '] = Str_replace ($param. ' =0 ', $param. '=' .
      $next, $url);
      $tmp [$tmp _count][' name '] = "Next page";
    $tmp [$tmp _count][' no '] = $next;
  return $tmp;

    //Append home and Last function Make_first_end_link ($arr, $act, $url, $param) {$tmp = array ();
    $before = 1;

    $next = $this->page_num;
      if ($act = = 1) {$before = 1;
      $tmp [0][' url '] = ';
      $tmp [0][' name '] = "Home page";
    $tmp [0][' no '] = $before; else {$tmp [0][' url '] = Str_replace ($param. ' =0 ', $param. '=' .
      $before, $url);
      $tmp [0][' name '] = "Home page";
    $tmp [0][' no '] = $before;
    } $counts = sizeof ($arr); $tmp _count = sizeof ($TMP);
      for ($i = 0; $i < $counts; $i + +) {$tmp [$tmp _count][' url '] = $arr [$i] [' url '];
      $tmp [$tmp _count][' name '] = $arr [$i] [' name '];
      $tmp [$tmp _count][' no '] = $arr [$i] [' no '];
    $tmp _count++;
      } if ($act = = $this->page_num) {$tmp [$tmp _count][' url '] = ';
      $tmp [$tmp _count][' name '] = "last";
    $tmp [$tmp _count][' no '] = $next; else {$tmp [$tmp _count][' url '] = Str_replace ($param. ' =0 ', $param. '=' .
      $next, $url);
      $tmp [$tmp _count][' name '] = "last";
    $tmp [$tmp _count][' no '] = $next;
  return $tmp; /** * Take previous page, next page, ellipsis page * @param int $total record total number of bars * @param int $act _page Current page number * @param    String $url URL * @param int $maxpageicon Maximum number of page numbers * @param int $style Previous page, next page shows style * @param string $param URL parameter */function make_page_with_points ($total, $act _page, $url, $maxpageicon, $style, $param) {$page _num = $th Is->calculation_page_nUm ($total);    Total number of pages $arr _min_max = $this->calculation_min_max ($act _page);

    Maximum page, min page if ($total ==0) {return "";
      } if ($act _page > $page _num) {$act _page = $page _num+1;
    $page _num = $page _num+1;
        Switch ($style) {Case 1: $name _before = ' previous page ';
        $name _next = ' next page ';
      Break
        Case 2: $name _before = ' < ';
        $name _next = ' > ';
      Break
        Case 3: $name _before = ' << ';
        $name _next = ' >> ';
      Break
        Default: $name _before = ' previous page ';
    $name _next = ' next page '; if (!eregi) ([? | =] $param =) ", $url)) {$url = Strpos ($url,"? ") ===false $url. "?": $url. "
      & "; $url = $url. "
    $param = 0 "; Change the URL to a regular $url = Eregi_replace ($param. ' =[0-9]+ ', $param.
    ' =0 ', $url);
    $res = Array ();
    $no _before = $act _page-1;
    
    $no _next = $act _page+1; Total pages if less than or equal to initialize maximum rendered page if ($page_num<= ($maxpageicon + 1)) {//If the current number of pages is first previous page is invalid if ($act _page = = 1) {$res [0][' URL ']
        = '';
        $res [0][' name '] = $name _before;
      $res [0][' no '] = $no _before; else//Previous page is valid {$res [0][' url '] = Str_replace ($param. ' =0 ', $param. '=' .
        ($act _page-1), $url);
        $res [0][' name '] = $name _before; 
      $res [0][' no '] = $no _before;
      //Loop Add page number $d = 1; for ($i = 1; $i <= $page _num $i + +) {if ($i!= $act _page) {$res [$d] [' url '] = Str_repl Ace ($param. ' =0 ', $param. '=' .
          $i, $url);
          $res [$d] [' name '] = $i;
        $res [$d] [' no '] = $i;
          else//Current page, page number {$res [$d] [' url '] = ';
          $res [$d] [' name '] = $i;
          $res [$d] [' no '] = $i;
        $res [$d] [' attr '] = ' current ';
      } $d + +;
      $last _d = count ($res);
       Judgment last if ($act _page = = $page _num)//Next page is invalid { $res [$last _d][' url '] = ';
        $res [$last _d][' name '] = $name _next;    
      $res [$last _d][' no '] = $no _next; else {$res [$last _d][' url '] = Str_replace ($param. ' =0 ', $param. '=' .
        ($act _page + 1), $url);
        $res [$last _d][' name '] = $name _next;
      $res [$last _d][' no '] = $no _next; }else if ($page _num > ($maxpageicon + 1))//If the total number of pages satisfies the add ellipsis {if ($act _page <= $maxpageicon)//If the current page is less than the
          To initialize the number of {//if the current page is first previous invalid if ($act _page = 1) {$res [0][' url '] = ';
          $res [0][' name '] = $name _before;
        $res [0][' no '] = $no _before; else//Previous page is valid {$res [0][' url '] = Str_replace ($param. ' =0 ', $param. '=' .
          ($act _page-1), $url);
          $res [0][' name '] = $name _before; 
        $res [0][' no '] = $no _before;
        //Loop Add page number $d = 1; for ($i = 1; $i <= $maxpageicon; $i + +) {if ($i!= $act _page)
          {$res [$d] [' url '] = Str_replace ($param. ' =0 ', $param. '=' .
            $i, $url);
            $res [$d] [' name '] = $i;
          $res [$d] [' no '] = $i;
            else//Current page, page number {$res [$d] [' url '] = ';
            $res [$d] [' name '] = $i;
            $res [$d] [' no '] = $i;
          $res [$d] [' attr '] = ' current ';
        } $d + +;
        $last _d = count ($res);
        Add ellipsis $res [$last _d][' url '] = ';
        $res [$last _d][' name '] = ' ... ';
        $res [$last _d][' no '] = '; Total pages $res [$last _d+1][' url '] = Str_replace ($param. ' =0 ', $param. '=' .
        $page _num, $url);
        $res [$last _d+1][' name '] = $page _num;
        $res [$last _d+1][' no '] = $page _num; The next page $res [$last _d+1][' url '] = Str_replace ($param. ' =0 ', $param. '=' .
        ($act _page + 1), $url);
        $res [$last _d+1][' name '] = $name _next;     
      $res [$last _d+1][' no '] = $no _next;
        }else//If the current page is greater than the maximum display page {if ($act _page > ($page _num-$maxpageicon))//Meet the next few pages {//prev $res [0][' url '] = Str_replace ($p Aram. ' =0 ', $param. '=' .
          ($act _page-1), $url);
          $res [0][' name '] = $name _before;
          $res [0][' no '] = $no _before; The first page $res [1][' url '] = Str_replace ($param. ' =0 ', $param.
          ' =1 ', $url);
          $res [1][' name '] = 1;  
          $res [1][' no '] = 1;
          Ellipsis $res [2][' url '] = ';
          $res [2][' name '] = ' ... '; 
          $res [2][' no '] = ';
          $d = 3;
            for ($i = ($page _num-$maxpageicon + 1); $i <= $page _num $i + +) {if ($i!= $act _page) {$res [$d] [' url '] = Str_replace ($param. ' =0 ', $param. '=' .
              $i, $url);
              $res [$d] [' name '] = $i;
            $res [$d] [' no '] = $i;
              else//Current page, page number {$res [$d] [' url '] = ';
              $res [$d] [' name '] = $i;
$res [$d] [' no '] = $i;              $res [$d] [' attr '] = ' current ';
          } $d + +;
          $last _d = count ($res);
            Judgment last if ($act _page = = $page _num)//Next page is invalid {$res [$last _d][' url '] = ';
            $res [$last _d][' name '] = $name _next;    
          $res [$last _d][' no '] = $no _next; else {$res [$last _d][' url '] = Str_replace ($param. ' =0 ', $param. '=' .
            ($act _page + 1), $url);
            $res [$last _d][' name '] = $name _next;
          $res [$last _d][' no '] = $no _next; }else//satisfied in the middle {//previous page $res [0][' url '] = Str_replace ($param. ' =0 ', $param. '=' .
          ($act _page-1), $url);
          $res [0][' name '] = $name _before;
          $res [0][' no '] = $no _before; The first page $res [1][' url '] = Str_replace ($param. ' =0 ', $param.
          ' =1 ', $url);
          $res [1][' name '] = 1;  
          $res [1][' no '] = 1; Ellipsis $res [2][' url '] ='';
          $res [2][' name '] = ' ... '; 
          $res [2][' no '] = ';
            for ($i = ($act _page-($maxpageicon-2)/2) $i <= floor ($act _page+ ($maxpageicon-2)/2); $i + +) {
            $i = Ceil ($i); if ($i!= $act _page) {$res [$d] [' url '] = Str_replace ($param. ' =0 ', $param. '=' .
              $i, $url);
              $res [$d] [' name '] = $i;
            $res [$d] [' no '] = $i;
              else//Current page, page number {$res [$d] [' url '] = ';
              $res [$d] [' name '] = $i;
              $res [$d] [' no '] = $i;
            $res [$d] [' attr '] = ' current ';
          } $d + +;
          $last _d = count ($res);
          Add ellipsis $res [$last _d][' url '] = ';
          $res [$last _d][' name '] = ' ... ';
          $res [$last _d][' no '] = '; The current page $res [$last _d+1][' url '] = Str_replace ($param. ' =0 ', $param. '=' .
          $page _num, $url); $res [$last _d+1][' name '] = $page _nUm    
          $res [$last _d+1][' no '] = $page _num; The next page $res [$last _d+2][' url '] = Str_replace ($param. ' =0 ', $param. '=' .
          ($act _page + 1), $url);
          $res [$last _d+2][' name '] = $name _next;
          $res [$last _d+2][' no '] = $no _next;  
         Exit
  }} return $res;
 }}?>

The above is for you to share the two sections of the PHP page class, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.