Home > Backend Development > PHP Tutorial > PHP universal paging component

PHP universal paging component

WBOY
Release: 2016-07-29 08:57:28
Original
1047 people have browsed it
/* ***************************************** ****
* @Class name: page
* @Parameters: $myde_total - Total number of records
* $myde_size - Number of records displayed on one page
* * $myde_page - Current page
* $myde_url - Get the current url
* @Function: Paging implementation
*/
class page {
private $myde_total; //Total number of records
private $myde_size; _count; //The total number of pages
private $myde_i; s
* The format of page display, The number of pages to display links is 2*$show_pages+1.
     * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]
     */
    private $show_pages;
    public function __construct($myde_total = 1, $myde_size = 1, $myde_page = 1, $myde_url, $show_pages = 2) {
        $this->myde_total = $this->numeric($myde_total);
        $this->myde_size = $this->numeric($myde_size);
        $this->myde_page = $this->numeric($myde_page);
        $this->myde_page_count = ceil($this->myde_total / $this->myde_size);
        $this->myde_url = $myde_url;
        if ($this->myde_total < 0)
            $this->myde_total = 0;
        if ($this->myde_page < 1)
            $this->myde_page = 1;
        if ($this->myde_page_count < 1)
            $this->myde_page_count = 1;
        if ($this->myde_page > $this->myde_page_count)
            $this->myde_page = $this->myde_page_count;
        $this->limit = ($this->myde_page - 1) * $this->myde_size;
        $this->myde_i = $this->myde_page - $show_pages;
        $this->myde_en = $this->myde_page + $show_pages;
        if ($this->myde_i < 1) {
            $this->myde_en = $this->myde_en + (1 - $this->myde_i);
            $this->myde_i = 1;
        }
        if ($this->myde_en > $this->myde_page_count) {
            $this->myde_i = $this->myde_i - ($this->myde_en - $this->myde_page_count);
            $this->myde_en = $this->myde_page_count;
        }
        if ($this->myde_i < 1)
            $this->myde_i = 1;
    }
    //检测是否为数字
    private function numeric($num) {
        if (strlen($num)) {
            if (!preg_match("/^[0-9]+$/", $num)) {
                $num = 1;
            } else {
                $num = substr($num, 0, 11);
            }
        } else {
            $num = 1;
        }
        return $num;
    }
    //地址替换
    private function page_replace($page) {
        return str_replace("{page}", $page, $this->myde_url);
    }
    //首页
    private function myde_home() {
        if ($this->myde_page != 1) {
            return "首页";
        } else {
            return "

首页

";
        }
    }
    //上一页
    private function myde_prev() {
        if ($this->myde_page != 1) {
            return "上一页";
        } else {
            return "

上一页

";
        }
    }
    //下一页
    private function myde_next() {
        if ($this->myde_page != $this->myde_page_count) {
            return "下一页";
        } else {
            return"

下一页

";
        }
    }
    //尾页
    private function myde_last() {
        if ($this->myde_page != $this->myde_page_count) {
            return "尾页";
        } else {
            return "

尾页

";
        }
    }
    //输出
    public function myde_write($id = 'page') {
        $str = "
";
        $str.=$this->myde_home();
        $str.=$this->myde_prev();
        if ($this->myde_i > 1) {
            $str.="

...

";
        }
        for ($i = $this->myde_i; $i <= $this->myde_en; $i++) {
            if ($i == $this->myde_page) {
                $str.="$i";
            } else {
                $str.="$i";
            }
        }
        if ($this->myde_en < $this->myde_page_count) {
            $str.="

...

";
        }
        $str.=$this->myde_next();
        $str.=$this->myde_last();
        $str.="

" . $this->myde_page_count .
                "
" . $this->myde_total . "条数据

";
        $str.="
";
        return $str;
    }
}

?>

include_once("config.php");
require_once('page.class.php'); //Paging class
$showrow = 3; //Number of rows displayed on one page
$curpage = empty($_GET['page']) ? 1 : $_GET['page']; //The current page should also handle non-numeric situations
$url = "?page={page}"; //Paging Address, if there is a search condition="?page={page}&q=".$_GET['q']
//The code linking mysql is omitted, add it yourself during testing
$sql = "SELECT id,content,lastdate FROM message";
$total = mysql_num_rows(mysql_query($sql)); //Total number of records
if (!empty($_GET['page']) && $total != 0 && $curpage > ceil( $total / $showrow))
$curpage = ceil($total_rows / $showrow); //The current page number is greater than the last page number, take the last page
//Get data
$sql .= " LIMIT " . ($ curpage - 1) * $showrow . ",$showrow;";
$query = mysql_query($sql);
?>



                                                                                                                                                  ;meta name="keywords" c /> .com/jquery/css/common.css" />
                                                                                       padding:20px 0px;
}
                                                                                                                                                                         display:block; px;
                                                                        using   use with             through ’ through ’ through     through using ’ through ’ s ’ through     through ’ through ‐ to ‐ ‐‐‐‐‐‐ ‐ ‐ ‐ ‐ ‐ _ _ _ to 1px;                                -decoration:none;
                                                                                      #page a:hover{
                                                                                                                                                      
                                                                                                                                                        #page p{
                                                                                                        float:left; height:24px;
                                                                                                                                                                                                margin-right:8px; style:none;
                    background:none; }
            #page p.pageRemark b{
                color:red;
            }
            #page p.pageEllipsis{
                border-style:none;
                background:none;
                padding:4px 0px;
                color:#808080;
            }
            .dates li {font-size: 14px;margin:20px 0}
            .dates li span{float:right}
       
   
   
       


           

               
               
           

       

       

           

               

教程:PHP简单漂亮的分页类


               

                   

                           
                               

  •                                
                                   
                               

  •                        
                       

                   
               

                                                                                                             $page = new page($total , $showrow, $curpage, $url, 2);                                                                                                         ; div class = "FOOT" & GT;
Powered by Sucaihuo.com This site is the original author, please indicate the original link: & lt; a href = "http://www.sucaihu.com" target = "_ Blank" "" >www.sucaihuo.com
                                                                                        ">






$host="localhost";
$db_user="root";
$db_pass=" ";
$db_name="test";
$timez/Shanghai";
$link=mysql_connect($host,$db_user,$db_pass);
mysql_select_db($db_name,$link);mysql_query("SET names UTF8 ");

header("Content-Type: text/html; charset=utf-8");

?>


The above has introduced the PHP general paging component, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template