PHP简单实用的数据分页显示代码

PHP简单实用的数据分页显示代码_第1张图片
index.php代码

  
02.include_once("config.php");  
03.require_once('page.class.php'); //分页类  
04.$showrow = 10; //一页显示的行数  
05.$curpage = empty($_GET['page']) ? 1 : $_GET['page']; //当前的页,还应该处理非数字的情况  
06.$url = "?page={page}"; //分页地址,如果有检索条件 ="?page={page}&q=".$_GET['q']  
07.//省略了链接mysql的代码,测试时自行添加  
08.$sql = "SELECT * FROM userinfo";  
09.$total = mysql_num_rows(mysql_query($sql)); //记录总条数  
10.if (!empty($_GET['page']) && $total != 0 && $curpage > ceil($total / $showrow))  
11.    $curpage = ceil($total_rows / $showrow); //当前页数大于最后页数,取最后一页  
12.//获取数据  
13.$sql .= " LIMIT " . ($curpage - 1) * $showrow . ",$showrow;";  
14.$query = mysql_query($sql);  
15.?>  
16.  
17.  
18.  
19.<html xmlns="http://www.w3.org/1999/xhtml">  
20.    <head>   
21.        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
22.        <title>演示:PHP简单漂亮的分页类title>  
23.        <meta name="keywords" content="php分页类" />  
24.        <meta name="description" content="本文介绍一款原生的PHP分页类,分页样式有点类似bootstrap。" />  
25.        <link rel="stylesheet" type="text/css" href="http://www.sucaihuo.com/jquery/css/common.css" />  
26.        <style type="text/css">  
27.            p{
      margin:0}  
28.            #page{  
29.                height:40px;  
30.                padding:20px 0px;  
31.            }  
32.            #page a{  
33.                display:block;  
34.                float:left;  
35.                margin-right:10px;  
36.                padding:2px 12px;  
37.                height:24px;  
38.                border:1px #cccccc solid;  
39.                background:#fff;  
40.                text-decoration:none;  
41.                color:#808080;  
42.                font-size:12px;  
43.                line-height:24px;  
44.            }  
45.            #page a:hover{  
46.                color:#077ee3;  
47.                border:1px #077ee3 solid;  
48.            }  
49.            #page a.cur{  
50.                border:none;  
51.                background:#077ee3;  
52.                color:#fff;  
53.            }  
54.            #page p{  
55.                float:left;  
56.                padding:2px 12px;  
57.                font-size:12px;  
58.                height:24px;  
59.                line-height:24px;  
60.                color:#bbb;  
61.                border:1px #ccc solid;  
62.                background:#fcfcfc;  
63.                margin-right:8px;  
64.  
65.            }  
66.            #page p.pageRemark{  
67.                border-style:none;  
68.                background:none;  
69.                margin-right:0px;  
70.                padding:4px 0px;  
71.                color:#666;  
72.            }  
73.            #page p.pageRemark b{  
74.                color:red;  
75.            }  
76.            #page p.pageEllipsis{  
77.                border-style:none;  
78.                background:none;  
79.                padding:4px 0px;  
80.                color:#808080;  
81.            }  
82.            .dates li {font-size: 14px;margin:20px 0}  
83.            .dates li span{
      float:right}  
84.        style>  
85.    head>  
86.    <body>  
87.        <div class="head">  
88.            <div class="head_inner clearfix">  
89.                <ul id="nav">  
90.                    <li><a href="http://www.sucaihuo.com">首 页a>li>  
91.                    <li><a href="http://www.sucaihuo.com/templates">网站模板a>li>  
92.                    <li><a href="http://www.sucaihuo.com/js">网页特效a>li>  
93.                    <li><a href="http://www.sucaihuo.com/php">PHPa>li>  
94.                    <li><a href="http://www.sucaihuo.com/site">精选网址a>li>  
95.                ul>  
96.                <a class="logo" href="http://www.sucaihuo.com"><img src="http://www.sucaihuo.com/Public/images/logo.jpg" alt="素材火logo" />a>  
97.            div>  
98.        div>  
99.        <div class="container">  
100.            <div class="demo">  
101.                <h2 class="title"><a href="http://www.sucaihuo.com/php/223.html">教程:PHP简单漂亮的分页类a>h2>  
102.  
103.                <div class="showData">  
104.  
105.                    <ul class="dates">  
106.                         while ($row = mysql_fetch_array($query)) { ?>  
107.                            <li>  
108.                                <span> echo $row['t3'] ?>span>  
109.                                <a target="_blank" href="http://www.sucaihuo.com/js"> echo $row['t1'] ?>a>  
110.                            li>  
111.                         } ?>  
112.                    ul>  
113.                      
114.                div>  
115.                <div class="showPage">  
116.                      
117.                    if ($total > $showrow) {
     //总记录数大于每页显示数,显示分页  
118.                        $page = new page($total, $showrow, $curpage, $url, 2);  
119.                        echo $page->myde_write();  
120.                    }  
121.                    ?>  
122.                div>  
123.            div>  
124.        div>  
125.        <div class="foot">  
126.            Powered by sucaihuo.com  本站皆为作者原创,转载请注明原文链接:<a href="http://www.sucaihuo.com" target="_blank">www.sucaihuo.coma>  
127.        div>  
128.        <script type="text/javascript" src="http://www.sucaihuo.com/Public/js/other/jquery.js">script>   
129.    body>  
130.html>  

