常用php分页函数

常用php分页函数...

/**
 *函数print_paging
 *功能:实现分页
 *将分页的导航制作包装成一个表的格式
 *分页导航格式,例如如下格式:
 */
//<table width=25% bgcolor=#DAE8F5 align=center><tr align=center><td>几页/几条</td><td>首页</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>尾页</td></tr></table>
function print_paging($tb) {
    $this->table = $tb;
    $page_id = $this->get_page_id ();
    $up_page = $this->get_up_page ( $page_id );
    $ne_page = $this->get_next_page ( $page_id );
    $total_rows = $this->counting ();
    $total_pages = $this->get_total_pages ();
    $page_size = $this->page_size;
    if ($page_size >= $total_rows) {
        //当每页的最大条数大于查询的总条数时,即只有1页

    } else {
        if ($total_pages <= 7) { //当页码数小于等于7页时,打印出所有的页码链接,格式如:共34条/7页 首页 1 2 3 4 5 6 7 尾页
            $str = "<table width=25% bgcolor=#DAE8F5 align=center><tr align=center>" . "<td>共" . $total_rows . "条/" . $total_pages . "页</td><td>" . $this->print_url ( "首页", 1 ) . "</td>";
            for($i = 1; $i <= $total_pages; $i ++) {
                if ($i == $page_id) {
                    $str .= "<td>" . $i . "</td>";
                } else {
                    $str .= "<td>" . $this->print_url ( $i, $i ) . "</td>";
                }
            }
            $str .= "<td>" . $this->print_url ( "尾页", $total_pages ) . "</td></tr></table>";

        //              return $str;
        } else { //当页面数大于7时,打印当前页的前三页后后三页,格式如下:共34条/12页 首页 3 4 5 6 7 8 9 尾页
            if ($page_id <= 3) { //当页码数小于3时,打印当前页以及前面的所有页和随后的3页
                $str = "<table width=25% bgcolor=#DAE8F5 align=center><tr align=center>" . "<td>共" . $total_rows . "条/" . $total_pages . "页</td><td>" . $this->print_url ( "首页", 1 ) . "</td>";
                for($i = 1; $i <= $page_id + 3; $i ++) {
                    if ($i == $page_id) {
                        $str .= "<td>" . $i . "</td>";
                    } else {
                        $str .= "<td>" . $this->print_url ( $i, $i ) . "</td>";
                    }
                }
                $str .= "<td>" . $this->print_url ( "尾页", $total_pages ) . "</td></tr></table>";

        //                  return $str;
            }
            if ($page_id >= $total_pages - 3) { //当页码数小于总页数-3时,打印当前页前三页和最后的所有页
                $str = "<table width=25% bgcolor=#DAE8F5 align=center><tr align=center>" . "<td>共" . $total_rows . "条/" . $total_pages . "页</td><td>" . $this->print_url ( "首页", 1 ) . "</td>";
                for($i = $page_id - 3; $i <= $total_pages; $i ++) {
                    if ($i == $page_id) {
                        $str .= "<td>" . $i . "</td>";
                    } else {
                        $str .= "<td>" . $this->print_url ( $i, $i ) . "</td>";
                    }
                }
                $str .= "<td>" . $this->print_url ( "尾页", $total_pages ) . "</td></tr></table>";

        //                  return $str;
            }
            if ($page_id < $total_pages - 3 && $page_id > 3) { //打印前三页当前页和后三页
                $str = "<table width=25% bgcolor=#DAE8F5 align=center><tr align=center>" . "<td>共" . $total_rows . "条/" . $total_pages . "页</td><td>" . $this->print_url ( "首页", 1 ) . "</td>";
                for($i = $page_id - 3; $i <= $page_id + 3; $i ++) {
                    if ($i == $page_id) {
                        $str .= "<td>" . $i . "</td>";
                    } else {
                        $str .= "<td>" . $this->print_url ( $i, $i ) . "</td>";
                    }
                }
                $str .= "<td>" . $this->print_url ( "尾页", $total_pages ) . "</td></tr></table>";

        //                  return $str;
            }

        //              return $str;
        }
        return $str;
    }
}

2 Responses to “常用php分页函数”

Leave a Reply

(will not be published)