PHP兌現翻頁處理的類

來源:互聯網
上載者:User
PHP實現翻頁處理的類

PHP執行個體原始碼:PHP實現翻頁處理的類

class Page{

var $CountAll; //共有紀錄數
var $CountPage; //每頁顯示記錄數
var $Link; //顯示 完整的分頁資訊
var $ForPage; //上一頁
var $NextPage; //下一頁
var $FirstPage; //第一頁
var $LastPage; //最後一頁
var $CurrPage; //第幾頁
var $PageNum; //共有多少頁
var $Parameter; //參數
var $LimitNum; //不是統計全部記錄,而是顯示部分記錄,例如共有100條記錄,但是只統計顯示前50條

function Page($sql, $num=30){

//初始化,統計記錄數
$this->CountPage = $num;
global $mysql;
$sql = base64_decode($sql);
$result = $mysql->Query($sql);
if (0 != $mysql->AffectedRows()){
$row = $mysql->FetchArray($result);
$this->CountAll = $row[0];
}
else{
$this->CountAll = 0;
}
//print "共有 $this->CountAll
";
}

function ListPage($sql, $page=0,$sql_all,$other){
//查詢,定義變數,擷取資料

global $mysql;
//print "sql sql
";
if (isset($this->LimitNum) && $this->CountAll > $this->LimitNum){
$this->CountAll = $this->LimitNum;
}//更新總瀏覽記錄數

$sql_src = $sql;
//if ($page > 0){
$sql = base64_decode($sql);
$sql_all = base64_decode($sql_all);
$sql_src = $sql;
//}
//echo $sql;
if (($this->CountAll % $this->CountPage) == 0)//統計共有多少頁
$pagecount = (integer)($this->CountAll/$this->CountPage);
else
$pagecount = (integer)($this->CountAll/$this->CountPage)+1;
$this->ageNum = $pagecount;
if ($page > $this->ageNum)//如果頁碼超過頁碼總數則設為最大頁碼
$page = $this->ageNum;
if ($page <= 0)//如果頁碼小於等於零則將頁碼設定為1
$page = 1;

if ($this->CountAll == 0)
{
$this->CurrPage = 0;
}else{
$this->CurrPage = $page;
}
$first_start = ($page-1)*$this->CountPage;
$sql = $sql." limit ".$first_start.", ".$this->CountPage;
//print "2sql
";
$result = $mysql->Query($sql);
if (0 != $mysql->AffectedRows()){
$i = 0;
while($row = $mysql->FetchArray($result)){
$array[$i] = $row;
//print "name:".$array[$i][Name]."
";
$i++;
}
}

$sql = base64_encode($sql_src);
$sql_all = base64_encode($sql_all);
if ($pagecount >1){
if($page == 1){
$nextpage = $page+1;
$forpage = 1;
$this->Link = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
$this->NextPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
$this->LastPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
}
else if(($page > 1)&&($page < $pagecount)) {
$forpage = $page-1;
$nextpage = $page+1;
$this->Link = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
$this->ForPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
$this->NextPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
$this->FirstPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
$this->LastPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
}
else if ($page = $pagecount){
$forpage = $page-1;
$nextpage = 1;
$this->Link = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
$this->FirstPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
$this->ForPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
}
}
else{
$this->Link = ' ';
}

return $array;//$array;

}

}
/***************
使用例子

include("class.config.php");
include("class.mysql.php");
include("class.page.php");
global $mysql;
$config = new Config;
$mysql = new TDatabase($config);

$query_all = "select count(*) from user";
$page_object = new Page($query_all,20);
//new Page('統計記錄個數語句',每頁記錄個數)
if(empty($query_page))
$query_sql = "select * from user";
//注意這裡的變數名必須為 $query_sql $query_page ,因為下一頁的串連參數預設為 query_sql query_page

$list = $page_object->ListPage($query_sql,$query_page);
//ListPage('沒有limit的前一部分,系統自動根據補齊',察看的頁數)
$page_object->Parameter = '&action=view';
//這是傳送的Url 所帶的其它參數,如果有就修改變數 Parameter ,系統自動將她補在後面
//顯示資料
for ($i=0;$i< $page_object->CountPage;$i++)
print $list[$i][ID]."->".$list[$i][UserName]."
";
//返回的資料為二維哈西(關聯)數組,一維為紀錄的標識ID號,二維為哈西(關聯)數組,取值標識建議採取用資料庫中欄位名的方法,例如list[0][UserName]。

//顯示其他相關資料
echo $page_object->CountAll;//紀錄總數
echo $page_object->CountPage;//每頁顯示資料個數
echo $page_object->Link;//顯示完整的分頁資訊
echo $page_object->FirstPage;//第一頁
echo $page_object->NextPage;//下一頁
echo $page_object->ForPage;//上一頁
echo $page_object->LastPage;//最後一頁
echo $page_object->CurrPage;//第幾頁
echo $page_object->PageNum;//共有多少頁
$mysql->DatabaseClose();
****************/

?>

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.