<?php

$arr_page = array(10, 30, 50, 100, 200);
if (isset($_GET['pn'])) {
  $_GET['pn'] = abs($_GET['pn']);
  if (in_array($_GET['pn'], $arr_page)) {
    $web['pagesize'] = $_GET['pn']; //分页记录数
  }
}
  
//页数
function get_page($n, $ps = false) {
  global $web, $arr_page;
  $ps = $ps ? $ps : $web['pagesize'];
  @ $_GET['p'] = abs($_GET['p']);
  if (!$_GET['p'] || $_GET['p'] < 1) {
    return 1;
  } elseif ($n && $_GET['p'] > ceil($n / $ps)) {
    return ceil($n / $ps);
  } else {
    return floor($_GET['p']);
  }
}

//分页
function get_page_foot($totallists, $pagesize, $p, $mpurl_before, $mpurl_after = '', $pagestep = 10, $target = '_self') {
  global $web, $arr_page;
  $text = '';
  if (!isset($GLOBALS['page_style'])) {
    $text .= '<span id="page_style"><style type="text/css">
<!--
/*页码*/
.page_table { padding-bottom:10px; }
.page { display:inline-block !important; display:inline; zoom:1; margin-top:10px; margin-bottom:10px; background-color:#FFFFFF; border:1px #DDDDDD solid; height:24px; padding:2px; border-radius:3px; -moz-border-radius:3px; -webkit-border-radius:3px; line-height:24px !important; font-size:12px; }
.page_table .page { margin-top:0; margin-bottom:0; }
.page .pagein, .pagein { display:inline-block !important; display:inline; zoom:1; line-height:24px !important; }
.rightmenu .page { margin-top:10px; margin-bottom:0; }
.page a, .page strong { display:inline-block !important; display:inline; zoom:1; margin-left:1px; margin-right:1px; padding-left:5px; padding-right:5px; cursor:pointer; }
.page a b { }
.page strong { color:#FF6600; background-color:#ECECEC; cursor:auto; }
.page a { color:#696969; text-decoration:none; background-color:#F4F4F4; }
.page a:hover { color:#FF6600; background-color:#ECECEC; }
.page .prev, .page .next { }
.page .totallists { font-weight:bold; width:auto; margin-right:2px; float:left; display:inline-block !important; display:inline; zoom:1; height:24px; padding:0 6px; background-color:#ECECEC; }
.page #pagego { padding:0; margin:0; margin-top:-3px; width:26px; height:20px; border:1px #CCCCCC inset; background:url(inc/images/enter.gif) 6px 6px no-repeat; display:inline-block !important; display:inline; zoom:1; }
.pnis { color:#FF6600; text-decoration:underline; font-weight:bold; }
.pagein a, .pagein b, .pagein span, .pagein strong { line-height:24px !important; font-size:12px;}
-->
</style></span>
';
    $GLOBALS['page_style'] = true;
  }

  $totalpages = 1;
  if ($totallists > $pagesize) {
    $text .= '<div class="page"><div class="pagein">';
    $offset = 4; //当前页前面显几页
    $totalpages = @ceil($totallists / $pagesize);
    if ($pagestep > $totalpages) {
      $fr = 1;
      $to = $totalpages;
    } else {
      $fr = $p - $offset;
      $to = $fr + $pagestep - 1;
      if ($fr < 1) {
        $to = $p + 1 - $fr;
        $fr = 1;
        if ($to - $fr < $pagestep) {
          $to = $pagestep;
        }
      } elseif ($to > $totalpages) {
        $fr = $totalpages - $pagestep + 1;
        $to = $totalpages;
      }
    }

    $text .= '<span class="totallists" title="总记录数" style="width:auto">'.$totallists.'</span>';
    $text .= ($p - $offset > 1 && $totalpages > $pagestep ? '<a target="'.$target.'" href="'.$mpurl_before.'1'.$mpurl_after.'&pn='.@$_GET['pn'].'" class="first"><b>1 …</b></a>' : '').($p > 1 ? '<a target="'.$target.'" href="'.$mpurl_before.($p - 1).$mpurl_after.'&pn='.@$_GET['pn'].'" class="prev" title="前一页"><b>&lt;&lt;</b></a>' : '');
    for ($i = $fr; $i <= $to; $i++) {
      $text .= $i == $p ? '<strong><b>'.$i.'</b></strong>' : '<a target="'.$target.'" href="'.$mpurl_before.$i.$mpurl_after.'&pn='.@$_GET['pn'].'" title="第'.$i.'页"><b>'.$i.'</b></a>';
    }
    $text .= ($p < $totalpages ? '<a target="'.$target.'" href="'.$mpurl_before.($p + 1).$mpurl_after.'&pn='.@$_GET['pn'].'" class="next" title="后一页"><b>&gt;&gt;</b></a>' : '').($to < $totalpages ? '<a target="'.$target.'" href="'.$mpurl_before.$totalpages.$mpurl_after.'&pn='.@$_GET['pn'].'" class="last" title="最后页"><b>… '.$totalpages.'</b></a>' : '');
    $text .= ($totalpages > $pagestep ? '<input type="text" name="pagego" id="pagego" size="3" title="按回车直接跳页" onblur="$(\'pagego\').value=this.value;" onkeydown="if(event.keyCode==13&&!isNaN(this.value)&&this.value>=1&&this.value<='.$totalpages.'){window.location=\''.$mpurl_before.'\'+this.value+\''.$mpurl_after.'&pn='.@$_GET['pn'].'\';return false;}" />' : '');
    $text .= '</div>';
    $text .= ' | <div class="pagein">以每页';
    foreach ($arr_page as $val) {
      $text .= '<a href="'.$mpurl_before.$p.$mpurl_after.'&pn='.$val.'"'.(@$_GET['pn']==$val?' class="pnis"':'').' target="'.$target.'">'.$val.'</a>';
      if ($val >= $totallists) break;
    }
    $text .= '条浏览</div>';
    $text .= '</div>';
  } else {
    if ($totallists > 0) {
      $text .= '<div class="page"><div class="pagein">';
	  $text .= '<span class="totallists" title="总记录数" style="width:auto">'.$totallists.'</span> 共1页';
      $text .= '</div>';
      $text .= '</div>';
    }
  }
  return $text;
}








?>