page.class.php代码

php  
02.  
03./* * *********************************************  
04. * @类名:   page  
05. * @参数:   $myde_total - 总记录数  
06. *          $myde_size - 一页显示的记录数  
07. *          $myde_page - 当前页  
08. *          $myde_url - 获取当前的url  
09. * @功能:   分页实现  
10. * @作者:   宋海阁  
11. */  
12.  
13.class page {  
14.  
15.    private $myde_total;          //总记录数  
16.    private $myde_size;           //一页显示的记录数  
17.    private $myde_page;           //当前页  
18.    private $myde_page_count;     //总页数  
19.    private $myde_i;              //起头页数  
20.    private $myde_en;             //结尾页数  
21.    private $myde_url;            //获取当前的url  
22.    /*  
23.     * $show_pages  
24.     * 页面显示的格式,显示链接的页数为2*$show_pages+1。  
25.     * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]   
26.     */  
27.    private $show_pages;  
28.  
29.    public function __construct($myde_total = 1, $myde_size = 1, $myde_page = 1, $myde_url, $show_pages = 2) {  
30.        $this->myde_total = $this->numeric($myde_total);  
31.        $this->myde_size = $this->numeric($myde_size);  
32.        $this->myde_page = $this->numeric($myde_page);  
33.        $this->myde_page_count = ceil($this->myde_total / $this->myde_size);  
34.        $this->myde_url = $myde_url;  
35.        if ($this->myde_total < 0)  
36.            $this->myde_total = 0;  
37.        if ($this->myde_page < 1)  
38.            $this->myde_page = 1;  
39.        if ($this->myde_page_count < 1)  
40.            $this->myde_page_count = 1;  
41.        if ($this->myde_page > $this->myde_page_count)  
42.            $this->myde_page = $this->myde_page_count;  
43.        $this->limit = ($this->myde_page - 1) * $this->myde_size;  
44.        $this->myde_i = $this->myde_page - $show_pages;  
45.        $this->myde_en = $this->myde_page + $show_pages;  
46.        if ($this->myde_i < 1) {  
47.            $this->myde_en = $this->myde_en + (1 - $this->myde_i);  
48.            $this->myde_i = 1;  
49.        }  
50.        if ($this->myde_en > $this->myde_page_count) {  
51.            $this->myde_i = $this->myde_i - ($this->myde_en - $this->myde_page_count);  
52.            $this->myde_en = $this->myde_page_count;  
53.        }  
54.        if ($this->myde_i < 1)  
55.            $this->myde_i = 1;  
56.    }  
57.  
58.    //检测是否为数字  
59.    private function numeric($num) {  
60.        if (strlen($num)) {  
61.            if (!preg_match("/^[0-9]+$/", $num)) {  
62.                $num = 1;  
63.            } else {  
64.                $num = substr($num, 0, 11);  
65.            }  
66.        } else {  
67.            $num = 1;  
68.        }  
69.        return $num;  
70.    }  
71.  
72.    //地址替换  
73.    private function page_replace($page) {  
74.        return str_replace("{page}", $page, $this->myde_url);  
75.    }  
76.  
77.    //首页  
78.    private function myde_home() {  
79.        if ($this->myde_page != 1) {  
80.            return "首页";  
81.        } else {  
82.            return "

首页

"
; 83. } 84. } 85. 86. //上一页 87. private function myde_prev() { 88. if ($this->myde_page != 1) { 89. return "上一页"; 90. } else { 91. return "

上一页

"
; 92. } 93. } 94. 95. //下一页 96. private function myde_next() { 97. if ($this->myde_page != $this->myde_page_count) { 98. return "下一页"; 99. } else { 100. return"

下一页

"
; 101. } 102. } 103. 104. //尾页 105. private function myde_last() { 106. if ($this->myde_page != $this->myde_page_count) { 107. return "尾页"; 108. } else { 109. return "

尾页

"
; 110. } 111. } 112. 113. //输出 114. public function myde_write($id = 'page') { 115. $str = "
. $id . ">"; 116. $str.=$this->myde_home(); 117. $str.=$this->myde_prev(); 118. if ($this->myde_i > 1) { 119. $str.="

...

"
; 120. } 121. for ($i = $this->myde_i; $i <= $this->myde_en; $i++) { 122. if ($i == $this->myde_page) { 123. $str.="$i"; 124. } else { 125. $str.="$i"; 126. } 127. } 128. if ($this->myde_en < $this->myde_page_count) { 129. $str.="

...

"
; 130. } 131. $str.=$this->myde_next(); 132. $str.=$this->myde_last(); 133. $str.="

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

"
; 135. $str.="
"
; 136. return $str; 137. } 138. 139.} 140. 141.?>

config.php代码

  
02.$host="localhost";  
03.$db_user="root";  
04.$db_pass="root";  
05.$db_name="job";  
06.$timezone="Asia/Shanghai";  
07.  
08.$link=mysql_connect($host,$db_user,$db_pass);  
09.mysql_select_db($db_name,$link);  
10.mysql_query("SET names UTF8");  
11.  
12.header("Content-Type: text/html; charset=utf-8");  
13.?>  

你可能感兴趣的:(php+mysql基础,php,分页)