这个类是我经常使用的一个类,配合jquery实现ajax的分页,非常好用
最近在开发一个web2.0网站,纯ajax效果。。。
PHP代码
- <?php
- /*
- PAGE CLASS
- Copyright (C) 2004-2005 Harry Wang
- GNU General Public License 321
- This program is free software; you can
- redistribute it and/or modify it under the
- terms of the GNU General Public License as
- published by the Free Software Foundation;
- either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that
- it will be useful, but WITHOUT ANY WARRANTY;
- without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR
- PURPOSE. See the GNU General Public License
- for more details.
- You should have received a copy of the GNU
- General Public License along with this
- program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330,
- Boston, MA 02111-1307 USA
- */
- /**
- * 數據庫分頁類
- * 這個數據庫分頁類可以使用在帶有ADODB的數據庫中
- * @author Weizhen Wang <harry34010493@gmail.com>
- * @version $Id: page.inc.php,v 1.19 2007/10/15 10:00:05 nbx Exp $
- * @example /inc/example/page.example.php
- * 使用簡介:
- * $p = new ShowPage(根据?($DB), 表名字, 分頁條數, 具体sql語句(可選)); //建立新對象
- * $p->SetPageVar(pagecount); //設置翻頁的標示,默認是’p’
- * $pVar = $p->GetPageVar(); //返回當前PageVar
- * $p->GetTotalPageArray(); //生成下拉菜單
- * $p->GetTotalPage(); //返回總頁數
- * $p->GetLeftSide(); //返回向左的箭頭
- * $p->GetRightSide(); //返回向右的箭頭
- * $p->GetCurrentPage(); //返回當前頁面
- * $p->GetLimit(); //返回每頁顯示條數
- * $p->GetRowFrom(); //返回Select語句用的當前Row的位置 E.G."SELECT * FROM SupplierLogin LIMIT " . $p->GetRowFrom() . ", " . $p->GetLimit();
- * $p->SetVar(); //設置同時需要傳送的參數
- */
- class ShowPage
- {
- /**
- * 左翻頁
- * @var string
- */
- var $outputLeftSide;
- /**
- * 右翻頁
- * @var string
- */
- var $outputRightSide;
- var $file;
- /**
- * 翻頁參數
- * @var string
- */
- /**
- * 當前頁
- * @var int
- */
- var $curr;
- var $varstr;
- var $tPage;
- /**
- * 數據庫對象
- * @var DB
- */
- var $DB;
- /**
- * 數據庫表名
- * @var string
- */
- var $table;
- /**
- * 每頁顯示的數據條數
- * @var string
- */
- var $limit;
- /**
- * SQL語句
- * @var string
- */
- var $sqlQuery;
- var $tPageArray;
- var $total;
- var $tRow;
- var $target = ‘_self’;
- var $maxPage = 0;
- /**
- * Paging style "option" or "listing"
- */
- var $style = "listing";
- function ShowPage($DB, $maxpage=0, $limit, $sqlQuery = "", $pVar=‘p’) {
- $this->DB = $DB;
- $this->table = $table;
- $this->limit = $limit;
- $this->sqlQuery = $sqlQuery;
- $this->maxPage = ($maxpage>0)?$maxpage:0;
- $tmpGET = $_GET;
- $this->pVar = $pVar;
- //$this->pVar2 = $pVar;
- //echo $pVar;
- unset($tmpGET[$this->pVar]);
- $this->setVar($tmpGET);
- $tmpQuery = $sqlQuery;
- //adodb hack start
- if(DB_DRIVER == ‘pdo’)
- {
- $rs = $DB->query($tmpQuery);
- $rs = $DB->query("SELECT FOUND_ROWS()");
- $tPage = $rs->fetchColumn();
- }
- else
- {
- $rs = $DB->execute($tmpQuery) or die(print_r($DB->ErrorInfo()));
- $tPage = $rs->RecordCount();
- }
- $this->tRow = $tPage;
- $this->tPage = ($this->maxPage>0)?$this->maxPage:ceil($tPage / $this->limit);
- if ($this->tPage > 0)
- {
- for ($i = 1; $i < $this->tPage + 1; $i++)
- {
- $this->tPageArray[] = $i;
- }
- }
- else
- {
- $this->tPageArray[] = 0;
- }
- }
- /**
- * 設置翻頁參數
- * @param string $pvar 翻頁參數
- */
- function SetPageVar($pvar = ‘p’)
- {
- // set type turnpage parameter (E.G. http://www.aa.com/index.php?p=1)
- $this->pVar = $pvar;
- }
- /**
- * 返回總行數
- * @return int
- */
- function GetTotalRow()
- {
- return $this->tRow;
- }
- /**
- * 返回翻頁參數
- * @return string
- */
- function GetPageVar()
- {
- return $this->pVar;
- }
- /**
- * 返回當前頁數
- * @return int
- */
- function GetCurrentPage()
- {
- //echo $this->pVar;
- return $_GET[$this->pVar];
- }
- /**
- * 返回總頁數數組
- * @return array
- */
- function GetTotalPageArray()
- {
- return $this->tPageArray;
- }
- /**
- * 返回總頁數
- * @return int
- */
- function GetTotalPage()
- {
- return $this->tPage;
- }
- /**
- * 返回每頁顯示條數
- * @return int
- */
- function GetLimit()
- {
- return $this->limit;
- }
- /**
- * 返回數據行數
- * @return int
- */
- function GetRowFrom()
- {
- if ($this->GetCurrentPage() <= 1)
- {
- return 0;
- }
- else
- {
- return ($this->GetCurrentPage() – 1) * $this->GetLimit();
- }
- }
- /**
- * 返回朝左翻
- * @return string
- */
- function GetLeftSide()
- {
- $current = $_GET[$this->pVar];
- if (!$current)
- {
- $current = 1;
- }
- if ($current<1)
- {
- $current = 1;
- }
- if ($current>$this->tPage)
- {
- $current = $this->tPage;
- }
- if ($this->tPage > 1)
- {
- $this->outputLeftSide = ”;
- if ($current > 10)
- {
- $this->outputLeftSide = ‘<a href=’.$this->file.‘?’.$this->pVar.‘=1’.($this->varstr).‘ title="First Page" class="p_num" target=’.$this->target.‘>|<</a> ‘;
- }
- if ($current > 1)
- {
- $this->outputLeftSide .= ‘<a href=’.$this->file.‘?’.$this->pVar.‘=’.($current-1).($this->varstr).‘ title="Previous Page" class="p_num" target=’.$this->target.‘>上一页</a> ‘;
- }
- }
- return str_replace(‘"’, "‘", $this->outputLeftSide);
- }
- /**
- * 返回朝右翻
- * @return string
- */
- function GetRightSide() {
- $current = $_GET[$this->pVar];
- if (!$current) {
- $current = 1;
- }
- if ($current>$this->tPage) {
- $current = $this->tPage;
- }
- if ($current<1) {
- $current = 1;
- }
- if ($this->tPage > 1) {
- $this->outputRightSide = ‘‘;
- if ($current<$this->tPage) {
- $this->outputRightSide = ‘<a href=‘.$this->file.’?‘.$this->pVar.’=‘.($current+1).($this->varstr).’ title="Next Page" class="p_num" target="‘.$this->target.’">下一页</a> ‘;
- }
- if ($this->tPage>10 && ($this->tPage-$current)>=10 ) {
- $this->outputRightSide .= ‘<a href=’.$this->file.’?’.$this->pVar.’=’.$this->tPage.($this->varstr).’ title="Last Page" class="p_num" target="‘.$this->target.’">>|</a>’;
- }
- }
- return str_replace(‘"‘, "’", $this->outputRightSide);
- }
- /**
- * 返回一個完整的翻頁選項
- * 包含了向左翻(<< <), 直接選擇頁面數字, 向右翻(> >>), 和所需要的Javascript
- * @return string
- */
- function GetFullSide() {
- if($this->style == "options")
- {
- $sCenter = "<select onChange=’fnJump(this.value)’ name=’select’>\n";
- $pages = $this->GetTotalPageArray();
- foreach($pages as $page) {
- $selected = "";
- if($page == $this->GetCurrentPage())$selected = "selected";
- $sCenter .= " <option value=\"$page\" $selected>$page</option>\n";
- }
- $sCenter .= "</select>\n";
- $sScript = $this->GetTurnPageScript();
- }
- else
- {
- $sCenter = "";
- $pages = $this->GetTotalPageArray();
- foreach($pages as $page) {
- if($this->GetCurrentPage()-$page>=4)continue;
- if($page >= $this->GetCurrentPage()+4)break;
- $sCenter .= "<a href=\"".$_SERVER[‘PHP_SELF’]."?".$this->GetPageVar()."=$page"."&".($this->varstr). "\" class=’p_num’ target=’$this->target’>";
- if($page == $this->GetCurrentPage())
- {
- $sCenter .= "<strong>".$page."</strong>";
- }else $sCenter .= $page;
- $sCenter .= "</a>\n";
- }
- $sScript = "";
- }
- $sLeftSide = $this->GetLeftSide();
- $sRightSide = $this->GetRightSide();
- return ($sLeftSide ."\n". $sCenter ."\n". $sRightSide ."\n". $sScript);
- } // end func
- /**
- * 返回Sql語句
- * @return string
- */
- function GetNewSql() {
- return $this->sqlQuery . " LIMIT " . $this->GetRowFrom() . ", " . $this->GetLimit();
- } // end func
- /**
- * 返回翻頁時所需的Javascript
- * @return string
- */
- function GetTurnPageScript() {
- return "<SCRIPT LANGUAGE=\"JavaScript\">\n" .
- "<!–\n" .
- "function fnJump(page)\n" .
- "{\n" .
- "location.href=’".$_SERVER[‘PHP_SELF‘]."?" . $this->GetPageVar() ."=’+page" . "+’".($this->varstr) . "’;\n" .
- "}\n" .
- "//–>\n" .
- "</SCRIPT>\n";
- } // end func
- /**
- * 設置其它參數
- * 設置翻頁時需要通過用GET方法一同傳送過去的其他參數
- * @param array $data
- */
- function setVar($data) {
- // set the turnpage addition value need to transfer by get mode
- foreach ($data as $k=>$v) {
- $this->varstr.=‘&’.$k.‘=’.urlencode($v);
- }
- }
- }
- class ajax_ShowPage extends ShowPage
- {
- var $func;
- var $currentpage;
- function ajax_ShowPage($DB, $table, $limit, $sqlQuery = "", $function=‘ShowPage([page])’, $currentpage, $target=”)
- {
- $this->ShowPage($DB, $table, $limit, $sqlQuery);
- $this->func = $function;
- $this->currentpage = ($currentpage)?$currentpage:1;
- $this->target = $target;
- }
- function ajaxGetCurrentPage()
- {
- return $this->currentpage;
- }
- function ajaxGetRowFrom()
- {
- if ($this->ajaxGetCurrentPage() <= 1) {
- return 0;
- }
- else {
- return ($this->ajaxGetCurrentPage() – 1) * $this->GetLimit();
- }
- }
- function ajaxGetNewSql()
- {
- return $this->sqlQuery . " LIMIT " . $this->ajaxGetRowFrom() . ", " . $this->GetLimit();
- }
- function ajaxLeftSide() {
- $current = $this->currentpage;
- //echo $current ;
- if (!$current) {
- $current = 1;
- }
- if ($current<1) {
- $current = 1;
- }
- if ($current>$this->tPage) {
- $current = $this->tPage;
- }
- if ($this->tPage > 0) {
- //if ($current > 10) {
- $this->outputLeftSide = ‘<a href="JavaScript:’.$this->func."(‘1’)".’" title="" class="p_num" target="_self">第一页</a> ‘;
- //}
- if ($current > 1) {
- $this->outputLeftSide.= ‘<a href="JavaScript:’.$this->func."(".($current-1).")".’" title="" class="p_num" target="_self">上一页</a> ‘;
- }else{
- $this->outputLeftSide.= ‘<a href="JavaScript:’.$this->func."(".($current).")".’" title="" class="p_num" target="_self">上一页</a> ‘;
- }
- }
- return $this->outputLeftSide;
- }
- function ajaxRightSide() {
- $current = $this->currentpage;
- if (!$current) {
- $current = 1;
- }
- if ($current<1) {
- $current = 1;
- }
- if ($current>$this->tPage) {
- $current = $this->tPage;
- }
- if ($this->tPage > 0) {
- if ($current<$this->tPage) {
- $this->outputRightSide = ‘<a href="JavaScript:’.$this->func.‘(\”.($current+1).‘\’)" title="Next Page" class="p_num" target="_self">下一页</a> ‘;
- }else{
- $this->outputRightSide = ‘<a href="JavaScript:’.$this->func.‘(\”.($current).‘\’)" title="Next Page" class="p_num" target="_self">下一页</a> ‘;
- }
- // if ($this->tPage>10 && ($this->tPage-$current)>=10 ) {
- $this->outputRightSide .= ‘<a href="JavaScript:’.$this->func.‘(\”.$this->tPage.‘\’)" title="Next 10 Page" class="p_num" target="_self">最后</a>’;
- //}
- }
- return $this->outputRightSide;
- }
- function ajaxGetFullSide() {
- $sLeftSide = $this->ajaxLeftSide();
- $sRightSide = $this->ajaxRightSide();
- if($this->style == "options")
- {
- $sCenter = "<select onChange=’fnJump(this.value)’ name=’select’>\n";
- $pages = $this->GetTotalPageArray();
- foreach($pages as $page) {
- $selected = "";
- if($page == $this->ajaxGetCurrentPage())$selected = "selected";
- $sCenter .= " <option value=\"$page\" $selected>$page</option>\n";
- }
- $sCenter .= "</select>\n";
- $sScript = $this->ajaxGetTurnPageScript();
- }
- else
- {
- $sCenter = "";
- $pages = $this->GetTotalPageArray();
- foreach($pages as $page) {
- if($this->currentpage-$page>=5)continue;
- if($page >= $this->currentpage+5&$this->tPage-$page>2)continue;
- $sCenter .= ‘<a href="JavaScript:’.$this->func."(‘".$page."’)".’" class="p_num" target="_self">’;
- //echo $this->GetCurrentPage();
- if($page ==$this->currentpage)
- {
- $sCenter .= "<strong><font color=red>".$page."</font></strong>";
- }else $sCenter .= $page;
- $sCenter .= "</a>\n";
- }
- $sScript = "";
- }
- return ($sLeftSide ."\n". $sCenter ."\n". $sRightSide ."\n". $sScript);
- }
- function ajaxGetTurnPageScript()
- {
- return "<SCRIPT LANGUAGE=\"JavaScript\">\n" .
- "<!–\n" .
- "function fnJump(page)\n" .
- "{\n" .
- " ".$this->func."(page);\n" .
- "}\n" .
- "//–>\n" .
- "</SCRIPT>\n";
- } // end func
- }
- ?>
希望作者能给个实例,用这个类结合jquery分页,谢谢!