PHP 后端分页函数的两种实现

一种是下拉列表的。

// 分页函数
function pagination($tot , $cur = 1, $psize = 30 , $base_url = '')
{
  $cur = $cur < 1?1:$cur;
  $tot_page = ceil($tot / $psize);
  if($tot_page <= 1 )return; //no pagin
  $result = '<ul class="pagination">';
  $startp = intval(($cur-1)/10);
  if($tot_page > ($startp+1)*10 ) {
    $total=($startp+1)*10;
  }else {
    $total=$tot_page;
  }

  $qp = strpos($base_url, '&');
  $query = strstr($base_url, '&');
  $qp && $base_url = substr($base_url, 0, $qp);

  if( $cur > 10 ) {
    $result.='<li><a href="'.$base_url.'1/'.$query.'"><span class="glyphicon glyphicon-step-backward"></span></a></li><li><a href="'.$base_url.($startp*10-9).'/'.$query.'"><span class="glyphicon glyphicon-backward"></span></a></li>';
  }

  $cur > 1 && $result .= '<li><a href="'.$base_url.($cur-1).'/'.$query.'"><span class="glyphicon glyphicon-chevron-left"></span></a></li>';
  $result .= '<select id="paginator">';
  for($i=$startp*10+1;$i<=$total;$i++)
  {
    if( $cur == $i )$result.='<option selected value="' . $base_url . $i . '/">'.$i;
    else $result.='<option value="' . $base_url . $i . '/'.$query.'">'.$i;
    $result .= '</option>';
  }
  $result .= '</select>';
  $cur < $total && $result .= '<li><a href="'.$base_url.($cur+1).'/'.$query.'"><span class="glyphicon glyphicon-chevron-right"></a></li>';

  if( $tot_page>($startp*10+10) )
    $result.='<li><a href="'.$base_url.($startp*10+11).'/'.$query.'" ><span class="glyphicon glyphicon-forward"></span></a></li>';
    $result.= '</ul>';
    return $result;
}

另外一种是一排按钮的分页函数

//重新定义样式的分页函数
function pagination_cms($tot , $cur = 1, $psize = 30 , $base_url = '')
{
  $cur = $cur < 1?1:$cur;
  $tot_page = ceil($tot / $psize);
  if($tot_page <= 1 )return; //no pagin
  $result = '<ul class="pagination">';
  $startp = intval(($cur-1)/10);
  if($tot_page > ($startp+1)*10 ) {
    $total=($startp+1)*10;
  }else {
    $total=$tot_page;
  }

  $qp = strpos($base_url, '&');
  $query = strstr($base_url, '&');
  $qp && $base_url = substr($base_url, 0, $qp);

  if( $cur > 10 ) {
    $result.='<li><a href="'.$base_url.'1/'.$query.'"><span class="glyphicon glyphicon-step-backward"></span></a></li><li><a href="'.$base_url.($startp*10-9).'/'.$query.'"><span class="glyphicon glyphicon-backward"></span></a></li>';
  }

  $cur > 1 && $result .= '<li><a href="'.$base_url.($cur-1).'/'.$query.'"><span class="glyphicon glyphicon-chevron-left"></span></a></li>';

  for($i=$startp*10+1;$i<=$total;$i++)
  {
    if( $cur == $i )$result.='<a href="' . $base_url . $i . '/">'.$i;
    else $result.='<a  href="' . $base_url . $i . '/'.$query.'">'.$i;
    $result .= '</option>';
  }
  $cur < $total && $result .= '<li><a href="'.$base_url.($cur+1).'/'.$query.'"><span class="glyphicon glyphicon-chevron-right"></a></li>';

  if( $tot_page>($startp*10+10) )
    $result.='<li><a href="'.$base_url.($startp*10+11).'/'.$query.'" ><span class="glyphicon glyphicon-forward"></span></a></li>';
    $result.= '</ul>';
    return $result;
}