php 分頁類別(源碼 實例)
分享一个php分页类,写的不错,提供了二个该分页类的调用实例,供大家学习参考。
1,php分页类代码 <?php /** * php分页类 * by bbs.it-home.org */ //定义错误级别 error_reporting(E_ALL); //分页类 class pagination{ public $Start; // 开始mysql查询 public $End; // 结束mysql查询 private $Number; //分布数据的数量 public $Pages; // 页数 private $N_p_p; //每页要显示的内容数量 private $Page_number; //当前页数 private $Buttons; //最大显示的按钮数,即每页面中显示出的页数 function __construct($number,$n_p_p=10,$page_number=1,$buttons=5) { //page start from 1 $page_number = ($page_number<1) ? 1 : $page_number ; $this-> Number = $number; $this-> N_p_p = $n_p_p; $this-> Pages = ceil ( $this-> Number / $this-> N_p_p ) ; $this-> Buttons = $buttons ; $page_number= ($page_number>$this->Pages) ? $this->Pages : $page_number ; $this-> Page_number = $page_number ; $this-> Ret() ; } public function Show_Pagination($link,$get='page',$div_class_name='pagination') { //if pages == 1 , no need to print pagination if($this->Pages==1)return; //$link is the addres of current page //$get is name of get method //echo pagination's div echo'<div class="'.$div_class_name.'">'; //echo pre button if($this->Page_number>1)echo 'Page_number -1 ).'">Per '; else echo 'Per '; //print button $this->Buttons=(int)$this->Buttons; $start_counter = $this->Page_number-floor($this->Buttons/2);//for normal mode $end_conter = $this->Page_number+floor($this->Buttons/2);//for normal mode //try to buttons exactly equal to $Buttons if($start_counter<1) $end_conter=$end_conter+abs($start_counter); if($end_conter>$this->Pages) $start_counter=$start_counter-($end_conter-$this->Pages); if(($this->Page_number-floor($this->Buttons/2))<1)$end_conter ++; for ($i=$start_counter;$i<=$end_conter;$i++) { if($i>$this->Pages || $i<1)continue; //no print less than 1 value or grater than totall page if($i==$this->Page_number)echo ' '.$i.' '; // change current page' class else echo ' '.$i.' '; // normal pages } //echo next button if($this->Page_number<$this->Pages)echo 'Page_number +1 ) .'">Nex '; else echo 'Nex '; //close div tag echo'</div>'; } //give the page number and return start and end of selection private function Ret() { $this->Start=(($this->Page_number-1)*$this->N_p_p); $this->End= $this->N_p_p ; } } 登入後複製 2,分页类的示例一 <html> <head> <style type="text/css"> /* style for show*/ .pagination{direction:ltr} .pagination a{border-radius:3px;background-color:#eee;color:#555;border:1px solid #aaaaaa;padding-top:2px;padding-bottom:2px; padding-right:5px;padding-left:5px;text-decoration:none;} .pagination a:hover ,.pagination .cur{border-color:#0C52CE;color:#0C52CE;background-color:#fff;} </style> <title>php分页类示例---bbs.it-home.org</title> </head> <?php //include class: require_once"pagination.php"; //get the numbet of current page //note! you should safe it if(isset($_GET['page'])){ $page=$_GET['page']; } else $page=1; //connect to db ... mysql_connect("localhost","root","");//use your host,username and password to connect to the db mysql_select_db("dbname");//select your database //run query to get number of all records $query=mysql_query("select count(id) from table"); //raplace table with your table name $totall=mysql_result($query,0); //creat new object $pagination=new pagination($totall,3 /*number of content per page*/,$page,5 /*number of button to show*/); //get records of current page $SecondQuery=mysql_query("select id from table order by id desc limit $pagination->Start , $pagination->End"); //echo your result while($row=mysql_fetch_assoc($SecondQuery)) { echo 'id='.$row['id'].'<br>'; } //show pagination: $pagination->Show_Pagination("ExampleMysql.php?param1=value1",'page','pagination'); ?> 登入後複製 3,分页类的调用示例二 <html> <head> <style type="text/css"> /* style for show*/ .pagination{direction:ltr} .pagination a{border-radius:3px;background-color:#eee;color:#555;border:1px solid #aaaaaa;padding-top:2px;padding-bottom:2px; |padding-right:5px;padding-left:5px;text-decoration:none;} .pagination a:hover ,.pagination .cur{border-color:#0C52CE;color:#0C52CE;background-color:#fff;} </style> <title>php分页类的调用示例-bbs.it-home.org</title> </head> <?php //include分页类文件 require_once "pagination.php"; //param of url to specify page_number $get_param='page'; //get current page from url $current_page=(isset($_GET[$get_param]) && is_numeric($_GET[$get_param]))?$_GET[$get_param]:1; //notice: when get param , youe should SAFE it //get totall available content // for example you can get number of news from news table in database ( mysql_num_rows or count()) $totall_content=120; // for example //creat new cat object $cat=new pagination($totall_content,10 /*number of content per page*/,$current_page,5 /*number of button*/); //when you want to load content of page: for example in db queris : // select * from table where conditions order by key limit $cat->Start , $cat->End //for show : $cat->Show_Pagination("example.php?",'page','pagination'); ?> </html> 登入後複製 |
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
2 週前
By DDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP客戶端URL(curl)擴展是開發人員的強大工具,可以與遠程服務器和REST API無縫交互。通過利用Libcurl(備受尊敬的多協議文件傳輸庫),PHP curl促進了有效的執行

文章討論了PHP 5.3中介紹的PHP中的晚期靜態結合(LSB),允許靜態方法的運行時間分辨率調用以更靈活的繼承。 LSB的實用應用和潛在的觸摸

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

使用PHP的cURL庫發送JSON數據在PHP開發中,經常需要與外部API進行交互,其中一種常見的方式是使用cURL庫發送POST�...

深入解讀ReactPHP的非阻塞特性ReactPHP的一段官方介紹引起了不少開發者的疑問:“ReactPHPisnon-blockingbydefault....
