Home > Backend Development > PHP Tutorial > PHP pagination can use tables to paginate classes_PHP tutorial

PHP pagination can use tables to paginate classes_PHP tutorial

WBOY
Release: 2016-07-13 16:59:44
Original
1344 people have browsed it

* Variables needed when creating a new object: $query (sql statement to obtain the number of records from the data table), $page (current page number), $maxline (number of rows per page)) * 1. Showpage method: If the $query of the object created above is correct, call it directly to output paging information. * 2. Showtable method: Required variables: $query (SQL statement to read records from the database, do not add Limit, because it has been added in the method)

, so just add
The code is as follows Copy code

/*
* Directly output data table and paging information
* Variables needed when creating a new object: $query (sql statement to obtain the number of records from the data table), $page (current page number), $maxline (number of rows per page))
* 1. Showpage method: If the $query of the object created above is correct, call it directly to output the paging information
* 2. Showtable method: Required variables: $query (SQL statement to read records from the database, do not add Limit, because it has been added in the method)
* Directly output the table of

before and after to get a complete table
* 3. showresult method: directly return the $result resource according to the SQL in the submitted $query, and the table can be defined by yourself
* Example:
//Get the current page and define the maximum row per page
$page=1;
$maxline="10";
if(!empty($_GET["page"])){
$page=$_GET["page"];
}
//Define the SQL statement to calculate the total number of data in the table. This must be the same table and condition as the $query below, create the object, and output the page number and table
$query="select count(*) from mailbox";
$a=new PageList($query, $maxline, $page);
$a->showpage();
//Displaying the list here requires the same conditions as the above SQL statement
$query="select username,name,quota,created,modified,active from mailbox order by created desc";
echo "";
$a->showtable($query);
echo "
";
* */

class PageList{
private $link;
private $result;
private $maxline;
private $page=1;
private $startline=0;
private $countline;
public $countpage;
private $prevpage;
private $nextpage;
//Database connection needs to be modified to your own address
private $dbhost=DBHOST;
private $dbuser=DBUSER;
private $dbpasswd=DBPASSWD;
private $dbtable=DBTABLE;

/*
* Establish database connection in the constructor
* 1. The four parameters of the database connection are set as constants and recorded in the config.php page
* 2. Connect to the database and select the database
* 3. Set the encoding method of database execution to utf8
* 4. Assign the received $maxline and $page variables to the class attributes and become the general attributes of the class
* (where $query is the SQL of count(*), which is different from the query in the method below)
* 5. According to the $query statement submitted when creating a new object, execute a query on the database and assign the total number of records obtained to the class attribute $this->countline
* Divide the total number of records/number of rows per page, and then use the ceil function to round the high order to get the total number of pages and assign it to the class attribute $this->countpage
* 6. Based on the submitted current page number $page, calculate the numbers of the previous and next pages $this->prevpage and $this->nextpage
* It is also necessary to calculate the starting line for database reading $this->startline
* There are three situations here, page<2, page=last page, page>1 (you can not judge this situation, just use else)
* */
public function __construct($query,$maxline,$page){
@$this->link=mysql_connect($dbhost,$dbuser,$dbpasswd) or die($this->feedback='System Error ,Please contact admin');
@mysql_select_db($dbtable,$this->link) or die($this->feedback='System Error ,Please contact admin');
@mysql_query('set names utf8');
$this->maxline=$maxline;

//Read the number of lines and return the result to $coutline
$this->result=mysql_query($query) or die($this->feedback='System Error ,Please contact admin');
if($count=mysql_fetch_row($this->result)){
//intval converts the string to int, you don’t need to convert it, but this program is healthier
$this->countline = intval($count[0]);
$this->countpage = ceil($this->countline/$maxline);
}
//Determine whether the submitted $page is greater than the total number of pages
if($page<=$this->countpage){
$this->page=$page;
}

if($this->page<2){
$this->prevpage=0;
$this->nextpage=2;
$this->startline= 0;
}elseif($this->page==$this->countpage){
$this->prevpage=$this->page-1;
$this->nextpage=0;
$this->startline= ($this->page-1)*$this->maxline;
}else{
$this->prevpage=$this->page-1;
$this->nextpage=$this->page+1;
$this->startline= ($this->page-1)*$this->maxline;
}
}

/*
* Destructor
* Release resources and close database connection
* */
public function __destruct(){
mysql_free_result($this->result);
mysql_close($this->link);
exit();
}

/*
* Output paging information
* */
public function showpage(){
//$listnum displays the number of digits between the upper and lower pages, it must be an even number! Otherwise it cannot be divided by 2
$listnum=10;
echo $this->countline." Items, ".$this->countpage." Pages ";

  if($this->prevpage==0){
   echo "<<Prev ";
  }else{
   echo "prevpage."><<Prev ";
  }
  
  if($this->countpage<$listnum){ //判断总页数是否小于$listnum
$page_start=1;
$page_end=$this->countpage;
  }elseif($this->page<$listnum/2){ //判断当前页是否小于$listnum的一半
$page_start=1;
$page_end=$listnum;
}elseif($this->page>$this->countpage-($listnum/2)){  //判断当前页是否是最后几页了
   $page_start=$this->countpage-($listnum-1);
   $page_end=$this->countpage;
  }else{                    //如果上面的条件都不符合,那当前也正在中间
   $page_start=$this->page-($listnum/2-1);
   $page_end=$this->page+($listnum/2);
  }
  
  for($i=$page_start;$i<=$page_end;$i++){ //根据上面判断的start和end页码,循环输出之间的页码
if($i==$this->page){
    echo "".$i."  ";
   }else{
    echo "".$i."  ";
   }
  }
  
  if ($this->nextpage==0){
   echo " Next>>";
  }else{
   echo " nextpage.">Next>> ";
  }
  
 }

/*
 * 根据sql语句读取数据库中的数据,然后列成表单输出
 * 需要的变量:$field(字段),$table(表名),$startline(开始行),$maxline(每页显示行数)
 * 输出从表格的tr开始,从tr结束,所以在使用这个方法前后要加table的标签
 * */ 
 public function showtable($query){
  $query=$query." LIMIT ".$this->startline.",".$this->maxline;
  $result = mysql_query($query) or die($this->feedback='System Error ,Please contect admin');
  //行循环开始,定义一个$i变量,用来显示行号,每次执行一条while语句,指针就指向下一条数据
  $i=0;
  while ($fetch = mysql_fetch_assoc($result)){
   $i++;
   echo "".$i."";
   //列循环开始,因为通过while后,$fetch已经是个数组,所以通过foreach遍历数组即可
   foreach ($fetch as $value){
    echo "".$value."";
   }
   echo "";
  }
 }

/*
 * 这个方法是将资源传出,表格在外面自定义样式
 * */
 public function showresult($query){
  $result = mysql_query($query) or die($this->feedback='System Error ,Please contect admin');
  return $result;
 }
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/631299.htmlTechArticle* 在新建对象时需要的变量:$query(从数据表中获取记录数的sql语句),$page(当前页码),$maxline(每页几行))* 1、showpage方法:如果上面创建对象...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template