Home php教程 php手册 php分页可利用表格来分页类

php分页可利用表格来分页类

Jun 13, 2016 am 10:03 AM
php query s Pagination variable exist object number data New of kind Obtain in the table sheet Record

* 在新建对象时需要的变量:$query(从数据表中获取记录数的sql语句),$page(当前页码),$maxline(每页几行)) * 1、showpage方法:如果上面创建对象的$query正确,直接调用,即可输出分页信息 * 2、showtable方法:需要的变量:$query(从数据库读取记录的SQL语句,不要加Limit,因为在方法中已经添加)

的表格,所以只需在前后加上
 代码如下 复制代码

/*
 * 直接输出数据表和分页信息
 * 在新建对象时需要的变量:$query(从数据表中获取记录数的sql语句),$page(当前页码),$maxline(每页几行))
 * 1、showpage方法:如果上面创建对象的$query正确,直接调用,即可输出分页信息
 * 2、showtable方法:需要的变量:$query(从数据库读取记录的SQL语句,不要加Limit,因为在方法中已经添加)
 *   直接输出

就是完整的表格
 * 3、showresult方法:根据提交的$query中的SQL,直接将$result资源返回,表格可以自己定义
 * 示例:
//获取当前页,并定义每页最大行
$page=1;
$maxline="10";
if(!empty($_GET["page"])){
 $page=$_GET["page"];
}
//定义计算表内数据总数的SQL语句,这里必须和下面的$query是同一个表和条件,创建对象,输出页码和表格
$query="select count(*) from mailbox";
$a=new PageList($query, $maxline, $page);
$a->showpage();
//这里显示列表,需要和上面的SQL语句一样的条件
$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;
 //数据库联接,需要修改为您自己的地址
 private $dbhost=DBHOST;
 private $dbuser=DBUSER;
 private $dbpasswd=DBPASSWD;
 private $dbtable=DBTABLE;

/*
 * 构造函数中建立数据库联接
 * 1、数据库连接的4个参数设置为常量 记录在config.php页面中
 * 2、连接数据库,并选择数据库
 * 3、设置数据库执行的编码方式为utf8
 * 4、将接收到的$maxline,$page两个变量赋值给类属性,成为该类通用属性
 *   (其中$query是count(*)的SQL,和下面方法中的query是不一样的)
 * 5、根据新建对象时递交的$query语句,对数据库执行查询,将得到的总记录数赋值到类属性中$this->countline
 *   将总记录数/每页行数,再用ceil函数高位取整,得到总页数并赋值到类属性中$this->countpage
 * 6、根据递交的当前页码$page,算出前后页的数字$this->prevpage和$this->nextpage
 *  还有必须算出数据库读取的起始行$this->startline
 *  这里分3种情况,page1(这个情况可以不判断,直接用else)  
 * */
 public function __construct($query,$maxline,$page){
  @$this->link=mysql_connect($dbhost,$dbuser,$dbpasswd) or die($this->feedback='System Error ,Please contect admin');
  @mysql_select_db($dbtable,$this->link) or die($this->feedback='System Error ,Please contect admin');
  @mysql_query('set names utf8');
  $this->maxline=$maxline;
  
  //读取行数,并将结果返回$coutline
  $this->result=mysql_query($query) or die($this->feedback='System Error ,Please contect admin');
  if($count=mysql_fetch_row($this->result)){
   //intval将字符串转为int,可以不转,但这样的程序更健康
   $this->countline = intval($count[0]);
   $this->countpage = ceil($this->countline/$maxline);
  }
  //判断递交的$page是否大于总页数
  if($pagecountpage){
   $this->page=$page;
  }
  
  if($this->page    $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;
  }
 }

/*
 * 析构函数
 * 释放资源,关闭数据库连接
 * */
 public function __destruct(){
  mysql_free_result($this->result);
  mysql_close($this->link);
  exit();
 }
 
/*
 * 输出分页信息
 * */
 public function showpage(){
  //$listnum显示上下页中间的数字位数,一定要偶数阿!否则不能被2除
  $listnum=10;
  echo $this->countline." Items, ".$this->countpage." Pages  ";

  if($this->prevpage==0){
   echo "< }else{
echo "prevpage.">< ";
}

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;
 }
}
?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

Top 10 Global Digital Virtual Currency Trading Platform Ranking (2025 Authoritative Ranking) Top 10 Global Digital Virtual Currency Trading Platform Ranking (2025 Authoritative Ranking) Mar 06, 2025 pm 04:36 PM

In 2025, global digital virtual currency trading platforms are fiercely competitive. This article authoritatively releases the top ten digital virtual currency trading platforms in the world in 2025 based on indicators such as transaction volume, security, and user experience. OKX ranks first with its strong technical strength and global operation strategy, and Binance follows closely with high liquidity and low fees. Platforms such as Gate.io, Coinbase, and Kraken are at the forefront with their respective advantages. The list covers trading platforms such as Huobi, KuCoin, Bitfinex, Crypto.com and Gemini, each with its own characteristics, but investment should be cautious. To choose a platform, you need to consider factors such as security, liquidity, fees, user experience, currency selection and regulatory compliance, and invest rationally

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

Top 10 exchanges in the currency circle in 2025 latest digital currency app rankings Top 10 exchanges in the currency circle in 2025 latest digital currency app rankings Feb 27, 2025 pm 06:33 PM

Ranking of the top ten virtual currency trading platforms (latest in 2025): Binance: Global leader, high liquidity, and regulation has attracted attention. OKX: Large user base, supports multiple currencies, and provides leveraged trading. Gate.io: A senior exchange, with a variety of fiat currency payment methods, providing a variety of trading pairs and investment products. Bitget: Derivatives Exchange, high liquidity, low fees. Huobi: An old exchange that supports a variety of currencies and trading pairs. Coinbase: A well-known American exchange, strictly regulated. Phemex and so on.

See all articles