1,php分页类代码
<?php /** * php分页类 * by www.jb200.com */ //定义错误级别 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 '<a href="'.$link.'&'.$get.'='.($this->Page_number -1 ).'">Per</a> '; else echo '<a >Per</a> '; //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 ' <a class="cur">'.$i.'</a> '; // change current page' class else echo ' <a href="'.$link.'&'.$get.'='.$i.'">'.$i.'</a> '; // normal pages } //echo next button if($this->Page_number<$this->Pages)echo '<a href="'.$link.'&'.$get.'='. ($this->Page_number +1 ) .'">Nex</a> '; else echo '<a >Nex</a> '; //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分页类示例---www.jb200.com</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分页类的调用示例-www.jb200.com</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>