程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 一個odbc連mssql分頁的類

一個odbc連mssql分頁的類

編輯:關於PHP編程

<!--二泉.net -->
<?
class Pages{
    var $cn;        //連接數據庫游標
    var $d;            //連接數據表的游標
    var $result;    //結果
    var $dsn;        //dsn源
    var $user;        //用戶名    
    var $pass;        //密碼

    var $total;        //記錄總數
    var $pages;        //總頁數
    var $onepage;    //每頁條數
    var $page;        //當前頁
    var $fre;        //上一頁
    var $net;        //下一頁
    var $i;            //控制每頁顯示

    function getConnect($dsn,$user,$pass){
        $this->cn=@odbc_connect($dsn,$user,$pass);
        if(!$this->cn){
            $error="連接數據庫出錯";
            $this->getMess($error);
        }
    }

    function getDo($sql){//從表中查詢數據
        $this->d=@odbc_do($this->cn,$sql);
        if(!$this->d){
            $error="查詢時發生了小錯誤......";
            $this->getMess($error);
        }
        return $this->d;
    }

    function getTotal($sql){
        $this->sql=$sql;
        $dT=$this->getDo($this->sql);        //求總數的游標
        $this->total=odbc_result($dT,'total');//這裡為何不能$this->d呢?
        return $this->total;
    }

    function getList($sql,$onepage,$page){
        $this->s=$sql;
        $this->onepage=$onepage;
        $this->page=$page;
        $this->dList=$this->getDo($this->s);    //連接表的游標
        $this->pages=ceil($this->total/$this->onepage);
        if($this->pages==0)
            $this->pages++; //不能取到第0頁
        if(!isset($this->page))
            $this->page=1;
        $this->fre = $this->page-1;                    //將顯示的頁數
        $this->nxt = $this->page+1;
        $this->nums=($this->page-1)*$this->onepage;
        //if($this->nums!=0){
        //    for($this->i=0;$this->i<$pg->getNums();odbc_fetch_row($this->dd),$this->i++);//同上
        //}
        //$this->i=0;//為何這部分不能封裝?
        return $this->dList;
    }

    function getFanye(){
        $str="";
        if($this->page!=1)
            $str.="<a href=".$PHP_SELF."?page=1> 首頁 </a><a href=".$PHP_SELF."?page=".$this->fre."> 前頁 </a>";
            else
                $str.="<font color=999999>首頁 前頁</font>";
        if($this->page<$this->pages)
            $str.="<a href=".$PHP_SELF."?page=".$this->nxt."> 後頁 </a>";
            else
                $str.="<font color=999999> 後頁 </font>";
        if($this->page!=$this->pages)
            $str.="<a href=".$PHP_SELF."?page=".$this->pages."> 尾頁 </a>";
            else
                $str.="<font color=999999> 尾頁 </font>";

        $str.="共".$this->pages."頁";
        $str.="您正浏覽第<font color=red>".$this->page."</font>頁";
        return $str;
    }

    function getNums(){
        return $this->nums;
    }

    function getOnepage(){//每頁實際條數
        return $this->onepage;
    }

    function getI(){
        return $this->i;
    }

    function getPage(){
        return $this->page;
    }

    function getMess($error){//定制消息
        echo"<center>$error</center>";
        exit;
    }
}

$pg=new Pages();
$pg->getConnect("lei","sa","star");
$pg->getTotal("select count(*) as total from xs");            //連學生表求總數
$pg->getList("select xs_name from xs order by xs_id",8,$page);
if($pg->getNums()!=0){
    for($i=0;$i<$pg->getNums();odbc_fetch_row($pg->dList),$i++);//同上
}
$i=0;
while(odbc_fetch_row($pg->dList)){
    $name=odbc_result($pg->dList,"xs_name");
    echo $name."<br>";
    if($i==$pg->getOnepage()){//跳出循環
        break;
    }
    $i++;
}
echo$pg->getFanye();
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved