ExtMail服务器社区's Archiver

fengyong 发表于 2008-9-16 13:24

php分页轻松搞定(PHP 分页函数+数据类)

最近用ajax+php 写了一个基于web 的小程序(如下图),发现php 真的比perl 方便不少。在写码代过程中,自己写了一个分页的函数
现在把代码贴出来跟大家一起分享  开源易有 [url]http://www.yiyou.org[/url] (复制时请不要去掉开源易有的网址)
数据库类[code]
<?php
//this file name is :dbclass.php ,please include in your php file.
if (!defined(ACCESS)) {
        echo "can't access";
        exit;
}
class Db {
        function dbconn($host,$dbuser,$dbpass,$dbname) {
                mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("can't connect to mysql server".mysql_error());
                mysql_select_db($dbname) or die ("can't select db:".mysql_error());
                $this->q('set names "utf8"');
        }
        function q($sql){
                $result=mysql_query($sql) or die ("can't query:".mysql_error());
                if (!$result){
                        die ("can't query server:".mysql_error());
                }
                return $result;
        }
        function fetch_all($sql) {
                $result=$this->q($sql);
                while($row=mysql_fetch_assoc($result)){
                        $rows[]=$row;
                }
                mysql_free_result($result);
                return $rows;
        }
        function sum($sql){
                $result=$this->q($sql);
                $rows=mysql_num_rows($result);
                mysql_free_result($result);
                return $rows;
        }
        function query($sql){
                mysql_db_query($sql);
        }
        //$array_hash=$db->paging($recordofpage,$sql);
        function paging($rop,$sql){
                //get page
                $page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
                $total=$this->sum($sql);//all records.
                $pagenum=ceil($total/$rop); //get page total
                $page=min($pagenum,$page); // if request page > pagenum ,then fix it.
                $page=max(1,$page); //if request page < 1 then fix it.
                $rstart=($page-1)*$rop;
                //$rend=$page*$rop;
                //$sql.=" limit $rstart,$rend";
                                //上面行有错,下面是修正
                $sql.=" limit $rstart,$rop";
                $rows=$this->fetch_all($sql);
               
                $rr['total']=$total;
                $rr['pagenum']=$pagenum;
                $rr['crpage']=$page;
                $rr['rows']=$rows;
                $rr['rop']=$rop;
                return $rr;
        }
}
?>[/code]函数:[code]
<?php
//this file name is :lib.php
function makepaging($rr,$href){
//link this: $herf='javascript:page('LIST','inquiry.php?page=%P%');
//or :$herf='inquiry.php?page=%P%';
$pre=$rr['crpage']-1;
$next=$rr['crpage']+1;
$prelink=str_replace("%P%",$pre,$href);
$nextlink=str_replace("%P%",$next,$href);
?>
        <div align="center">
        <?php echo $rr['total'] ?> 条记录,每页[<?php echo $rr['rop']?>]条,当前第[<?php echo $rr['crpage'] ?>]页
        <a href="<?php echo $prelink?>">上一页</a>        <a href="<?php echo $nextlink ?>">下一页</a>        </div>
<?php } //end function?>[/code]调用方法:[code]
<?php
require_once("dbclass.php");
require_once("lib.php");
$db=new Db;
$db->dbconn('yourhost','username','passwd','mydb');
$SQL=" select * FROM yourtable";
$rr=$db->paging(20,$SQL); //  20条记录每页,注意SQL 里不能有limit 关键字
$rows=$rr['rows']; //取得每页的所有记录,
for($i=0;$i<count($rows);$i++){
//这里输出你的所有记录,假设数据库记录如下:
// name|age
//fy  | 100
// zs | 200
//  $rows[0]=array(name=>fy,age=>100);
//$rows[1]=array(name=zs,age=>200);
echo $rows[$i]['name'];
}
$href='query.php?page=%P%'
makepaging($rr,$href);  //输出分页
?>
[/code]paging 方法返回简介:
$rr['total']// 总共记录数
$rr['pagenum']// 总共的页数
$rr['crpage'] 当前页
$rr['rows']// 该页里的所有记录,数组
$rr['rop']// 每页多少条记录

[[i] 本帖最后由 fengyong 于 2008-9-18 09:30 编辑 [/i]]

hzqbbc 发表于 2008-9-16 21:43

yong哥厉害啊。。。越来越厉害了。景仰中:D

fengyong 发表于 2008-9-17 09:39

:lol  都是叼虫小技,那能与你相比

一帘幽梦 发表于 2008-9-17 14:49

不错,学习了。。。。。。。。。。支持下  :handshake

lastrose 发表于 2009-4-3 15:17

个人觉得php比perl容易理解 :lol

fengyong 发表于 2009-4-7 16:55

确实如此,至少PHP 有很多书可以参考,而且写得比较认真,你看看大驼骆,屁话一堆,不知所云

hzqbbc 发表于 2009-4-8 06:54

;P ;P ;P

bing4229 发表于 2009-4-18 17:29

因为这篇文章,上来注册个id,谢谢楼主分享

fengyong 发表于 2009-4-19 14:20

;P  现在在写yiyou.org 的自助友情链,用上面的代码,发现省了不少功夫,真舒服。

ryuhi 发表于 2009-4-24 00:03

完了完了,LZ这串代码我一定会Copy走的。
最近在写一个PHP的程序,最初只想写一个简单的PHP程序,不要太多的麻烦,结果写起来就收不住了,Smarty、Ajax(用Jquery)、类、视图……结果在配合Smarty的分页上出问题了。

ijkl591 发表于 2010-11-19 21:09

顶!

先回贴,再看贴。

[img]http://hiphotos.baidu.com/%D0%A1%B0%D7%CD%C3635241/pic/item/fd9ee53180a0f3c8a71e1226.jpg[/img]





[img]http://www.avidmeager.com/sigline.gif[/img]
[url=http://www.bjfthy.cn/][size=1][color=silver]方庄搬家公司[/color][/size][/url] [url=http://www.bjbj158.com/][size=1][color=silver]丰台搬家公司[/color][/size][/url]

lovemay 发表于 2011-6-15 10:05

php还是jsp 矛盾中

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.