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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

See all articles