---
title: "PHP分页"
author: "conkeyn"
date: "2010-05-05T07:15:27Z"
tags: []
categories: ["编程语言"]
slug: "php"
draft: false
lastmod: "2010-05-13T10:31:29Z"
id: 659190
url: "https://conkeyn.iteye.com/blog/659190"
---

```
<?php
header("content-type:text/html;charset=utf-8");
/*
 * 分页功能
 *
 */
function cut_pages($url_prefix = "", $total_records = 0, $record_per_page = 20, $pages_length = 7,
$pages_ul_style = "list-style:none;width:410px;margin:auto; ",
$page_li_style = "float:left;font-size:12px; background:white; margin:0px 3px; line-height:1em; padding: .25em .5em; border:solid #39F 1px; cursor:pointer;",
$link_style = "text-decoration:none;", $onmousemove_color = "#39F", $onmouseout_color = "white")
{
    $start = 1;
    $end   = 7;
    if (isset ($_REQUEST["pageno"])){
        $current_page = intval($_REQUEST["pageno"]);
    } else
    {
        $current_page=1;
    }
    $page_string="";
    $total_pages=0;
    // 根据总记录数计算出总页数
    if ($total_records > 0)
    {
        $pages_temp  = intval($total_records / $record_per_page);
        $total_pages = $total_records%$record_per_page == 0 ? $pages_temp : $pages_temp + 1;
        if ($current_page < $total_pages)
        {
            $start = $current_page - intval($pages_length / 2);
            if ($start < 1)
            {
                $start = 1;
            }
            $end = $start + $pages_length - 1;
            if ($end > $total_pages)
            {
                $end   = $total_pages;
                $start = $end - $pages_length + 1;
            }
            if ($current_page == 1)
            $end = $pages_length;
        }
        if ($current_page == $total_pages)
        {
            $start = $total_pages - $pages_length + 1;
            $end = $total_pages;
        }
        if ($start < 1) {
            $start = 1;
        }
        if ($end > $total_pages)
        {
            $end = $total_pages;
        }
        // 分页字符串
        $page_string="";
        if (! empty($pages_ul_style))
        {
            $page_string = "<ul style=\"".$pages_ul_style."\">\n";
        }
        // URL参数
        // $url_paramters = $_SERVER["QUERY_STRING"];
        // if (stripos($url_paramters, "pageno") === false)
        // {
        //     if (strlen($url_paramters) > 0)
        //     {
        //         $url_paramters .= "&pageno=1";
        //     }
        // }
        
        $url_prefix = cal($url_prefix);
        // 如果$pages_length为偶数，则+1改为奇数
        $pages_length % 2 == 0 ? $pages_length + 1 : $pages_length;
        if ($current_page > intval($pages_length / 2))
        {
            // 首页
            $page_string .= "<li style=\"".$page_li_style."\" onmousemove=\"this.style.backgroundColor='$onmousemove_color';\" onmouseout=\"this.style.backgroundColor='$onmouseout_color';\" ><a style=\"$link_style\" href=\"".preg_replace("/pageno=\d+/", "pageno=1", $url_prefix)."\" title=\"首页\">首页</a></li>\n";
        } else
        {
            $page_string .= "<li style=\"".$page_li_style."\" >首页</li>\n";
        }
        if ($current_page > 1)
        {
            $page_string.="<li style=\"".$page_li_style."\" onmousemove=\"this.style.backgroundColor='$onmousemove_color';\" onmouseout=\"this.style.backgroundColor='$onmouseout_color';\" ><a style=\"$link_style\" href=\"".preg_replace("/pageno=\d+/", "pageno=".($current_page-1), $url_prefix)."\" title=\"上页\">上页</a></li>\n";
        } else
        {
            $page_string.= "<li style=\"".$page_li_style."\" >上页</li>\n";
        }
        for ($i = $start; $i <= $end; $i++)
        {
            if ($current_page == $i)
            {
                $page_string .= "<li style=\"".$page_li_style."\" ><font color=red><b>&nbsp;". $i. "&nbsp;</b></font></li>\n";
            } else
            {
                $page_string .= "<li style=\"".$page_li_style."\" onmousemove=\"this.style.backgroundColor='$onmousemove_color';\" onmouseout=\"this.style.backgroundColor='$onmouseout_color';\" ><a style=\"$link_style\" href=\"" .preg_replace("/pageno=\d+/", "pageno=".$i, $url_prefix). "\"><font color=#000000>&nbsp;" .$i."&nbsp;</font></a></li>\n";
            }
        }
        // 下一页
        if ($total_pages > $current_page)
        {
            $page_string .= "<li style=\"".$page_li_style."\" onmousemove=\"this.style.backgroundColor='$onmousemove_color';\" onmouseout=\"this.style.backgroundColor='$onmouseout_color';\" ><a style=\"$link_style\" href=\"".preg_replace("/pageno=\d+/", "pageno=".($current_page+1), $url_prefix)."\" title=\"下页\">下页</a></li>\n";
        } else
        {
            $page_string .= "<li style=\"".$page_li_style."\" >下页</li>\n";
        }
        if ($total_pages > ($current_page + intval($pages_length / 2)))
        {
            $page_string .= "<li style=\"".$page_li_style."\" onmousemove=\"this.style.backgroundColor='$onmousemove_color';\" onmouseout=\"this.style.backgroundColor='$onmouseout_color';\" ><a style=\"$link_style\" href=\"".preg_replace("/pageno=\d+/", "pageno=".$total_pages, $url_prefix)."\" title=\"末页\">末页</a></li>\n";
        } else
        {
            $page_string .= "<li style=\"".$page_li_style."\" >末页</li>\n";
        }
        $page_string .= "</ul>";
    } else
    {
        // 总记录数为0
    }
    echo $page_string."<br />";
}
function cal($url_prefix)
{
    // 如果URL不包含查询字符串时，加上查询字符串的问号
    if (stripos($url_prefix, "?") === false)
    {
        $url_prefix.="?";
    } else if(strripos($url_prefix, "?") != strlen($url_prefix) - 1 
        && strripos($url_prefix,"&") != strlen($url_prefix) - 1 
        && stripos($url_prefix, "pageno") === false)
    {
        $url_prefix.="&";
    }
    if (stripos($url_prefix, "pageno") === false)
    {
        $url_prefix .= "pageno=1";
    }
    return $url_prefix;
}
// $url_prefix="http://localhost/dbcenter/view/credit_cost_logs.php?XDEBUG_SESSION_START=ECLIPSE_DBGP&KEY=12737129566093";
// $total_records=50;
// $record_per_page=10;
// $pages_length=7;
// cut_pages($url_prefix,$total_records, $record_per_page, $pages_length);
?>
```

 在其他页面引用代码及需要增加的代码如下：

```
<?php
$url_prefix = $_SERVER["REQUEST_URI"];
$record_per_page = 20;
$pages_length = 7;
$pageno = 1;
if (! empty($_REQUEST["pageno"]))
{
    $pageno = intval($_REQUEST["pageno"]);
}

// 以下是显示分页的代码。

cut_pages($url_prefix,$total_records, $record_per_page, $pages_length);?>

```