<html>
<body>
<?
$gConn=OCILogon("jdswz","jdswz","yc"); 
$sql="select max(rownum) from ST_WAS_R where stcd='51001200' and TM>=trunc(sysdate-2 ,'dd') ";
$stmt=ociparse($gConn,$sql);
ociexecute($stmt);
ocifetch($stmt);
$rowcount=ociresult($stmt,1);
ocifreestatement($stmt);
echo("共有".$rowcount."条记录<br>");
$recordperpage=30; //每页显示多少条记录
$pages=ceil($rowcount/$recordperpage);  //总页数
echo("共有".$pages."页<br>");
?>
<table border=0><tr><td>TM</td><td>UPZ</td></tr>
<?//判断偏移量参数是否传递给了脚本,如果没有就使用默认值0if (empty($offset))
{
$offset=1;
}$currentpage=ceil($offset/$recordperpage); //显示当前页
echo("当前页:".$currentpage."<br>");
$endset=$offset+$recordperpage;$stmt2=ociparse($gConn,"SELECT rownum,TM,UPZ FROM ST_WAS_R  where STCD='51003850' and TM>=trunc(sysdate-2 ,'dd') and rownum<".$endset." minus select rownum,TM,UPZ from ST_WAS_R where STCD='51003850' and TM>=trunc(sysdate-2 ,'dd') and rownum<".$offset);ociexecute($stmt2);
//可以是任何sql语句,但select后面一定要有rownum,这是oracle特有的!
while (ocifetch($stmt2)){ 
echo("<tr><td>".ociresult($stmt2,"TM")."</td><td>".ociresult($stmt2,"UPZ")."</td></tr>");//换成你用于显示返回记录的代码
}
//要写出到所有页面的链接
print "</table><br><br>";for ($i=1; $i <= $pages; $i++)
{
$newoffset=($recordperpage*($i-1))+1;
print "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> ";
}$nextoffset=$recordperpage*$currentpage+1;
$prevoffset=$recordperpage*($currentpage-2)+1;
//判断是否需要上一页连接
if (($currentpage>1) && ($currentpage<=$pages))
{
print "<a href=\"$PHP_SELF?offset=$prevoffset\">上一页</a> ";
}//判断是否需要下一页连接
if (($pages!=0) && ($currentpage<$pages))
{
print "<a href=\"$PHP_SELF?offset=$nextoffset\">下一页</a> ";
}
ocifreestatement($stmt2);
ocilogoff($gConn);?>
</body>
</html> 从网上找的代码修改了下 ,反复运行发现当点第二页的时候http://localhost/jwz.php?offset=31 显示还是当前第一页,offset的值没有传递,求高手帮忙修改。

解决方案 »

  1.   

    这个没有模拟测试环境很难看出来啊。 
    我在网上也看过很多分页的代码。
    不过我觉得,分页代码应该单独抽象出来。不应该和表现层的东西绑定,也不应该和数据库的东西绑定。Pagination.php
            // 以下这5个变量交给表现层。
            private $preGroup;
    private $nextGroup;
    private $previous;
    private $next;
    private $pages; //array();
    构造函数如下:function __construct($curpage,$totalRecord,$pagesize=10,$linksize=5)
    //***********************************************************************
    <?php
    class Pagination { private $preGroup;
    private $nextGroup;
    private $previous;
    private $next;
    private $pages; //array();

    /**
     * @return the $preGroup
     */
    public function getPreGroup() {
    return $this->preGroup;
    } /**
     * @return the $nextGroup
     */
    public function getNextGroup() {
    return $this->nextGroup;
    } /**
     * @return the $previous
     */
    public function getPrevious() {
    return $this->previous;
    } /**
     * @return the $next
     */
    public function getNext() {
    return $this->next;
    } /**
     * @return the $pages
     */
    public function getPages() {
    return $this->pages;
    } /**
     * @param field_type $preGroup
     */
    public function setPreGroup($preGroup) {
    $this->preGroup = $preGroup;
    } /**
     * @param field_type $nextGroup
     */
    public function setNextGroup($nextGroup) {
    $this->nextGroup = $nextGroup;
    } /**
     * @param field_type $previous
     */
    public function setPrevious($previous) {
    $this->previous = $previous;
    } /**
     * @param field_type $next
     */
    public function setNext($next) {
    $this->next = $next;
    } /**
     * @param field_type $pages
     */
    public function setPages($pages) {
    $this->pages = $pages;
    } public function __construct($curpage,$totalRecord,$pagesize=10,$linksize=5){
    if(!is_numeric($curpage)){
    //PandaExceptionHandle::errorprint('Wrong current page number .');
    exit();
    }
    $tolpage = ceil($totalRecord/$pagesize)   ;
    if($curpage>$tolpage){
    $curpage = $tolpage;
    }
    if($curpage<=0){
    $curpage = 1;
    }

    if($tolpage==0){
    // null
    }else{
    $thin = floor($curpage/$linksize);  //   1 2 3 4 5     6 7 8 9 10   11 12 13 14 
    $full = ceil($curpage/$linksize);  
    $mod = $curpage % $linksize ;
    if($mod==0){
    $thin = $thin-1;
    }


    $first = 0;  //当前分页第一页
    $last = 0;   //当前分页最后一页

    //preGroup 和 prePage
    if($thin>0){
    $this->preGroup = $thin*$linksize;
    }
    if($curpage>1){
    $this->previous = $curpage-1;
    }

    //pages array();
    $minpage = $thin*$linksize +1 ;
    $maxpage = $full*$linksize;
    if($tolpage<$full*$linksize){
    $maxpage = $tolpage;
    }
    for($i=$minpage;$i<$maxpage+1;$i++){
    $this->pages[] = $i;
    }

    // nextGroup 和 nextpage 
    if($curpage<$tolpage){
    $this->next = $curpage+1;
    }

    //
    if($tolpage>$full*$linksize){
    $this->nextGroup = $full*$linksize +1;
    }

    }
    }
    public function test(){
    if($this->preGroup){
    echo '<<'.$this->preGroup.' ';
    }

    if($this->previous){
    echo '<'.$this->previous.' ';
    }

    if(!empty($this->pages)){
    foreach ($this->pages as $i){
    echo $i.' ';
    }
    }

    if($this->next){
    echo $this->next.'> ';
    }
    if($this->nextGroup){
    echo $this->nextGroup.'>> ';
    }

    }// $curpage,$totalRecord,$pagesize=10,$linksize=5   totalpage = 10页      1 2 3 4 5 6 7 8 9 10
    $paginating = new Pagination(6,99);
    $paginating->test();