The _php skill of PHP to realize the paging function of the imitation Google paging effect

Source: Internet
Author: User
Tags php programming trim

The example in this article describes the paging function of PHP to implement the Google page-splitting effect. Share to everyone for your reference. as follows:

/** * Page function * @param int $total Total pages * @param int $pagesize per page several * @param string $pkey the page variable name in the URL * @param string $ur L base URL, where {page} will be replaced with page number * Dependency function Request_uri httpint/function page ($total, $pagesize =10, $pkey = ' P ', $url =null) {$a _num
  = 10;
  $first = ' home ';
  $last = ' last ';
  $pre = ' Up page ';
  $next = ' next page ';
  $a _num= $a _num%2==0 $a _num+1: $a _num; if (! $url) {$url =preg_replace ("/&?{
    $pkey}=[^&]{0,}/",", Request_uri ()); $url =strpos ($url, '? ') ===false $url. '? '. $pkey. ' ={page} ': $url. ' & '. $pkey. '
  ={page} ';
  $pages =ceil ($total/$pagesize);
  $curpage =httpint ($pkey, false,1); $curpage = $curpage > $pages | |
  $curpage <=0?1: $curpage//The current page has a range of 1 $body = '; $prefix = '; $subfix = '; $start = $curpage-($a _num-1)/2;//start Page $end = $curpage + ($a _num-1)/2;  End page $start = $start <=0?1: $start; Start Page range Correction $end = $end > $pages? $pages: $end//End page >= correction if ($pages _num) {//Total pages greater than display pages if ($a <= ($curpage _num-1 )/2 {$end = $a _num;} The current page complements the left half of the right if ($end-$curpage <= ($a _num-1)/2) {$start-=5-($end-$curpage);
     The current page is on the right half of the left for ($i = $start; $i <= $end; $i + +) {if ($i = = $curpage) {$body. = ' <b> '. $i. ' </b> '; }else{$body. = ' <a href= '. Str_replace (' {page} ', $i, $url). ' "
     > '. $i. ' </a> '; }} $prefix = ($curpage ==1? ': ' <a href= '. Str_replace (' {page} ', 1, $url). ' " > '. $first. ' </a><a href= "'. Str_replace (' {page} ', $curpage-1, $url)." > '. $pre. '
  </a> '); $subfix = ($curpage = = $pages? ': ' <a href= '. Str_replace (' {page} ', $curpage +1, $url). ' " > '. $next. ' </a><a href= "'. Str_replace (' {page} ', $pages, $url)." > '. $last. '
  </a> ');
  $info = "{$curpage}/{$pages} page"; $go = ' <script>function ekup () {if (event.keycode==13) {clkyup ();}} function Clkyup () {if (!/\d+/.test (document.getElementById (\ ' gsd09fhas9d\ '). Value)) {alert (\ ' Enter page number!\ '); Location=\ '. $url. ' \ '. replace (/\\{page\\}/,document.getelementbyid (\ ' gsd09fhas9d\ '). Value); </script><input onkeyup= "Ekup ()" type= "text" id= "gsd09fhas9d" style= "Width:40px;vertical-align:text-baseline;padding:0 2px;font-size:10px;border:1px solid gray; " /> <span id= "Gsd09fhas9daa onclick=" Clkyup (); "style=" cursor:pointer;text-decoration:underline; "
  > Go to </span>;
Return $prefix. $body. $subfix. $info. $go;
    /** * Description: Gets _server[' Request_uri ' value of the generic solution/function Request_uri () {if (Isset ($_server[' Request_uri '])) {
  $uri = $_server[' Request_uri '];
    else {if (Isset ($_server[' argv ')) {$uri = $_server[' php_self ']. $_server[' argv '][0];
    else {$uri = $_server[' php_self ']. $_server[' query_string '];
} return $uri; /** * Get http Pass integer * @param string $key parameter name * @param bool $ispost take post delivery, the default is True http://blog.ddian.cn * @return integer, not
       The default is $default */function Httpint ($key, $ispost =true, $default =0) {if ($ispost) {if (Isset ($_post[$key))) {
       if (Empty ($_post[$key])) {return $default;
       else {$val = Trim ($_post[$key]);  if (Preg_match ("/^\-?\d+$/", $val)) return intval ($val);
       else return $default;
     } else {return $default;
       } else {if (Isset ($_get[$key])) {if (Empty ($_get[$key))) {return $default;
         else {$val = Trim ($_get[$key]);
         if (Preg_match ("/^\-?\d+$/", $val)) {return intval ($val);
         else {return $default;
     }} else {return $default; }
   }
}

I hope this article will help you with your PHP programming.

Related Article

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.