简易分页种

Jun 13, 2016 pm 01:12 PM
count page sql static

简易分页类
这是一个简单易用的分页类。只需在你原有的程序中加两句、改一句就可以了
先贴代码
paging.php

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php class Paging {
  public static $count = 0;
  public static $size = 0;
  public static $page = 0;
  static function prepare($sql, $pagesize=10) {
    $page = isset($_GET['page']) ? $_GET['page'] : 1;
    $pageon = ($page - 1) * $pagesize;
    $sql = preg_replace('/select\s/i', '$0SQL_CALC_FOUND_ROWS ', $sql) . " limit $pageon, $pagesize";
    $rs = mysql_query($sql);
    $p = mysql_query('SELECT FOUND_ROWS()');
    list(self::$count) = mysql_fetch_row($p);
    self::$size = $pagesize;
    self::$page = $page;
    return $rs;
  }
  static function bar($tpl='') {
    if(!$tpl) $tpl = '<a href=?reset>首页 <a href="?prve">上一页</a> <a href="?next">下一页</a> <a href="?end">尾页</a>';
    $count = ceil(self::$count / self::$size);
    $page = self::$page;
    unset($_GET['page']);
    $d = array(
      'reset' => 1,
      'prve' => $page > 1 ? $page - 1 : 1,
      'next' => $page  $count,
    );
    foreach($d as $k=>$v) {
      $_GET['page'] = $v;
      $tpl = str_replace($k, http_build_query($_GET), $tpl);
    }
    echo $tpl;
  }
}


通常你都有类似这样的语句
$sql =".....";
$rs = mysql_query($sql);

$rs = mysql_query("select ....");


你只需改作
include 'paging.php';
$rs = paging::prepare($sql, 每页行数);
在需要出现分页条的地方写入
paging::bar();

就可以了,非常简单!


------解决方案--------------------
前排拜模。
这样的写法还是第一次见到。
 $sql = preg_replace('/select\s/i', '$0SQL_CALC_FOUND_ROWS ', $sql) . " limit $pageon, $pagesize";

sql还用preg_replace……
------解决方案--------------------

谢谢分享,确实很方便 ,简单易用 。学习了。
------解决方案--------------------
探讨
这是一个简单易用的分页类。只需在你原有的程序中加两句、改一句就可以了
先贴代码
paging.php
PHP code
class Paging {
public static $count = 0;
public static $size = 0;
public static $page = 0;
static function prepare($sql, $……
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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

Hot Topics

PHP Tutorial
1587
276
What are the BLOB and CLOB data types in SQL? What are the BLOB and CLOB data types in SQL? Aug 07, 2025 pm 04:22 PM

BLOBstoresbinarydatalikeimages,audio,orPDFsasrawbyteswithoutcharacterencoding,whileCLOBstoreslargetextsuchasarticlesorJSONusingcharacterencodinglikeUTF-8andsupportsstringoperations;2.Bothcanhandleuptogigabytesofdatadependingonthedatabase,butperforman

How to find the sum of a column in SQL? How to find the sum of a column in SQL? Aug 08, 2025 pm 05:54 PM

TofindthesumofacolumninSQL,usetheSUM()function,whichreturnsthetotalofallnumericvaluesinaspecifiedcolumnwhileignoringNULLs;1.Usebasicsyntax:SELECTSUM(column_name)ASaliasFROMtable_name;2.Ensurethecolumnhasnumericdatatoavoiderrors;3.ApplyWHEREtofilterro

How to get the first and last day of the year in SQL? How to get the first and last day of the year in SQL? Aug 11, 2025 pm 05:42 PM

ThefirstdayoftheyearisobtainedbyconstructingortruncatingtoJanuary1stofthegivenyear,andthelastdayisDecember31stofthesameyear,withmethodsvaryingbydatabasesystem;2.Fordynamiccurrentyeardates,MySQLusesDATE_FORMATorMAKEDATE,PostgreSQLusesDATE_TRUNCorDATE_

Understanding SQL Execution Context and Permissions Understanding SQL Execution Context and Permissions Aug 16, 2025 am 08:57 AM

SQL execution context refers to the identity or role when running SQL statements, which determine which resources and operation permissions can be accessed. Permission setting should follow the principle of minimum permissions, and common permissions include SELECT, INSERT, EXECUTE, etc. To troubleshoot permission issues, you need to confirm the login name, role permissions, EXECUTEAS settings and schema authorization. Performing context switching can be implemented through EXECUTEAS, but attention should be paid to user existence, permission granting and performance security impact. It is recommended to avoid arbitrarily assigning db_owner or sysadmin roles. The application account should only access necessary objects and be authorized through schema.

How to join a table to itself in SQL How to join a table to itself in SQL Aug 16, 2025 am 09:37 AM

Aself-joinisusedtocomparerowswithinthesametable,suchasinhierarchicaldatalikeemployee-managerrelationships,bytreatingthetableastwoseparateinstancesusingaliases,asdemonstratedwhenlistingemployeesalongsidetheirmanagers'nameswithaLEFTJOINtoincludetop-lev

What is the ALTER TABLE statement in SQL? What is the ALTER TABLE statement in SQL? Aug 08, 2025 pm 02:13 PM

TheALTERTABLEstatementisusedtomodifyanexistingtable’sstructurewithoutrecreatingit;1.AddanewcolumnusingADDCOLUMN;2.DropacolumnwithDROPCOLUMN,whichalsodeletesitsdata;3.RenameacolumnusingRENAMECOLUMN,withsyntaxconsistentinMySQL,SQLServer,andPostgreSQL;4

How to create a view in SQL How to create a view in SQL Aug 11, 2025 pm 12:40 PM

The syntax for creating a view is the CREATEVIEWview_nameASSELECT statement; 2. The view does not store actual data, but is based on the real-time query results of the underlying table; 3. The view can be modified using CREATEORREPLACEVIEW; 4. The view can be deleted through DROPVIEW; 5. The view is suitable for simplifying complex queries, providing data access control, and maintaining interface consistency, but attention should be paid to performance and logic, and finally ends with a complete sentence.

How to use FULL OUTER JOIN in SQL? How to use FULL OUTER JOIN in SQL? Aug 17, 2025 am 12:25 AM

AFULLOUTERJOINreturnsallrowsfrombothtables,withNULLswherenomatchexists;1)Itcombinesmatchingrecordsandincludesunmatchedrowsfrombothleftandrighttables;2)Itisusefulfordatareconciliation,mergereports,andidentifyingmismatches;3)Notalldatabasessupportitnat

See all articles