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

本文介绍了一款原生PHP分页类的实现方法,通过具体代码展示了如何进行数据分页展示,并提供了完整的分页样式设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图
index.php代码

<?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.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
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">PHP</a></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.                        <?php while ($row = mysql_fetch_array($query)) { ?>  
107.                            <li>  
108.                                <span><?php echo $row['t3'] ?></span>  
109.                                <a target="_blank" href="http://www.sucaihuo.com/js"><?php echo $row['t1'] ?></a>  
110.                            </li>  
111.                        <?php } ?>  
112.                    </ul>  
113.                    <!--显示数据区-->  
114.                </div>  
115.                <div class="showPage">  
116.                    <?php  
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.com</a>  
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 "<a href='" . $this->page_replace(1) . "' title='首页'>首页</a>";  
81.        } else {  
82.            return "<p>首页</p>";  
83.        }  
84.    }  
85.  
86.    //上一页  
87.    private function myde_prev() {  
88.        if ($this->myde_page != 1) {  
89.            return "<a href='" . $this->page_replace($this->myde_page - 1) . "' title='上一页'>上一页</a>";  
90.        } else {  
91.            return "<p>上一页</p>";  
92.        }  
93.    }  
94.  
95.    //下一页  
96.    private function myde_next() {  
97.        if ($this->myde_page != $this->myde_page_count) {  
98.            return "<a href='" . $this->page_replace($this->myde_page + 1) . "' title='下一页'>下一页</a>";  
99.        } else {  
100.            return"<p>下一页</p>";  
101.        }  
102.    }  
103.  
104.    //尾页  
105.    private function myde_last() {  
106.        if ($this->myde_page != $this->myde_page_count) {  
107.            return "<a href='" . $this->page_replace($this->myde_page_count) . "' title='尾页'>尾页</a>";  
108.        } else {  
109.            return "<p>尾页</p>";  
110.        }  
111.    }  
112.  
113.    //输出  
114.    public function myde_write($id = 'page') {  
115.        $str = "<div id=" . $id . ">";  
116.        $str.=$this->myde_home();  
117.        $str.=$this->myde_prev();  
118.        if ($this->myde_i > 1) {  
119.            $str.="<p class='pageEllipsis'>...</p>";  
120.        }  
121.        for ($i = $this->myde_i; $i <= $this->myde_en; $i++) {  
122.            if ($i == $this->myde_page) {  
123.                $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页' class='cur'>$i</a>";  
124.            } else {  
125.                $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页'>$i</a>";  
126.            }  
127.        }  
128.        if ($this->myde_en < $this->myde_page_count) {  
129.            $str.="<p class='pageEllipsis'>...</p>";  
130.        }  
131.        $str.=$this->myde_next();  
132.        $str.=$this->myde_last();  
133.        $str.="<p class='pageRemark'>共<b>" . $this->myde_page_count .  
134.                "</b>页<b>" . $this->myde_total . "</b>条数据</p>";  
135.        $str.="</div>";  
136.        return $str;  
137.    }  
138.  
139.}  
140.  
141.?>  

config.php代码

<?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.?>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值