PHP 分页函数

前端调用

<tr><td colspan="5" align="right">{$pagebar1}</td></tr>


index.php


		 $page = $_REQUEST['page'] == '' ? 1 : $_REQUEST['page'];
		 $url = "my_task.php";
		 $per = 12;
		 $start = ( $page - 1 ) * $per;
		 unset( $_REQUEST['page'] ); 

		 /**
		  * 我的任务列表
		  * $pagebar  分页函数  functionn.php
		  */
		$sql="SELECT  count(*)  FROM  task  where user_id='".$_SESSION['user_id']."'    order by status,create_time DESC ";	
		$total=$db->getone($sql);

		$pagebar1 = pagebar( $url, array("type"=>"mytask"), 10, 1, $total );
		$mytasklimit="LIMIT  0,10 ";

分页函数
pagebar_lib.php

//分页
function pagebar( $url, $param, $limit, $page, $total, $type = 0 ) {
  if( $total < 0 ) {
	  return false;
  }
  if( $url == "" ) {
	return false;
  }
  $link = $url . "?";
  if (is_array($param)) {
	foreach ($param as $str_key => $str_value) {
	    $link = $link . "$str_key=" . urlencode($str_value) . "&";
	}
  }
  $int_pages = ceil($total / $limit);
  if ($page < 1) {
	$page = 1;
  }
  if ($page > $int_pages) {
	$page = $int_pages;
  }
  
  $start_url = $link . "page=1";
  $end_url = $link . "page=$int_pages";
  $pre_url = $link . "page=" . ( $page - 1 );
  $next_url = $link . "page=" . ( $page + 1 );
  if( $page < 6 ){
    /**
     * $start_page  从第几页开始
     * $end_page    显示到第几页
     */
	$start_page = 1;
    // $end_page = 7;
	$end_page = 2;
  }else{

    /**
     * $start_page  从第几页开始
     * $end_page    显示到第几页
     */
    // $start_page = $page -5;
	$start_page = $page -1;
	$end_page = $page +1;
  }
  if( $end_page > $int_pages ){
	$end_page = $int_pages;
  }
  $urls = null;
  /**  THE URL */
  for( $i = $start_page, $j = 0; $i <= $end_page; $i++, $j++ ){
	 $temp_url = $link . "page=$i";
	 if( $i == $page ){
 	    $urls[$j] = "<strong>[" . $i . "]</strong>&nbsp;";
	 }else{
	    $urls[$j] = "<a href=\"$temp_url\">[" . $i . "]</a>&nbsp;";
	}
  }
  $str_html="";
  if( is_array( $urls ) ){
	$str_html = "共&nbsp;".$total."&nbsp;条信息&nbsp;&nbsp;共&nbsp;".$int_pages."&nbsp;页&nbsp;&nbsp;". $str_html . "<a href=\"$start_url\" text=\回第1页\>首页</a>&nbsp;&nbsp;";
	if( $page > 1 ){
	    $str_html = $str_html . "<a href=\"$pre_url\">上页</a>&nbsp;&nbsp;";
	}else{
	    $str_html = $str_html . "上页&nbsp;&nbsp;";
	}
    if( $type == 0 ){
	    foreach ($urls as $sub_url) {
	      $str_html = $str_html . $sub_url;
	    }
    }
	$str_html = $str_html . "&nbsp;";
	if( $page >= $int_pages ){
	    $str_html = $str_html . "下页&nbsp;&nbsp;";
	}else{
	    $str_html = $str_html . "<a href=\"$next_url\">下页</a>&nbsp;&nbsp";
	}
    if( $type == 0 ){
	    $str_html = $str_html . "<a href=\"$end_url\" text=\"到第$int_pages\">尾页</a> &nbsp;&nbsp;跳到 <input type=text size=4 onBlur=window.location.href='$pre_url&page='+this.value  /> 页 ";  
	    return $str_html;
	}
	if( $type == 1 ){
        $str_html = $str_html . "<a href=\"$end_url\" text=\"到第$int_pages\">尾页</a>";
	    return $str_html;
	}
 }
	return false;
}

猜你喜欢

转载自blog.csdn.net/JXL9910/article/details/83410961